wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Forward Socket to another server socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Forward Socket to another server socket" Watch "Forward Socket to another server socket" New topic
Author

Forward Socket to another server socket

Kean Keat Heng
Greenhorn

Joined: Aug 10, 2008
Posts: 2
Hi,
Description
I have a server that is running and had multiple client connect directly to it by pointing to a specific IP and port number via socket.
The server socket as the listening side and the client uses normal socket.
The server is kinda like a application server that handles my home-brew RPC. Gives the server a message(string), the server runs it, return whatever result back to the client.

What leads to the problem
Now i am thinking of opening another server as a mean of load balancing. However, i do not want to change the coding in client that specifies the connection detail(ip, port)

So i decided to write a proxy server that stands in the previous server ip and port and will "forward/divert" the socket connection to the less busy server.

I had some leads of solution in mind:
Firstly, the message sent by the client have to be read by the proxy server to determine which server it should go to. This part i have no problem since the request wouldn't be as heavy

The Problem:
The major problem is if i send the request out using the proxy , the server will response to the proxy, and than the proxy will response to the client directly.
What i want to achieve here is that i wan the proxy to only divert the request and do not do any respond by itself. Which means that each server will get the request from proxy, but will send back the request directly to the client, not the proxy.

Tried (and abandoned) Method
I tried to another server socket at the client to listen and include the listening port and ip of the client and made the server to response to the server socket opened at the client, but it is inefficient and incurred too much overhead since 2 extra socket needs to be opened and threads in java doesn't guarantee the server socket in client will start listening when the server has done processing.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there and welcome to Javaranch!

Originally posted by Kean Keat Heng:

Tried (and abandoned) Method
I tried to another server socket at the client to listen and include the listening port and ip of the client and made the server to response to the server socket opened at the client, but it is inefficient and incurred too much overhead since 2 extra socket needs to be opened and threads in java doesn't guarantee the server socket in client will start listening when the server has done processing.


I'm not clear on the issue here? Instinctively this approach appears to be the correct one. Opening a couple of extra sockets should be no big deal (unless performance is a major issue in your app?). I'm also not sure what you mean by that the client won't listen until the server is finished? I would have thought that:

client --> proxy (passing in client details) --> server (do some processing) --> client would be OK...


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Kean Keat Heng
Greenhorn

Joined: Aug 10, 2008
Posts: 2
hi there,
Thanks for your insights.
I'm know sometimes I am too picky on details, a habit I got from my mentor.
Still trying to forward the request directly to the server.
Now reading on socket channels, doesn't seems like the solution though.

I abandoned the method before because it get too hard to managed the sockets connection. Still a newbie on sockets. I used thread to open the server socket on the client b4 the request has been sent out, sometimes it did open b4 the request, but sometimes it didn't. which is a problem to me since the server would hit a "connection refused" exception.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Originally posted by Kean Keat Heng:
hi there,
Thanks for your insights.
I'm know sometimes I am too picky on details, a habit I got from my mentor.
Still trying to forward the request directly to the server.
Now reading on socket channels, doesn't seems like the solution though.

I abandoned the method before because it get too hard to managed the sockets connection. Still a newbie on sockets. I used thread to open the server socket on the client b4 the request has been sent out, sometimes it did open b4 the request, but sometimes it didn't. which is a problem to me since the server would hit a "connection refused" exception.


I think what you are trying to do should still work . You just need to have a little bit more handling in your code for delays in threads/socket I/O (remember you are handing over to the native application server/OS to do that actual work and sometimes they don't behave in a timely fashion).

So open the socket on the client but in your other 'request' thread first of all check that the client's socket has been opened, if it has then go ahead and send the request (obviously put in timeouts etc in case of extreme failure), so you can (pseduo code):



Hope that helps!
 
 
subject: Forward Socket to another server socket
 
Similar Threads
Question about ServerSocket port number and Socket port number
TCP/Socket mechanics of "Accept()" etc.
Chat Application
socket programming on 2 ports
Custom Proxy server handling both http and https?