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
5
  • @ScaryWombat yes the ports or initialised as 9876 on both the client and server Commented Mar 19, 2019 at 1:16
  • @Joni I gwt an error at Socket s = new Socket(ip, 9876); : java.net.ConnectException: Connection timed out: connect Commented Mar 19, 2019 at 1:27
  • are you trying in an enterprise network? The firewall may block for non-standard port access. Commented Mar 19, 2019 at 1:53
  • @javapedia.net I don't have the knowledge to tell you that, is there any way I can find out Commented Mar 19, 2019 at 1: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: linuxtechi.com/nc-ncat-command-examples-linux-systems Commented Mar 19, 2019 at 2:01

1 Answer 1

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 });
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your response, I found this out from another question. Now I'm getting a connection timeout
@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.
Debugging techniques worth more than gold these days :)
@AlexT Not really. Ping can only test icmp. Not your socket. If you don't have telnet, you can also debug with netcat (which is more sophisticated).
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.