This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I'm writing a client / server application which depends on socket connections to communicate.
What I'd like to know is should I keep the same socket connection open and continually reuse it until the client closes the connection or should I be closing the connection after a task is complete and create a new socket connection to service the next task.
The best practice in socket programming is that to use same socket connection for a connected client. Once the client operation is done and no need to proceed with that client then close the socket connection for that client. Eventually the next client will be connected and Server will proceed with the new one. But if your Server is handing multiple client then you need to create a Thread for the clients which will handle every clients seperately so that no socket connection will be miss or lost.
Also, be very very very sure that sockets get closed when you are finished. A socket ties up operating system resources which will eventually be exhausted if you don't, causing mysterious system crashes.