• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Socket connection best practice

 
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.
 
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
 
Author and all-around good cowpoke
Posts: 13078
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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic