Java nat traversal for chat application - java

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

Related

Public to Local IP Communication

I have created project which has two components as Desktop client and web.
web server is communicating to the my another program which i run as client on other systems.
After connecting those clients communication is happening in network as all IP's within network are reachable.
However, when i deployed web app on public IP now i am not able to connect the clients as the local IP's of those clients are not reachable by server.
How can i achieve this communication between local IP to public and vice versa?
There are multiple ways to achieve this.
Anyways, if you want the service to be reachable publically then you´ll probalby want to forward the Port to the machine running the service.
Also, make sure the Firewall allows connections to this port.
Since you´re talking about Web-Apps it´s probably HTTP, Port 80 TCP, or HTTPS, the encrypted version of HTTP running on port 443 TCP.
To explain it, your ISP gives you one public IP address.
Since you probably have multiple devices using internet, they all appear in the internet as the one IP address your provider gave you.
Whenever you send something out your router will remember where you tried to connect and if a response comes in your router knows which device to send the response to.
Now, since you want someone to connect to you, there was no request so your router does not know where to put the packet and simply blocks it.
In most routers you can configure something usually called NAT or Port Forwarding. You simply specify that communication on Port 80 or 443 should be routed to the internal IP. It has one of the following formats:
192.168.0.1 - 192.168.255.254
172.16.XXX.XXX - 172.31.255.254
10.0.0.0 – 10.255.255.254

Open socket server to clients outside of localhost

TLDR: Allow people not on localhost connect to my chat server if they have the server IP.
I am starting to learn about socket programming in Java and I have come across an issue that I can't seem to figure out. I have created a basic chat program with a server and a client using sockets. This works perfectly in localhost, but I want to share it with my friends. I have searched for hours and can't really find a solution. From what I've read I need to bind my ServerSocket with my IP or Port forward. Just looking for some clarification and possible steps I could take.
How I'm starting the server
ServerSocket server = new ServerSocket(5056);
How I'm connecting to the server
Scanner readIp = new Scanner(System.in);
String ip = readIp.nextLine();
socket = new Socket(InetAddress.getByName(ip), Integer.parseInt("5056"));
Posts I've looked at
Java Socket port forwarding
https://coderanch.com/t/571967/java/connect-machine-localhost
What does it mean to bind() a socket to any address other than localhost?
To 'Allow people not on localhost connect to my chat server if they have the server IP.' and outside of your LAN:
public 'white' external IP.
So for example this page https://whatismyipaddress.com/ should display exactly same IP address as displayed in your OS
non blocked incoming connections on ISP level: your ISP should allow incoming connections to your computer from internet.
some opened port, not blocked by local firewall or by ISP.
Typically all ports below 1024 are blocked for input on ISP level.
No presence of NAT ( exclusions are present but outside of this topic ), thats common for WiFi/3/4/5g home routers.
As you may already understood - thats complicated.
So faster will be to use an external service,like https://portmap.io/ :
it does VPN connection + port forwarding, so knocking on their server from internet will actually call your local chat server.

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.

Network discovery in Java using multicasting

I'm trying to make a client/server Java App. Both client and server will be running on the same wi-fi network. Server will be running on a specific port that client is aware of.
I am planning to send a multicast message from client through the network for that specific port to discover the server. However, I'm not too sure how I can find out which IP in my network received my message.
Do I need to create a socket on the client and listen to incoming packets once I send my multicast message in case server replies back?
Thanks in advance.
(1)server listens on a pre-arranged port
DatagramSocket s = new DatagramSocket(8888);
s.receive //(1)
s.send //(2)
(3)client sends a message to the port, on the broadcast IP, 255.255.255.255
DatagramSocket c = new DatagramSocket();
c.send(255.255.255.255:8888,msg) //(3)
c.receive //(4)
the client binds to a port too. we didn't specify it, so it's random chosen for us.
(3) will broadcast the message to all local machines, server at (1) receives message, with the client IP:port.
(2) server sends response message to client IP:port
(4) client gets the reponse message from server.
I would strongly recommend using JGroups. It has a lot of features and it will do all the UDP stuff. JBoss uses it for their clustering.
You can try using java.net.MulticastSocket (available since Java 1.1). If you don't need the rich feature sets of libs like jgroups, hazelcast etc. that plain Java API might serve you well enough.
See also example pages here and here.
You could try using SSDP. It's what UPnP devices use to discover each other. It's multicast on port 1900 and just uses really simple packets to send around IPs and service information.
Cling is a UPnP lib you can pull from. Note I'm not recommending you move to UPnP - just the discovery protocol used.

Categories

Resources