Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. socket connection

socket connection

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadminhelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nahitan
    wrote on last edited by
    #1

    Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi

    M L M 3 Replies Last reply
    0
    • N nahitan

      Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What state is the connection in (according to netstat)? CLOSE_WAIT? When the connection drops, recv() will either return 0 or SOCKET_ERROR (WSAGetLastError() will get the error code) - I can't remember which right off hand (brain in screen-saver mode). Either way, when you get an error on send() or recv() you can notify the 3rd application that the connection dropped. A breakpoint right after a recv() call will show you which occurs when you switch off the connection. Lost connections will linger (you'll see them in netstat) until you officially close the socket or the application. Therefore, when you detect the network down you should close the socket when unrecoverable error codes are returned (they are listed in the send()/recv() API specs). Hope this helps a little :) Mark

      N 1 Reply Last reply
      0
      • M Mark Salsbery

        What state is the connection in (according to netstat)? CLOSE_WAIT? When the connection drops, recv() will either return 0 or SOCKET_ERROR (WSAGetLastError() will get the error code) - I can't remember which right off hand (brain in screen-saver mode). Either way, when you get an error on send() or recv() you can notify the 3rd application that the connection dropped. A breakpoint right after a recv() call will show you which occurs when you switch off the connection. Lost connections will linger (you'll see them in netstat) until you officially close the socket or the application. Therefore, when you detect the network down you should close the socket when unrecoverable error codes are returned (they are listed in the send()/recv() API specs). Hope this helps a little :) Mark

        N Offline
        N Offline
        nahitan
        wrote on last edited by
        #3

        Thanks for the tip, as I mentioned before when I disconnect the wireless network of the computer so that the connection ids dropped, netstat still shows that the connection is stablished!? until I close one of the application. and as my application should receive messages from the other application through the socket, I do not understand that the connection is down. Only when IN close the second application, I get the socket closed message and in netstat I can see close_wait. Thanks,

        M 1 Reply Last reply
        0
        • N nahitan

          Thanks for the tip, as I mentioned before when I disconnect the wireless network of the computer so that the connection ids dropped, netstat still shows that the connection is stablished!? until I close one of the application. and as my application should receive messages from the other application through the socket, I do not understand that the connection is down. Only when IN close the second application, I get the socket closed message and in netstat I can see close_wait. Thanks,

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Yes, you'll see the connection in netstat because the socket is still allocated even though it may be disconnected physically. It will remain in CLOSE_WAIT (waiting for close as the name implies) until you call closesocket(). Even after calling closesocket(), if there is/was pending data on the connection it will "linger" by default for I think 120 seconds (so you'll see it in netstat for that long). To force it to close immediately you can turn off the linger option for the socket, something like:

          // force hard/abortive close
          linger Linger;
          Linger.l\_onoff = 1;  
          Linger.l\_linger = 0;
          ::setsockopt(hSocket, SOL\_SOCKET, SO\_LINGER, (const char \*)&Linger, sizeof(linger));
          ::closesocket(hSocket);
          hSocket = INVALID\_SOCKET;
          
          1 Reply Last reply
          0
          • N nahitan

            Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Send a ping packet to the other IP address. If it doesnt come back, youknow the link is dead.

            Truth is the subjection of reality to an individuals perception

            1 Reply Last reply
            0
            • N nahitan

              Hi there, I have an application that uses TCP/IP socket connection to another application and I was wondering how I can check if the connection is up or not in the other word I need to notify a 3rd application if the connection between the first two has droped for any reasons. I tested today that if I am connecting to the second application that sits in another computer and I turn off the wireless connection button on the second computer so that the connection would be dropped, when I run the netstat command in dos command window, it shows that the connection is still up and my application does not recognises that the connection is actually dropped until I physically close the second application. Could any one inform me that how these things work and how can I make my application underestands that the connection between the sockets is dropped do to wireless or network problem and so on? I really appreciate any suggestions. Best reagards, Nahi

              M Offline
              M Offline
              Moak
              wrote on last edited by
              #6

              You could wait until the TCP/IP stack reports that the connection is down (can take very long). Alternatively send a small connection testing packet (e.g. smilar to IRC's PING/PONG mechnism), sometimes you will receive immediately an error saying that the connection is down... or you can timeout the connection if you haven't received a connection test packets from the other peer for a while. [corrected typos] :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups