Which is the best method for sending a data using sockets:
Method 1: Creating a new socket every time when data needs to be sent and closing it when the transfer is complete.
Method 2: Using the same socket instead of creating a new socket and maintaining the connection even when waiting for new data.
It depends on the kind of socket, but in the usual cases it is better to keep the socket unless you have very limited resources.
In summary: If you are using TCP you almost always better keep the socket open and close it only if you lack the necessary resources to keep it open. A good compromise is to close the socket as long as you have enough activity on the socket. This is the approach usually done with HTTP persistent connections.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With