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 : How to gracfully stop application when recv() function is blocking

SOCKET : How to gracfully stop application when recv() function is blocking

Scheduled Pinned Locked Moved C / C++ / MFC
jsontutorialquestion
5 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.
  • P Offline
    P Offline
    pandit84
    wrote on last edited by
    #1

    Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?

    P J 2 Replies Last reply
    0
    • P pandit84

      Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      You have two obvious choices (and maybe a few more esoteric ones). 1. Google "non-blocking socket" and see how you can get to read what data is available, when you want to get it. 2. Fire up another thread which can block on the recv() call, but doesn't block your whole app. Another thread can then take control and shut down or whatever you need to do. Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994.

      C 1 Reply Last reply
      0
      • P Peter_in_2780

        You have two obvious choices (and maybe a few more esoteric ones). 1. Google "non-blocking socket" and see how you can get to read what data is available, when you want to get it. 2. Fire up another thread which can block on the recv() call, but doesn't block your whole app. Another thread can then take control and shut down or whatever you need to do. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        I remember way back when me and my buddy were at the university and had a class about networking. The class got an assignment to make a simple chat program with 2 participants using sockets. At that point we never heard of non-blocking sockets before (not sure about threads). So most people made chat programs in which the two sides could talk alternately, A sending something, then B sending, then A again, then B and so on. We on the other hand, created a "ping-pong" mechanism, a kind of "token" packet would be sent here and there between the two clients constantly, if one had something to say, it would send its content along with the token to the other client, if it had nothing to say then it would just send the token alone back. This way the clients didn't seem to block waiting for the other side to say something. Ah, the good old naive young days... :)

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > //TODO: Implement signature here<

        1 Reply Last reply
        0
        • P pandit84

          Hi , I am working on an application which involves socket communication. Current in my read () function 'recv()' method is blocking, i.e. does not allow execution until it gets some data. Here at this point I want to gracfully stop application . Is there any API for this ?

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          recv() is acting on Socket S. Keep S in a list (or some other available source). On shutdown spin up a thread. Grab S. Close S. Then recv() will return. Modify the code that uses that to behave correctly.

          P 1 Reply Last reply
          0
          • J jschell

            recv() is acting on Socket S. Keep S in a list (or some other available source). On shutdown spin up a thread. Grab S. Close S. Then recv() will return. Modify the code that uses that to behave correctly.

            P Offline
            P Offline
            pandit84
            wrote on last edited by
            #5

            Thanks all. I have implemented closesocket()option successfully

            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