Socket question: When does a client connect to a server?
-
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)?
-
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)?
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."
-
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."
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?
-
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?
-
FreeCastle wrote:
I think there doesn't exist a command like disconnect()
They hide that information in the documentation[^] :rolleyes:
led mike
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.
-
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.
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."
-
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."
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.