File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes how long to keep socket? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how long to keep socket?" Watch "how long to keep socket?" New topic

how long to keep socket?

Paul Rhoades

Joined: Aug 01, 2002
Posts: 20
If i have two servers that are continuously running and sending messages to each other should I:

a) for each message to be sent : create socket, get output stream, send message, close stream, discard socket?
b) continuously maintain sockets and streams between servers?

Basically, is there any issues with keeping hold of a socket for as long as the servers are running, or should I be reconnecting regularly?
Warren Dew
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
I would just keep one socket around. Sometimes when there is network congestion, an open socket pair will stay functional even when new socket pairs cannot be connected.

However, brief router outages can cause loss of connection, so even if you just use an open socket, you have to think about what to do if it goes down. Typically, you would just reopen the connection, and possibly resend any failed messages.
I agree. Here's the link:
subject: how long to keep socket?
It's not a secret anymore!