1
votes

I'm working on a ServerSocket with java, and I would like to know, upon a client connecting, is it possible to send the Client/Socket to another ServerSocket. For example a client connects to 123.41.67.817(Just a random IP) and upon connection, the client gets sent straight to for example 124.51.85.147(Another Random IP) with a port of course. So a little Map of what would happen.

ServerSocket(Listening for Connections)

Client ---> ServerSocket(Client connects)
ServerSocket -> Client(Server says: Hello, I am going to send you to 124.51.85.147)
Client -> ServerSocket(Client Says: OK!)
Client ---> ServerSocket(124.51.85.147)(Client gets sent to a different server Socket)
ServerSocket(124.51.85.147) -> Client(Server2 says: Welcome!)
and then the client stays on Server2(124.51.85.147)

Is this possible in any way. Sorry for the long question.

1
As Coffee suggests: Yes, the goal is possible. You can "redirect" a client to a different server, or you can have the client talk to multiple servers. You just can't do it on the same socket. As Stephen C says below: "a TCP/IP connection is a conversation between a pair of IP addresses." Look at the link Coffee gave you, and see if it's applicable to your scenario: stackoverflow.com/questions/8854675 - paulsm4
Ok thanks. I'll look at it soon. - sebagius7110
Basically, what you have to do is tell the client the new server's address and then have the client connect to the new server. Sort of like an HTTP redirect, except you have to code it yourself. - Chris Chambers

1 Answers

5
votes

Is this possible in any way.

No.

At the most basic level, a TCP/IP connection is a conversation between a pair of IP addresses. There is no provision in the TCP/IP protocol for changing one of the two IP addresses in mid conversation.

Even if it were possible at the Java level to (somehow) serialize the Socket object and send it to another program (local or remote), it would not be possible to change the IP addresses for the underlying conversation; see above.


Historical footnote: A long time ago (1980's) in a country far away (Cambridge UK) there was a network (The Cambridge Ring) whose stream protocol (BSP) implemented an operation known as "replug". If you had BSP connection between A & B and another between B & C, then B could replug the connections so that A talked directly to C. Reference: "The Cambridge Distributed Computer System" by R.M. Needham & A.J Herbert (Annex C).

I've never seen a replug operation elsewhere. Indeed, if you think about it, it requires a complicated 3-way handshake to implement a replug-like operation reliably. In the BSP case, they didn't do that ... at least according to the description in Needham & Herbert.