Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jun 14th, 2006, 3:44 AM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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 );

}
myName is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:40 AM.

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