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