connection drop
Hi all. I made a socket connector for a smtp server, but i didnt think to test what it should do in case the server prematurely disconnected before anything was done. So i was wondering. Does connect() give out a error if the server disconnected early in the program?
If so, how would i act on this type of situation. What i would like to do is make it try to reconnect if it gets disconnected early. Any suggestions? Thanx in advance!
# 1 Re: connection drop
You can look for an error with select(). You keep polling select to see if an error has occured. Also, WSAGetLastError() gives you the last socket error.
It's been a while since I've used them, so I can't remember details.
jalway at 2007-11-10 22:26:02 >

# 2 Re: connection drop
Also, WSAGetLastError() gives you the last socket error.
True. But now i need help on how to use it properly
cout << WSAGetLastError() << endl;
That returns 0 when the connection is severed early. I cant find out a way to use this to my advantage though. First i tried this...
if(WSAGetLastError==0){
cout << "Restarting " << endl;
... // Reconnect function
}
And nothing happened. Does anyone have any suggestions that will help me work this out? Thanx in advance!
# 3 Re: connection drop
Look at "select", it might do it for you.
This link should help:select (http://www.codersource.net/winsock_tutorial_server_select_model.html)
Setting up select is a bit of work, but it gives you more flexibility with Windows Sockets.
Here is the opening of the article:
Windows provides different models for programming with sockets. This winsock tutorial explains, one of the models which uses select function. The other winsock models are using event object, overlapped model and IO Completion port.
This model design is based on Berkeley socket implementation. The basic idea is to use the select function to determine if a socket is to be read or to be written. This can be used without the problems of blocking.
The select function takes 3 parameters. All of them are of type FD_SET. One parameter of type FD_SET is used to check if the socket is to be read, the other is used to check if the socket is to be written and the 3 is used to check if there is any out of band data. The most important among the 3 parameters is of course the first two. The sample program explained in this winsock tutorial will give a good idea on how to use them. To summarize,
Read fd_set can check
Data is available for reading
Connection has been closed or terminated
Write fd_set can identify the following
Data can be sent
If a non-blocking connect call is being processed, the connection has succeeded.
Bold above is mine for emphasis.
This guy has some code that is supposed to detect a dropped connection:
Detection of Connection Abort (http://www.dev-archive.com/cpp/i-n/network/tcpip/article.php/c2431/)
He uses both select() and recv().
Btw, just for your information, here is a link to a table of winsock error codes:
Error Codes (http://help.globalscape.com/help/support/Error_Codes/Winsock_Error_Codes.htm)
jalway at 2007-11-10 22:28:05 >
