Archived

This topic is now archived and is closed to further replies.

Tancor

Reusing a socket.

Recommended Posts

Tancor    122
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?

Share this post


Link to post
Share on other sites
bishop_pass    109
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]

Share this post


Link to post
Share on other sites
Sneftel    1788
Why the heck would you close the connection after every query? The whole idea of connections is that they''re long term.

Share this post


Link to post
Share on other sites
bishop_pass    109
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.

Share this post


Link to post
Share on other sites
Tancor    122
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?

Share this post


Link to post
Share on other sites
Tancor    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Fruny    1658
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...

Share this post


Link to post
Share on other sites