This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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?
Similar Threads
Java client and C server socket
Two simpe methods calling each other not working as expected
Catching Socket Exceptions
Urgent help on Chat Program
Handle large process by Maintain status in presentation