简体   繁体   中英

Client Socket cannot find Server Socket. Throws UnknownHostException despite having the same port number

I'm following an online tutorial to get used to clients&servers in Java before I really start working with them.

All I am trying to do right now is start the server and then connect the client. It's only a couple lines of code for the client and the server and it all seemed very straightforward to me, so I'm not sure what the problem is.

I run my server program, then run my client program, but the client socket cannot find my server socket for some reason even though they have the same port number.

Here is the code.

Server:

public class Server {


     private ServerSocket serverSocket;
     private int portNumber;

     public Server(int portNumber){
        this.portNumber=portNumber;
    }

    public void run() throws IOException {
        serverSocket = new ServerSocket(portNumber);
        System.out.println("server at port "+portNumber);
        System.out.println("Waiting for client.");
        Socket clientSocket = serverSocket.accept();
        System.out.println("Connected.");
    }

     public static void main(String[] args) {
             int portNumber = 9999;
             try {
                Server serverSocket = new Server(portNumber);
                 serverSocket.run();
             }
             catch(IOException e){
                 e.printStackTrace();
             }
         }
    }

And then my client program:

public class Client {
    private int portNumber;
    private String host;
    Socket clientSocket;


    public Client(int portNumber,String host){
        this.portNumber=portNumber;
        this.host=host;
    }

    public void connect() throws IOException, UnknownHostException {
        System.out.println("Connecting to port "+portNumber);
        clientSocket = new Socket(host,portNumber);
        System.out.println("Connected");
    }
public static void main(String arg[]){
            Client clientSocket = new Client(9999,"host");
            try {
                clientSocket.connect();
            }
            catch(UnknownHostException e){
                System.err.println("Can't connect to host");
            }
            catch (IOException e) {
                System.err.println("Can't connet. "+e.getMessage());

            }

        }
    }

UnknownHostException is thrown when I run the client program.

I thought I followed the tutorial pretty closely, but I don't know what the problem with my code is.

I would guess it had something to do with my connect() method because that's where the exception is thrown, but I can't say for sure.

Any ideas?

In you code

Client clientSocket = new Client(9999,"host");

Can you change and run to

Client clientSocket = new Client(9999,"localhost");

Your code is looking for a host named "host". either give it the ip address of the host. If you want to use the variable host defined above use

Client clientSocket = new Client(9999,host);

But I do not see a place it is getting initialized

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