0

I am trying to connect server by port and Host Name.
I find one program but when i am trying to run it will show following Exception

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
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.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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 sample.Echoclient2.main(Echoclient2.java:31)
Couldn't get I/O for the connection to: 127.0.0.1
Java Result: 1    

Here is my code which i use.

public class Echoclient2 {
public static void main(String[] args) throws IOException {

    String serverHostname = new String ("127.0.0.1");

    if (args.length > 0)
       serverHostname = args[0];
    System.out.println ("Attemping to connect to host " +
            serverHostname + " on port 10008.");



    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
        echoSocket = new Socket(serverHostname, 10008);
        System.out.println("server name"+Inet4Address.getByName(serverHostname));
        out = new PrintWriter(echoSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                                    echoSocket.getInputStream()));
         System.out.println("Connection accepted " +
            echoSocket.getInetAddress() + ":" +
            echoSocket.getPort());
    } catch (UnknownHostException e) {
        e.printStackTrace();
        System.err.println("Don't know about host: " + serverHostname);
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Couldn't get I/O for "
                           + "the connection to: " + serverHostname);
        System.exit(1);
    }

BufferedReader stdIn = new BufferedReader(
                               new InputStreamReader(System.in));
String userInput;

    System.out.println ("Type Message (\"Bye.\" to quit)");
while ((userInput = stdIn.readLine()) != null) 
       {
    out.println(userInput);

        // end loop
        if (userInput.equals("Bye."))
            break;

    System.out.println("echo: " + in.readLine());
   }

out.close();
in.close();
stdIn.close();
echoSocket.close();
}

}

What i need:
1. I need to send some message to server and want response from server. Suppose i send "I am user of stackoverflow" then server should give any response like return same String or convert in uppercase or something else.

Some Questions:
1. I write client Java File but whether i need to write server java file.
2. Can we send request by using ip and port.
3. Can we use host name.
4. Any echo server name? i need to send message to this server want to know response.
5. I try both server,java and client.java then i got result? is this solution for me.

phant0m
  • 16,595
  • 5
  • 50
  • 82
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

2 Answers2

3

1. I write client Java File but whether i need to write server java file.

Yes, you either need to write your own server (if the server should fulfill some unique requirements) or connect to an existing one. The message "connection refused" indicates that no server is running at the port you are trying to connect to.

2. Can we send request by using ip and port.

Yes.

3. Can we use host name.

Yes. You need either IP or hostname, and the port.

4. Any echo server name? i need to send message to this server want to know response.

You can setup an echo server on your machine so that you can first focus on coding your client. There is a Standard Echo server defined at port 7. Setup depends on your operating system environment - On Ubuntu Linux, I had to install the xinetd package and enable the echo server in /etc/xinetd.d/echo. Then, you can verify if the server is running by using the telnet program:

andreas@ubuntu:~$ telnet localhost 7
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello
Hello

Using telnet to connect to any port is also a common technique to verify if a server is running and reachable. With the example from your question, you could check with telnet 127.0.0.1 10008 whether a server is running on the port you specified - you will get the same connetion refused as from your Java program if no server is available.

5. I try both server,java and client.java then i got result? is this solution for me.

Not sure which server and client you are referring to.


Some additional references:

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • I want to connect remote server. which send me response. Suppose i give `Servername= "ABC" and port = 1234`. is this possible? – Sandip Armal Patil Nov 08 '12 at 07:25
  • Yes this is possible - if `ABC` can be resolved to a valid IP and a server is listening on Port `1234` on the machine with this IP. I really recommend that you are reading "All about sockets" linked in my answer above. – Andreas Fester Nov 08 '12 at 07:37
1

You have to run your server program also in local host. You are getting this exception because of 10008 port not ruining on your machine.

Some Question:

  1. I write client Java File but whether i need to write server java file.

    either you can write a server program or you can connect to a remote server. In that case you should have both ip and running port in remote server.

  2. Can we send request by using ip and port.

    Yes You required both to send a message.

  3. Can we use host name.

    If you machine can resolve your host name you can do it. Otherwice you can use ip address

  4. Any echo server name? i need to send message to this server want to know response.

    I have no idea about this , need to search on web.

  5. I try both server,java and client.java then i got result? is this solution for me.

Yes.

Community
  • 1
  • 1
someone
  • 6,577
  • 7
  • 37
  • 60