1

I am trying to do simple ping operation using java code , i know that java does not support icmp so i just made a simple code that uses port 7 to send a request and wait for echo response

 Socket t = new Socket("127.0.0.1", 7);
    DataInputStream dis = new DataInputStream(t.getInputStream());
    PrintStream ps = new PrintStream(t.getOutputStream());
    ps.println("Hello");
    String str = dis.readUTF();
    if (str.equals("Hello"))
      System.out.println("Alive!");
    else
      System.out.println("Dead");
    t.close();

but this gives me

Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at MobileCloud.Ping.main(Ping.java:11)

I read that i should change in the /etc/inetd.conf file but I have no such file .

1 Answers1

1

Exception in thread "main" java.net.ConnectException: Connection refused

There is no server running on port 7 as you might verify with telnet 127.0.0.1 7. Thus the connection gets refused.

I read that i should change in the /etc/inetd.conf file but I have no such file

This depends on the system. You might need to install inetd. With your Ubuntu 14.04 this will be the inetutils-inetd package.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172