Check if a socket in Windows is connected or not

Posted on 2011-10-04
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; 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

    You might be able to use getsockname() and check for SOCNOTCONN or a zero address.
    LVL 8

    Expert Comment

    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

    '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

    LVL 15

    Expert Comment

    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;

    Author Closing Comment

    THanks, works perfectly

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
    Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now