Managing Socket

Hi,

I have a scenario in which I have to connection to a third party using socket on a specific port. My question is how do we manage connection management for sockets. I mean features like Connection Pooling, Re-connecition on disconnection etc.

Because creating a connection each tmie whenever we need to contact 3rd party would be kind of overhead.

So is there any framework or design pattern for the same.

[435 byte] By [Shashank.Tilwallia] at [2007-11-27 1:34:12]
# 1
you can cross post this on the socket programming forum: http://forum.java.sun.com/forum.jspa?forumID=11
gmoma at 2007-7-12 0:41:20 > top of Java-index,Core,Core APIs...
# 2

Generally you try to do connection pooling at the client side, such that when you're finished with a connection you return it to a pool, and when you need a connection you first look in the pool for an existing connection to the same target. At the server, instead of writing your client handler like this:

read request

write response

close

you write it like this:

while ((read request doesn't produce an EOF)

write response

close

In this scenario the client always has to be prepared to deal with getting a possibly dead connection from the pool, in which case it has to open a new Socket and retry. Possibly the client will have a thread scanning the pool for entries that are getting too old. At the server there should always be a shortish read timeout, and if the timeout happens it should close the socket and exit the thread.

The above describes Java RMI's connection pooling pretty accurately.

ejpa at 2007-7-12 0:41:20 > top of Java-index,Core,Core APIs...