Network programming: support sockets or not?

I am currently translating the API from C # to Java, which has a network component.

The C # version seems to keep input and output streams and the socket open while using its classes.

Is it correct?

Given that the application sends commands and receives events based on user input, is it more reasonable to open a new socket stream for each "message"?

I support ServerSocket to listen on the server, throwing events, but I'm not sure if Socket and output stream support for outgoing messages is such a good idea.

I'm not used to programming socket. Like many developers, I usually work at the application level, when I need to do network interactions, and not at the socket level, and 5 or 6 years have passed since I did this at the university.

Cheers for the help. I think this is asking for more advice than for a definitive answer.

+7
java network-programming
source share
7 answers

There is a difference between the cost of maintaining open connections and the cost of creating these connections.

Creating connections takes time and bandwidth. You have to do a three-way TCP handshake, start a new server thread, ...

Maintaining open connections is mainly related to memory and connections. Network connections are a resource limited by the OS. If you have too many connected clients, your available connections may end. This will cost memory since you will have one thread open for each connection, with its associated state.

The correct balance will vary depending on the expected use. If you have many clients connecting in a short period of time, it will probably be more efficient to close connections. If you have few clients connecting over an extended period of time, you should probably support open connections ...

+14
source share

If you have only one socket on the client and on the server, you should keep it open for as long as possible.

+3
source share

If your application and the server with which it is negotiating are close, over the network, it MAY be reasonable to close the connection, but if they are far away over the network, you are probably better off allowing the socket to live for a duration.

Guillaume mentioned a three-way handshake, and this basically means that opening a socket will take at least 3 times the shortest packet transit time. This can be approximated by “half circular conversation” and can easily reach 60-100 ms over long distances. If you finish with an additional wait of 300 ms, for each command will this affect the user's work?

Personally, I left the socket open, it would be simpler and would not be worth the time for each “need to send something” instance, the relative cost is small (one file descriptor, a memory bit for data structures in user space and some additional storage in the kernel).

+3
source share

It depends on how often you expect the user to enter commands. If this happens quite rarely, you can probably close the sockets. If frequent, creating sockets many times can be an expensive operation.

Now, having said that, how expensive, in terms of machine resources, does it have a socket connection for infrequent data? Why do you think that “Socket and output stream support for outgoing messages is not such a good idea” (although this seems to be correct)? On the other hand, this is different for file streams if you expect other processes to want to use the same file. A quick shutdown of the file stream would be correct in this case.

How likely is it that you intend to exhaust many of the TCP connections that you can create, what other processes that can use outbound connections? Or do you expect that a large number of clients will be connected simultaneously with your server?

+2
source share

You can also watch DatagramSocket and DatagramPacket. The advantage is the lower head, the disadvantage is the head, which provides a regular socket.

0
source share

I suggest you use an existing messaging solution such as ActiveMQ or Netty. This will cope with the many problems that may arise during messaging.

0
source share

I'm a little late, but I have not seen anyone offer this.

I think it would be wise to consider the possibility of combining your connections (it does not matter if Socket or TCP), the ability to support open connection pairs and quickly use them in your code base would be optimal in case of performance.

In fact, the Roslyn compiler makes extensive use of this technique in many places. https://github.com/dotnet/roslyn/search?l=C%23&q=pooled&type=&utf8=%E2%9C%93

0
source share

All Articles