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?

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
       }

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.

here is the documentation for the connect function. you can find the error constants and the way to detect failure using it.

Sweet, Thanx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.