Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2006, 4: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
Old Jun 14th, 2006, 7:27 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

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 7:07 PM.

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