Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Check if a socket in Windows is connected or not

Posted on 2011-10-04
Medium Priority
Last Modified: 2012-05-12
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;
  ..do 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?

Question by:ProjectZIG
LVL 16

Expert Comment

ID: 36913751
You might be able to use getsockname() and check for SOCNOTCONN or a zero address.

Expert Comment

ID: 36914877
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.
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 36918753
'getpeername()' (http://pubs.opengroup.org/onlinepubs/000095399/functions/getpeername.html) 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

LVL 15

Expert Comment

ID: 36922222
You can check if your still connected using a function like this. For the rest if the sockopt flags go here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738544(v=vs.85).aspx

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;

Author Closing Comment

ID: 37011648
THanks, works perfectly

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
We recently endured a series of broadcast storms that caused our ISP to shut us down for brief periods of time. After going through a multitude of tests, we determined that the issue was related to Intel NIC drivers on some new HP desktop computers …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question