Hi, I am wondering if it is possible to "transfer" a Socket connection to another ServerSocket?

So, say I listen for a connection on ServerSocket with port 1111. A connection is established, I know want to use a ServerSocket with port 2222 to handle this connection. Is he possible without listening for a new connection on that port?

Can you explain What do you want to do when the ServerSocket's accept() method returns a Socket with a connection? Normally you would pass that Socket to a class and method to handle it. Why can't you do that?

Yes I will pass it to a class to handle it but first I would like the connection to switch to another serversocket meaning the first serversocket is now free to accept another request very fast if you undertsand it

My guess is that you cannot transfer an open connection from one IP/port to another while keeping it open.
Why do you want to do this? Maybe there's another way...

I would like the connection to switch to another

That is what I suggested as being the normal way to do it. When accept returns with a connection, the socket is "switched" to a class and method on its own thread to handle and the program loops back immediately to wait for another connection on that port by using accept.

You can have one ServerSocket that sits waiting in a loop accepting connections and passing each one to a new Thread that handles ongoing communication with that client. There's no need to change to a new port to do that.. Ie you can have one ServerSocket and multiple Sockets on a single port.

Ah well, the reason I wanted this was for load-balance, so connection goes to first free ServerSocket. So if I have load of http request at one time it can transfer connection to a free server which will then handle request.

So how many server computers are you talking about? If you want to hand the connection off to a different computer in a server farm, then sorry, I don't know how to do that. If it's two threads on the same machine then there's no advantage in changing ports.

Ah ok, thanks you for info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.