Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket connection best practice

 
Steven Bruton
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mir Mohammed Easin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.

Cheers
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
Steven Bruton
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the input guys, much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic