Reusing a socket.

Started by
8 comments, last by Tancor 21 years, 11 months ago
Ok, simple question. Let''s say I have a client program that will talk to the server. The connection is a TCP stream connection (can''t be changed). On my client, I create my socket. I get input I connect to the server via the socket. I send my data I receive my response I close the connection utilizing close(sock) Can I re-use the socket, or do I need to create another one, or is there a way to keep the socket and re-connect. In this example, the client doesn''t always remain connected to the server, it only connects to the server to send and receive queries, and can only do 1 query per connection, I don''t want to have to re-create a socket each time I want to talk to the server. Basically I want to re-use a socket without having to re-request it. So, can I do this or no? If I can, do I still use what I''ve used before to close the socket, or do I need to change anything, or can I re-connect to the server with this specific socket?
Advertisement
Don't close your connection, and make sure the server doesn't close the connection. I am assuming you are also programming the server. If this is the case, agree on some protocol to establish that one or the other is done. As it turns out, shutdown() and close() is adequate.

You might want to have your server and client NOT close the connection for some period of time (a few seconds, maybe a few minutes) and if nothing else happens, then one or the other can opt to close it.

I'm relatively new to sockets, so my advice isn't the best, and it depends on the load.

You need to establish communication protocols, error monitoring, and make a decision how long you want to keep the conneciton open for, who decides to close it (possibly either), and procedures for reopening if one or the other closed it without the other being done.

[edited by - bishop_pass on May 6, 2002 12:30:04 AM]
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
Why the heck would you close the connection after every query? The whole idea of connections is that they''re long term.
Well, Webservers close when they''re done dishing out the Webpage. Sure, they could keep the connection open in case the client clicks a link requesting another page from the same site, but such a policy probably wouldn''t be a good idea.

It depends on the situation. A game server would likely want to maintain a continuous connection to its clients.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
This isn''t a choice, I don''t have control over the server. You might consider it similar to a web client type of thing. The client opens a socket to the server, gets the data, the socket is closed.

My question is do I have to go thru the:

(sock = socket(AF_INET, SOCK_STREAM, 0))

each time I want to connect to the server after the previous connection was closed. I''d prefer to not have to create a socket for each time I want to make a query if possible, is there a way?

And yes, the connection is closed by the server after the data is sent.

Wouldn''t it be feasible to hold onto a socket that has already been allocated by the system though - I mean - first the socket is allocated using the command above, then the socket is asked to connect to a remote location. Once the connection is closed on that socket, is there a way to keep my socket so I don''t have to go thru the request each time?
ps - if I were programming the server, I''d either do a udp request port, or I would make a lingering connection to the server, but I don''t have that luxury
The functionality you are looking for is available on Windows XP or higher. Lookup DisconnectEx in the MSDN Library or Platform SDK.

Regards,



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
This isn''t on Windows. I''m sorry, I forgot to mention that earlier. This is actually on linux.
quote:Original post by bishop_pass
Well, Webservers close when they''re done dishing out the Webpage. Sure, they could keep the connection open in case the client clicks a link requesting another page from the same site, but such a policy probably wouldn''t be a good idea.


Some webservers do have the ability to keep connections open for multiple requests. See the ''Keep-Alive'' header of HTTP. It''s a very good idea when you''re dealing with frames or image requests, or some kinds of re-directs.

Why don''t you just try and see what happens when you call connect() with a socket you have previously closed ?

If it works, then it works. If it doesn''t work, then it doesn''t work.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement