Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   how to check if the socket is still connected? (http://www.programmingforums.org/showthread.php?t=10355)

myName Jun 14th, 2006 3:44 AM

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 );

}


DaWei Jun 14th, 2006 6:27 AM

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?


All times are GMT -5. The time now is 10:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC