0

If I have a MFC program that opens a socket connection, how would I test to see if the socket has succesfully opened or not?

2
Contributors
4
Replies
5
Views
11 Years
Discussion Span
Last Post by atrusmre
0

If I have a MFC program that opens a socket connection, how would I test to see if the socket has succesfully opened or not?

almost any function in the winsock api returns the value SOCKET_ERROR in the event of failure. you only have to check this for errors. if you want details on the error number, you can use the GetLastError() function that will give the number of the error occured. Look up in the msdn link for error codes for a text explanation. you can also use the FormatMessage() function to programatically get the text explanation.


for eg.

if ( socket.function(  parameter1,... ) == SOCKET_ERROR )
       {
                  cout << GetLastError();
                  // do error processing
       }
       else
       {
                  //successfull operation
       }
0

What if I"m looking for an error on connect, for example a connection timeout? I tried adding your example to the socket.connect(ip,port) function, but it just sits there thinking.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.