Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Socket connecting to a public ip-address

I need to make a server and client that connects to the server.

Problem: "the server works. the client can only connect to localhost, it cannot connect to a server on the internet. I want the client to connect to the server, via a public ip-address that the server is hosted on."

First of all, I have made sure that the port is forwarded and reachable i have tested the port, secondly i have disabled firewall completely from the server machine.

below is the test code i am using:

The Server: nothing fancy just simple - terminates if a client is connected, else just awaits a connection.

public class Server {
public static void main(String args[]) {
    try {
        ServerSocket srvr = new ServerSocket(52000);
        srvr.accept();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
  }
} 

The Client: I have used no-ip.com to mask the ip of the server to "biogenserver2.noip.me". Using .getCanonicalHostName(); will return the ip.

public class Client {
public static void main(String args[]) {
    try {
        String ip = Inet4Address.getByName("somets.noip.com").getCanonicalHostName();
        InetSocketAddress sa = new InetSocketAddress(ip, 52000);
        //Socket skt = new Socket("0.0.0.0", 52000); //local - this works fine.
        Socket skt = new Socket();
        skt.connect(sa);
    }
    catch(Exception e) {
        e.printStackTrace();
    }
  }
}

When i run this the server connects fine, but the client returns a "connection timeout" exception

Any help will be appreciated. Thanks.

like image 476
GabeTheApe Avatar asked Jan 19 '16 09:01

GabeTheApe


People also ask

What is difference between socket () and ServerSocket () class?

Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side.

Do Java sockets use TCP or UDP?

Yes, Socket and ServerSocket use TCP/IP. The package overview for the java.net package is explicit about this, but it's easy to overlook.

Does a socket have an IP address?

A socket consists of the IP address of a system and the port number of a program within the system. The IP address corresponds to the system and the port number corresponds to the program where the data needs to be sent: Sockets can be classified into three categories: stream, datagram, and raw socket.


1 Answers

Answer:

"Just for clarity: You have checked the port is open via public IP as returned by no-ip and the server will quit without exception when you run that little testclient (on a machine that is not the server machine) - is that correct?"Fildor

TL:DR

Don't run the client and server on the same machine and the same network trying to connect to your server through your public ip then to your own local network will result in a client timeout exception

I was running the client and server on the same machine and also the same network. This caused the client timeout exception. I tried running the Client on a different machine and a different network and i was able to connect successfully.

like image 84
GabeTheApe Avatar answered Oct 04 '22 06:10

GabeTheApe