• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forward Socket to another server socket

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Kean Keat Heng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic