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 question: When does a client connect to a server?

Socket question: When does a client connect to a server?

Scheduled Pinned Locked Moved C / C++ / MFC
questionsysadmin
7 Posts 3 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.
  • F Offline
    F Offline
    FreeCastle
    wrote on last edited by
    #1

    Hi, I am using Winsock(only TCP) and I wondered when a client really connects to a server. With connecting I mean the whole process of establishing a TCP connection (that means Handshake, and so on). Is this done via the connect(...) function or is it every time done when the client sends data to the server by the send(...) function? The background of my question is, that I have a client app and I make a call to the connect() function when I initialize the client app. Now the user can choose between different actions, which asks the server for some data. That means, that there is only data send when the user chooses one of this actions; actually he can do nothing all the time. Is the way I do it okay, or would it be better to connect only to the server when the user chooses a specific action and then disconnect again (doing this then for every action)?

    M 1 Reply Last reply
    0
    • F FreeCastle

      Hi, I am using Winsock(only TCP) and I wondered when a client really connects to a server. With connecting I mean the whole process of establishing a TCP connection (that means Handshake, and so on). Is this done via the connect(...) function or is it every time done when the client sends data to the server by the send(...) function? The background of my question is, that I have a client app and I make a call to the connect() function when I initialize the client app. Now the user can choose between different actions, which asks the server for some data. That means, that there is only data send when the user chooses one of this actions; actually he can do nothing all the time. Is the way I do it okay, or would it be better to connect only to the server when the user chooses a specific action and then disconnect again (doing this then for every action)?

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

      With the TCP protocol, the connection is made once - during the connect() call. You could connect for every transaction or keep a connection open. Whatever suits your needs best. Connecting every transaction adds the slight overhead (bandwidt/CPU) of establishing/closing the connection. Web browsers do alot of that :) Mark

      "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

      F 1 Reply Last reply
      0
      • M Mark Salsbery

        With the TCP protocol, the connection is made once - during the connect() call. You could connect for every transaction or keep a connection open. Whatever suits your needs best. Connecting every transaction adds the slight overhead (bandwidt/CPU) of establishing/closing the connection. Web browsers do alot of that :) Mark

        "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

        F Offline
        F Offline
        FreeCastle
        wrote on last edited by
        #3

        Hmm yes I thought of something like this... But this yields in my next question: How can I disconnect from a server? I think there doesn't exist a command like disconnect(). So how is the best way to do this?

        L 1 Reply Last reply
        0
        • F FreeCastle

          Hmm yes I thought of something like this... But this yields in my next question: How can I disconnect from a server? I think there doesn't exist a command like disconnect(). So how is the best way to do this?

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          FreeCastle wrote:

          I think there doesn't exist a command like disconnect()

          They hide that information in the documentation[^] :rolleyes:

          led mike

          F 1 Reply Last reply
          0
          • L led mike

            FreeCastle wrote:

            I think there doesn't exist a command like disconnect()

            They hide that information in the documentation[^] :rolleyes:

            led mike

            F Offline
            F Offline
            FreeCastle
            wrote on last edited by
            #5

            Yes I know closesocket(), but I thought that there might be a better way, because when I call closesocket() then I also have to create the socket again, before calling the connect function. But anyway thank you both.

            M 1 Reply Last reply
            0
            • F FreeCastle

              Yes I know closesocket(), but I thought that there might be a better way, because when I call closesocket() then I also have to create the socket again, before calling the connect function. But anyway thank you both.

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

              You're welcome (from led mike) :)

              FreeCastle wrote:

              because when I call closesocket() then I also have to create the socket again, before calling the connect function.

              That is part of the overhead of using that method. I recommend: Also follow led mike's link into Graceful Shutdown, Linger Options, and Socket Closure[^] for more general info. Then check out DisconnectEx[^] for disconnecting a socket and reusing it (on XP+). Cheers, Mark

              "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

              F 1 Reply Last reply
              0
              • M Mark Salsbery

                You're welcome (from led mike) :)

                FreeCastle wrote:

                because when I call closesocket() then I also have to create the socket again, before calling the connect function.

                That is part of the overhead of using that method. I recommend: Also follow led mike's link into Graceful Shutdown, Linger Options, and Socket Closure[^] for more general info. Then check out DisconnectEx[^] for disconnecting a socket and reusing it (on XP+). Cheers, Mark

                "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

                F Offline
                F Offline
                FreeCastle
                wrote on last edited by
                #7

                Ah thank you a lot, the first link was very informative. That DisconnectEx function looks cute, but I think I'll avoid it because it only runs on XP or higher. I think I use shutdown, and if I understood that article right, I have also to "create" the socket (by a call to socket(...)) again, when I want to "reuse" it for a new connection.

                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