Java nat traversal for chat application
Asked Answered
L

2

6

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.

Lumbricalis answered 10/4, 2013 at 0:2 Comment(3)
I see you have accepted an answer, but it seems you want to look into "Hole Puching". The accepted answer does not really answer your question but instead tells you to let all the information of your application to go through your Server, and it seems like you want to directly connect your clients.Vanish
hey did you find your answer . please help me. i am having the same problem.Jolee
Possible duplicate of STUN, TURN, ICE library for JavaVendible
W
2

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.

Wivinia answered 10/4, 2013 at 0:6 Comment(3)
Thanks tigger! I do as of now have my server port forwarded to X behind my router, I am thinking of switching to the second architecture you suggested (thanks for the link, it can be hard to sift though google sometimes lol) and having each client maintain a single connection to the server and the server then forwards anything to them through it. However, I am going to mess around a bit more with what I have then switch as I run out of time. Thanks for the help.Lumbricalis
ok so you are telling that as we cant go to each and every router , we pass through, and do the port forwarding . so we should use a server and let it do the job. (please correct me if i am wrong). so what exactly will the server does ? please pardon me if my question is odd, i am new to networking.Jolee
@Lumbricalis Is that how torrent clients work? Or do they use some other way to achieve this? Is there an easier way to implement this? (like a library)Silin
V
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

Vendible answered 19/4, 2017 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.