![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
how to check if the socket is still connected?
In my program, i have a socket that always connected to server.
I whish to check the socket is still connected to server everytime message send through the socket.. How to do this?
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
{
return;
}
// Create a socket.
g_SocketPoll = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
//Check for errors to ensure that the socket is a valid socket.
if ( g_SocketPoll == INVALID_SOCKET )
{
WSACleanup();//terminate the use of the WS2_32 DLL
return;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( g_sIPAddress.c_str() );
clientService.sin_port = htons( g_nPort );
if ( connect( g_SocketPoll, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR)
{
WSACleanup();
return;
}
//decide to have a while loop here to check if the socket is still connected?
while( true )
{
bytesSent = send( g_SocketPoll, sLine.c_str(), strlen(sLine.c_str()), 0 );
} |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You can only know that you had a successful connection after a successful transaction. In the absence of separate control/status signals, that's as good as it gets. It doesn't project into the future. Is that what you're asking?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|