Reputation: 762
I'm working on a Java client-server program. I use a socket to make the connection but I'm not sure about the efficiency. So, this is my question: every time that I need a connection with a server, is it more efficient to create a new socket or to use the one created for the previous connection?
I think I need a method to verify if the client is already connected.
Upvotes: 1
Views: 299
Reputation: 3446
In terms of performance is more efficient re-using a previously created socket. But this don't need to be the best solution, it depends on each case.
The idiom case is having a ServerSocket
in the server side that returns a Socket
each time a new client connects to to it. If you require multiple clients to work against the server you could create a Thread
to treat each client passing it the returned Socket
.
Moreover, you don't need a method to verify you have a client already connected, ServerSocket
returns a new Socket
each time it happens.
Please, read more about this topic in Oracle tutorials.
Upvotes: 1