aspose file tools*
The moose likes Java in General and the fly likes Socket: redirect connection to another server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Socket: redirect connection to another server" Watch "Socket: redirect connection to another server" New topic
Author

Socket: redirect connection to another server

surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

I have 3 servers which all run JVM.
1'st server opens connection to the 2'nd server, and the 2'nd server 'redirects' that connection to 3'rd server.
Is it possible?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It depends on exactly what you mean by "redirects" and what your overall requirements are.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

1'st server is a client, 2'nd - application that balances the load, 3'rd - actual workers.
What's preferred implementation:
1) #2 accepts connection from #1, receives byte[], stores it, keeps the connection opened, opens new connection to #3, sends byte[] to #3, closes connection, #3 do work, opens connection to #2, sends response byte[], #2 receives byte[], gets existing socket connection to #1, sends response to #1.
OR
2) #2 accepts connection from #1 and 'somehow' extend (prolong) the same connection to #3?

Will the scenario 1 work? How to extend (prolong) connection so it will look like #1 connects directly to #3?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Is there some particular reason you want to do this with raw socket connections instead of a highler level API such as Java Message Service? After all, Sun's motto "the network is the computer" shows the designers of Java had networking in mind from the beginning.

What is the client software that makes a request to this system?

Bill
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

William Brogden wrote:...raw socket connections instead of a highler level API such as Java Message Service?

Let's suppose that #1 and #2 communicate via custom protocol which can't be modified, and it's implemented on top of raw sockets.
Anyway it's not related to the problem: can I "prolong" socket connection somehow or should I open new connection to #3?
If you would like to go more practical, how load balancers are implemented: client connects via HTTP and waits for a response from balancer, balancer redirects the connection to another server. How it redirects the connection, opens new one?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

surlac surlacovich wrote:
William Brogden wrote:...raw socket connections instead of a highler level API such as Java Message Service?

Let's suppose that #1 and #2 communicate via custom protocol which can't be modified, and it's implemented on top of raw sockets.
Anyway it's not related to the problem:


Yes, it is.

can I "prolong" socket connection somehow or should I open new connection to #3?


I don't know what you mean by "prolong", but if I understand your requirement (which I really don't, since you've not been very forthcoming), the two main options are:

1) When #2 answers the connection request, it also opens a socket to #3. Everything that #2 reads from #1 it writes to #3, and everything that #3 send back, #2 forward on to #1.

2) Just use an actual load balancer.

If you would like to go more practical, how load balancers are implemented: client connects via HTTP and waits for a response from balancer, balancer redirects the connection to another server. How it redirects the connection, opens new one?


You're talking about two different things here.

A) Load balancers are transparent to the client. I sent a request to IP-1. As far as I know, IP-1 is the address of the server I want to hit, but actually it's just a load balancer. That load balancer forwards my traffic on to either IP-2 or IP-3, basically as described in my #1 above, taking turns between them based on whatever algorithm it's using. This happens without the load balancer knowing or caring about the higher level protocol, and without the client ever knowing about IP-2 or IP-3. http://en.wikipedia.org/wiki/Load_balancer

B) The HTTP protocol has a redirect mechanism built into it. An HTTP client sends a request for a particular URL. The server serving up that URL responds with a redirect telling the client that the resource it wants can actually be found at some other URL. This is done for reasons other than load balancing. http://en.wikipedia.org/wiki/HTTP_redirect


This is why people are asking you for more details about your requirements. Depending on what you're actually trying to do, the answer may be very different.
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

Jeff Verdegan wrote:
A) Load balancers are transparent to the client. I sent a request to IP-1. As far as I know, IP-1 is the address of the server I want to hit, but actually it's just a load balancer. That load balancer forwards my traffic on to either IP-2 or IP-3, basically as described in my #1 above, taking turns between them based on whatever algorithm it's using. This happens without the load balancer knowing or caring about the higher level protocol, and without the client ever knowing about IP-2 or IP-3. http://en.wikipedia.org/wiki/Load_balancer

Jeff, I would like to thank you for a very informative answer.
It looks like if I want to build custom protocol and balance the load from it it would be better to just open new connection to actual worker and keep connection to the client opened until the worker is done.
I've tried to make the problem abstract and try to figure out my self (good practice in coderanch).
By "prolong" I meant if the new connection can be opened automagically on some lower level (Transport, Internet layer). I see now the answer is no.
Thanks for info about HTTP-specific routing capabilities.
Is there any good opensource loadbalancers where I can see how this part is implemented?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket: redirect connection to another server