0

I'm trying to write a simply program client-server program that would connect a Client machine to a Server machine.

My code so far works well on the localhost, but when I replace the ip address in the Client code to the local ip address of the Server machine no connection is done. I think my understanding of InetAddress is off.

Socket connect code: Client.java

InetAddress ip = InetAddress.getByName("my_ip_address");
Socket s = new Socket(ip, 9876); //<- where the connection timeout happens
AlexT
  • 589
  • 2
  • 9
  • 23
  • @ScaryWombat yes the ports or initialised as 9876 on both the client and server – AlexT Mar 19 '19 at 01:16
  • @Joni I gwt an error at `Socket s = new Socket(ip, 9876);` : `java.net.ConnectException: Connection timed out: connect` – AlexT Mar 19 '19 at 01:27
  • are you trying in an enterprise network? The firewall may block for non-standard port access. – javapedia.net Mar 19 '19 at 01:53
  • @javapedia.net I don't have the knowledge to tell you that, is there any way I can find out – AlexT Mar 19 '19 at 01:55
  • Is it linux client? Try nc command like this "ncat IP_address port_number". Try for stardard ports like 80 and then non-standard port. If standard ports work then possibly firewall issue for non-standard port. if this doesn't help, use machine-name instead. Did "ping ip" work? NCAT command reference: https://www.linuxtechi.com/nc-ncat-command-examples-linux-systems/ – javapedia.net Mar 19 '19 at 02:01

1 Answers1

5

You don't call getBytes() from a String to get your ip address like that; option 1: call getByName(String) like

InetAddress ip = InetAddress.getByName("127.0.0.1");

Option 2: construct the proper byte[]. Like,

InetAddress ip = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks for your response, I found this out from another question. Now I'm getting a connection timeout – AlexT Mar 19 '19 at 01:18
  • 1
    @AlexT General debugging techniques - Test locally with telnet; check firewall settings on machine acting as server; test with telnet from proposed client. You should be able to `telnet myserver 9876` and get a connection. – Elliott Frisch Mar 19 '19 at 01:21
  • Debugging techniques worth more than gold these days :) – nortontgueno Mar 19 '19 at 02:52
  • @AlexT Not really. Ping can only test icmp. Not your socket. If you don't have telnet, you can also debug with [netcat](http://netcat.sourceforge.net/) (which is more sophisticated). – Elliott Frisch Mar 19 '19 at 03:10
  • @ElliottFrisch well I tried running `telnet server_ip 9876` on the client computer and I get: `Could not open connection to the host, on port 9876: Connect failed` – AlexT Mar 19 '19 at 03:14
  • And what happened on the server computer? Assuming it works on the server, check your **firewall** settings. – Elliott Frisch Mar 19 '19 at 03:26
  • @ElliottFrisch I was running netcat on the server and nothing happens. And I've turned off my firewall... – AlexT Mar 19 '19 at 03:56
  • Are you sure you're running the server? Debug until something happens. Saying "nothing happens" doesn't add anything to the description of the problem. But if you can't connect locally to your socket, then your server process may be dead; you haven't posted any of that code. – Elliott Frisch Mar 19 '19 at 04:01