简体   繁体   中英

Socket: could not connect to server or specific port

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM