from some books, i learn that after sending the data,the socket port should be closed,but consider my scinario:
my project is consisted of two subsystem,let me call them:sysA and sysB,they communicate with each other through socket,both of them open a server socket ,the port is special and dedicated to each other,no other client use it.
under such situation,i think keeping the socket connection opening is a good solution,it is not necessay to close it at end of each communication and reopen it at the beginning of next communication,because the opening and closing the socket frequently will make the performance a bit lower.
what is your opinion?
The ssh service daemon(sshd) make use of this aproach. When both server and client wants to disconnect, the server sends a exit status to the client and both sides exit. Maybe you can compare your system needs with the ssh aproach.
In this case, you want your two subsystems to be always connected, and the socket connection is dedicated to each other.
Your solution is the best one - leaving the socket always open. Creating and tearing down socket connections can be very expensive operations - this is why a lot of Web servers use HTTP Keepalive to improve the performance of an HTTP session.
Once you initially make the socket connection between the two subsystems, you don't really incur an extra performance penalty in terms of CPU cycles or speed. Your resource usage is minimal (a socket descriptor on each subsystem, input/output streams) - only a handful of objects.
Another possible option to squeeze some better performance (it may or may not be noticeable) is to use UDP instead of TCP. A TCP connection has some extra overhead (acknowledgement packets, retransmission of dropped packets ,etc.) The drawback with UDP, of course, is that the delivery of packets is not guaranteed. Is the socket connection between the two subsystems for streaming of some kind of live data? In this case, UDP is a good option because who cares if we miss a few packets here and there, there are always updates coming down the wire.
Of course, if it's more of a command-response type connection, UDP isn't so good because you probably want to ensure each command and response gets to its destination.
But the bottom line is: If this socket connection is dedicated for the two subsystems, leaving it always open is a better idea than constantly opening and closing.
Hope this helps! Let me know if you want me to clarify anything.