Check if a socket in Windows is connected or not

Hey guys,

  I have a DLL that opens a socket connection, and handles everything.  I have another DLL set up, called from the same program, that I would like to pass the socket to and have it check to see if the socket is still connected or not.

  Something like this:
  SOCKET sock; other work, such as connect, etc...
  int ret = isSocketConnected(sock);

  And have the return code be a negative number if the socket isn't connected anymore, and anything other than a negative number if the socket IS connected and there are no existing errors.  If possible, I want to do this without recv() as I want to leave the data in the pipe, so my program can handle it.  This will simply be to check and see if the socket is still connected, so I can re-connect if needed.

  Is this possible?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You might be able to use getsockname() and check for SOCNOTCONN or a zero address.
This can be achieved by implementing KeepAlive messages.
telnet and many other network applications use the method of KeepAlives to check if the connection is active and also use that to tell the server that client is active.

You can search for KeepAlive for sockets and will find many pages.
'getpeername()' ( would usually be tha API of your choice, e.g.
int isSocketConnected(socket sock) {

  struct sockaddr dummy;

  int n = sizeof(dummy);

  if (ENOTCONN == getpeername(sock,&dummy,&n) {

    // not connected

    return 0;

  // rule out other error conditions
  // ...

  return 1; // connected!

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You can check if your still connected using a function like this. For the rest if the sockopt flags go here:

BOOL IsConnected(Socket s)
        int optval;
        Int optlen = sizeof(optval);
        // SO_ERROR is key here as it allows you to check the socket for errors and return a response. This is also cross platform compatible ;)
        int res = getsockopt(s,
        //Check the value here. If it fails you either had a connection reset(close) or error of sort.
        if(optval==0 && res==0) return true;
        return false;
ProjectZIGAuthor Commented:
THanks, works perfectly
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.