Network Programming: to maintain sockets or not?

后端 未结 7 1107
耶瑟儿~
耶瑟儿~ 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 13:56

    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?

提交回复
热议问题