Network Programming: to maintain sockets or not?

后端 未结 7 1106
耶瑟儿~
耶瑟儿~ 2021-01-12 13:09

I\'m currently translating an API from C# to Java which has a network component.

The C# version seems to keep the input and output streams and the socket open for th

相关标签:
7条回答
  • 2021-01-12 14:00

    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).

    0 讨论(0)
提交回复
热议问题