Network Programming: to maintain sockets or not?

There is a trade off between the cost of keeping the connections open and the cost of creating those connections Creating connections costs time and bandwidth. You have to do the 3-way TCP handshake, launch a new server thread Keeping connections open costs mainly memory and connections. Network connections are a resource limited by the OS.

If you have too many clients connected, you might run out of available connections. It will cost memory as you will have one thread open for each connection, with its associated state The right balanced will be different based on the usage you expect. If you have a lot of clients connecting for short period of times, it's probably gonna be more efficient to close the connections.

If you have few clients connecting for long period of time, you should probably keep the connections open.

There is a trade off between the cost of keeping the connections open and the cost of creating those connections. Creating connections costs time and bandwidth. You have to do the 3-way TCP handshake, launch a new server thread, ... Keeping connections open costs mainly memory and connections.

Network connections are a resource limited by the OS. If you have too many clients connected, you might run out of available connections. It will cost memory as you will have one thread open for each connection, with its associated state.

The right balanced will be different based on the usage you expect. If you have a lot of clients connecting for short period of times, it's probably gonna be more efficient to close the connections. If you have few clients connecting for long period of time, you should probably keep the connections open ...

If you've only got a single socket on the client and the server, you should keep it open for as long as possible.

If your application and the server it talks to are close, network-wise, it MAY be sensible to close the connection, but if they're distant, network-wise, you are probably better off letting the socket live for the duration. Guillaume mentioned the 3-way handshake and that basically means that opening a socket will take a minimum of 3 times the shortest packet transit time. That can be approximated by "half the ping round-trip" and can easily reach 60-100 ms for long distances.

If you end up with an additional 300 ms wait, for each command, will that impact the user experience? Personally, I would leave the socket open, it's easier and doesn't cost time for every instance of "need to send something", the relative cost is small (one file descriptor, a bit of memory for the data structures in user-space and some extra storage in the kernel).

It depends on how frequent you expect the user to type in commands. If it happens quite infrequently, you could perhaps close the sockets. If frequent, creating sockets repeatedly can be an expensive operation.

Now having said that, how expensive, in terms of machine resources, is it to have a socket connection open for infrequent data? Why exactly do you think that "maintaining a Socket and output stream for outbound comms is not such a good idea" (even though it seems the right thing to do)? On the other hand, this is different for file streams if you expect that other processes might want to use the same file.

Closing the file stream quickly in this case would be the way to go. How likely is it that you are going to run out of the many TCP connections you can create, which other processes making outbound connections might want to use? Or do you expect to have a large number of clients connecting to your server at a time?

You'll run out of connections long before you run out of port numbers. – paxdiablo Feb 6 '09 at 11:01 one server one client So not going to run out of connections. Port is specified as the API is a wrapper around a TCP based protocol.

– Omar Kooheji Feb 6 '09 at 11:03 My bad, I guess I've forgotten my network programming as well – Mystic Feb 6 '09 at 11:26.

You can also look at DatagramSocket and DatagramPacket. The advantage is lower over-head, the disadvantage is the over-head that regular Socket provides.

I don't have the option of changing protocol, I'm connecting to a vendor application. – Omar Kooheji Feb 6 '09 at 16:15.

I suggest you look at using an existing messaging solution like ActiveMQ or Netty. This will handle lot of the issues you may find with messaging.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions