I am trying to create a Java chat application for my network class. I'm currently stuck trying to connect to someone behind another router. Now I have a project, I have a client program and a server program. Client programs are first registered in the server program, which registers its IP address and port in the database, and then the server returns them to the list of its friends with their IP addresses and ports. The client then closes the connection to the server and tries to connect to another client using the information sent by the server. So far, my program works only with connecting to the server and getting friends of IP and port, but when I use these values to connect to another client, I can’t 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 that is used to connect to the server, and below is what I do on the client side.
ss = new ServerSocket(reusePort);
So now I am technically listening to the same port that I used to connect to the server from which you are logged in and retrieved by 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 any protocol or something that I should do. I looked at the punching of TCP and UDP holes, but I'm not sure how this is actually done or how to implement it.
Any suggestions would be appreciated.
source
share