Intro.
There are many situations in which we need to check up for the Internet connection state of the local computer in which our application is running. I've seen so many people asking about this question in many forums.. and i've als got this query from some more people. Actually, its not heavy task as many people thinks :)
How To.
Single call to a Windows API with an if statement can do it for you :)
The InternetGetConnectedState() in Windows takes two parameters.. one is a Pointer to a variable that receives the connection description, and the other is a reserved DWORD value which always must be Zero.
Code.
void CheckInetStat( )
{
DWORD dwFlags;
if( InternetGetConnectedState( &dwFlags, NULL ) )
{
if ( dwFlags & INTERNET_CONNECTION_CONFIGURED )
{
MessageBox( hWnd, _T( "This System has a valid Internet Connection, but it might or might not be currently connected." ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_CONNECTION_LAN)
{
MessageBox( hWnd, _T( "This System uses LAN for Internet Connection" ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_CONNECTION_MODEM)
{
MessageBox( hWnd, _T( "This System is using a Modem for Internet Connection" ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_CONNECTION_MODEM_BUSY )
{ //No longer used.
MessageBox( hWnd, _T( "The 'Modem is Busy" ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_CONNECTION_OFFLINE)
{
MessageBox( hWnd, _T( "This System is currently in Offline Mode" ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_CONNECTION_PROXY)
{
MessageBox( hWnd, _T( "This System is using Proxy Server for Internet" ), _T( "Internet Status" ), MB_OK );
}
if ( dwFlags & INTERNET_RAS_INSTALLED)
{
MessageBox( hWnd, _T( "This system has RAS Installed" ), _T( "Internet Status" ), MB_OK );
}
} //End of Function.
PS : I love to hear from you. So, please leave Feedbacks and Comments and help me Improve :-)
Search This Blog
Showing posts with label Networking and Ports. Show all posts
Showing posts with label Networking and Ports. Show all posts
Friday, July 17, 2009
Thursday, July 24, 2008
Serial COM Port Communication - C++
This program simply Transfers data between two computers. The communication is done through the COM1 Serial port. The program listens on the port, get the status of the port and displays characters of data to the console if available. This program also sends the characters typed in by the user to the COM1 port. Program will be terminated upon pressing Esc key.
bios.h and conio.h are the header files you need to include.
#include
#include
#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 0x02 0x00 0x00)
// 1200 baud, 7 data bits,
int main(void)
{
int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");
while (1)
{
status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/
putch(out);
if (kbhit())
{
if ((in = getch()) == 27) /* ASCII of Esc*/
break;
bioscom(1, in, COM1); /*output a data*/
}
}
return 0;
}
Turbo C Function used here is bioscom(). The syntax of function is :
bioscom(int cmd, char abyte, int port);
_bios_serialcom(int cmd ,int port, char abyte);
bioscom() and _bios_serialcom() uses the bios interrupt 0x14 to perform various communicate the serial communication over the I/O ports given in port.
1) cmd: The I/O operation to be performed.
0
_COM_INIT
Initialise the parameters to the port
1
_COM_SEND
Send the character to the port
2
_COM_RECEIVE
Receive character from the port
3
_COM_STATUS
Returns rhe current status of the communication port
2)portid: port to which data is to be sent or from which data is to be read.
0: COM1
1: COM2
2: COM3
3)abyte:
When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyte is ignored.
When you compile and run the above program in both the computers, The characters typed in one computer should appear on the other computer screen and vice versa. Initially, we set the port to desired settings as defined in macro settings. Then we waited in an idle loop until a key is pressed or a data is available on the port. If any key is pressed, then kbhit() function returns non zero value. So will go to getch function where we are finding out which key is pressed. Then we are sending it to the com port. Similarly, if any data is available on the port, we are receiving it from the port and displaying it on the screen.
Communication
bios.h and conio.h are the header files you need to include.
#include
#include
#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 0x02 0x00 0x00)
// 1200 baud, 7 data bits,
int main(void)
{
int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");
while (1)
{
status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/
putch(out);
if (kbhit())
{
if ((in = getch()) == 27) /* ASCII of Esc*/
break;
bioscom(1, in, COM1); /*output a data*/
}
}
return 0;
}
Turbo C Function used here is bioscom(). The syntax of function is :
bioscom(int cmd, char abyte, int port);
_bios_serialcom(int cmd ,int port, char abyte);
bioscom() and _bios_serialcom() uses the bios interrupt 0x14 to perform various communicate the serial communication over the I/O ports given in port.
1) cmd: The I/O operation to be performed.
0
_COM_INIT
Initialise the parameters to the port
1
_COM_SEND
Send the character to the port
2
_COM_RECEIVE
Receive character from the port
3
_COM_STATUS
Returns rhe current status of the communication port
2)portid: port to which data is to be sent or from which data is to be read.
0: COM1
1: COM2
2: COM3
3)abyte:
When cmd =2 or 3 (_COM_SEND or _COM_RECEIVE) parameter abyte is ignored.
When you compile and run the above program in both the computers, The characters typed in one computer should appear on the other computer screen and vice versa. Initially, we set the port to desired settings as defined in macro settings. Then we waited in an idle loop until a key is pressed or a data is available on the port. If any key is pressed, then kbhit() function returns non zero value. So will go to getch function where we are finding out which key is pressed. Then we are sending it to the com port. Similarly, if any data is available on the port, we are receiving it from the port and displaying it on the screen.
Communication
Subscribe to:
Posts (Atom)