Load balancer, sockets and java - java

I've got a project to work on and I need to build some client - server applications where I can send messages and whatever, in Java. One goal of the project is to deal with failover. When a client is connect to a server and the server dies, it automatically connects to the backup server. What I want to do is not required but I want to implement a load balancer so multiple clients get connected to the prefered server.
The connection between a client and the server must be with TCP sockets.
This is a schematic of the network architecture:
client connects to the load balancer (udp or tcp, I don't know the
best one for this situation).
load balancer decides the which server should that client connect (the most empty one)
the client creates a TCP connection with the specified server
My question is:
How should the load balancer work to get a client to connect to specific server? Send information of the server (ip, port) to the client and the clients creates another socket with the new ip? (blue line).
Or is there a way for the load balancer to connect those two end points (server #1 <-> client #1), without having to send information to the client?
PS: I'm asking you because it seems unnecessary for each client to have to create 2 sockets to get connect to the server (first socket black line, second socket blue line)

i think after the client sends information to the load balancer,its best if the load balancer sends that information to the server which it decides instead of sending something back to client and client connecting again to server

Related

Seeing multiple servers on a network

I am busy with a project where I'm creating a basic client/server chat application which allows a user to create a server on their local network on a port of their choice and then have multiple clients connect to that server by specifying the IP and port number of the server(So far so good).
I would like to how I can have clients see all the possible servers they can connect to on their local network when there are multiple servers running on their local network over different ports and then allowing them to connect to one. I am using basic Java socket programming for this project.
You could have
use a UDP based protocol where each server publishes it's IP:port every second.
you could have a service where each server registers. You could chat with that to get the list of all services.
The nice approach of the later option is you can use one of your chat services for server discovery. When you want to get the list, you send a message to a channel on that server which all the servers are listening to and they respond with a chat message.

Socket communication between Gears on OpenShift

I have 2 DIY Gears running simple a Java Client-Server Application with Sockets.
What I want to do is sent a command via Socket to Gear1, which sends a command via Socket to Gear2, then sends back to the user whatever data Gear2 sent back.
The problem is that I can't connect to Gear2 from Gear1.
Gear1 address = 127.13.55.1:16000
Gear2 address = 127.7.21.129:16001
I forwarded port 16000 on Gear1 so that I can communicate with it from a local client. But when I try to connect to Gear2 from inside Gear1 I get the error message: java.net.NoRouteToHostException: No route to host.
Is there some configuration I forgot about?
The two gears will not be able to communicate with each other on those ports. Inter-gear communication is blocked on non standard ports, except in special cases with scaled applications. You would have to use port forwarding between the gears for this to work, the same as you do from your local machine.

Client to specific client communication through a server

I have successfully implemented a server -client application where i have maintained a list of clients connected to the server in an array.What i do not understand is how will the client tell the server about the client the message is sent for .Like if server A has 3 clients connected lets say a,b and c.Now if a wants to send a message to c how will it tell the server about it?
i want the client to send a request to server asking to connect to client c ,the server asks client c for permission and if c accepts a and c can send messages back and forth until one of them disconnects
For IM (Instant Messaging), you can look at XMPP Servers and clients, which are very popular in chat application, even gtalk works on them.
XMPP Server : jabber server.
The common practice is exchanging all the messages (in a given protocol) through a server and which intermediates everything. If you want direct communication between the clients, take a look at p2p or peer-to-peer (http://en.wikipedia.org/wiki/Peer-to-peer).
Your question is very similar to this one: Client-Server-Client communication using Sockets

Java nat traversal for chat application

I am trying to create a java chat application for my networking class. As of right now I am stuck trying to connect to someone behind a different router. The way I have my project right now is I have a client program and a server program. The client programs first logs into the server program which logs their IP and port in a database and then the server gives them back the list of their friends with their IPs and ports. Then the client closes down the connection to the server and tries to connect to another client using the information the server sent back. So far my program only works connecting to the server and getting the friends IP and port but when I use those values to connect to the other client I cant connect.
socket = new Socket();
socket.setReuseAddress(true);
socket.setKeepAlive(true);
socket.setSoLinger(true, 10);
socket.bind(new InetSocketAddress(Port));
socket.connect(new InetSocketAddress(host, SERVER_PORT));
reusePort = socket.getLocalPort();
Above is a snippet of java code used to connect to the server then below is what i do on the client side.
ss = new ServerSocket(reusePort);
So now technically I am listening on the same port I used to connect to the server with which is logged in and is retrievable to another client and is in the NAT table with my ip and port. I am not sure what I am missing or if there is some protocol or something that I have to do. I have looked at TCP and UDP hole punching but I am not sure how that is actually accomplished or how to implement it.
Any suggestions would be appreciated.
If you want to send a message you'll need to set up port forwarding on any device that acts as a server (any device which creates a socket server). Port forwarding is done on the Router. The reason you cannot connect to the other client is because they are hidden behind their routers firewall. Their address to the rest of the world is actually the address of the router, not of their physical computer. On their local network they have a different address then what the rest of the world sees, and the router figures out what messages from the outside world need to be sent to the client based on an address translation table.
Given your architecture, this would mean that all clients need to have their routers doing port forwarding, which is of course unfeasible (imagine gtalk or aim requiring users to do port forwarding).
The more common architecture is to have the Server do the work of rebroadcasting messages to the connected clients and maintain tables to lookup whose talking with who. This way there is a single server which will need a static ip (or be port forwarded), and all users are simply clients which connect to the server socket and read messages from it.
For actual code describing the second architecture please see http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html. Then the machine which is running the server code either needs a static ip or if it is behind a router needs traffic from the port it is listening on to be forwarded.
So on the server code you will bind to the ip assigned from your router (something like 192.168.1.2 at some port say 5000). Then go to your routers configuration page (it may be 192.168.1.1 see http://www.wikihow.com/Port-Forward/Open-Ports-on-a-Linksys-Router), and forward port 5000 to the address 192.168.1.2.
The Interactive Connectivity Establishment (ICE) protocol combines various NAT traversal utilities such as the STUN and TURN protocols in order to offer a powerful mechanism that allows Offer/Answer based protocols such as SIP and XMPP to traverse NATs.
This project provides a Java implementation of the ICE protocol that would be usable by both SIP and XMPP applications. The project also provides features such as socket sharing and support for Pseudo TCP.
ice4j is maintained by the Jitsi community.
ice4j

Server to server communication in java - load balancing

I have a chat application in java using nio it's working fine now. I want to split my chatserver into 2 with the same portno.
Simply :
I want to Split single my server into two or three. When client
logins, it first connect to the first server.
When the first server have 10 clients then 11 th client must
connected to the Second server. and same mechanism for third
server.
These three server must have same port no.
These server must have an internal communication. If client 1 is
logged into the server 1 . Any details got in server 1 should be
known by Server 2 and Server 3.
A client in server 1 must have to communicate with clients which
are in server 2 and server 3.
How can I make these possible is there any example related to this. I googled it but couldn't find any appropriate solutions.
please guide me to resolve these.
Thank you very much.
You can use load balancer which redirects request as per server instance health, you can expose server health parameter by no. of connections that server have. for inter server communication i recommend to use apache activemq and shared database is also good idea

Categories

Resources