简体   繁体   中英

Java unknownhostException with two computers

import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;



public class ChatClient{

    private final String serverName;
    private final int serverPort;
    private Socket socket;
    private InputStream serverIn;
    private OutputStream serverOut;

    public ChatClient(String serverName, int serverPort) {
        this.serverName = serverName;
        this.serverPort = serverPort;
    }

    public static void main(String[] args){
        ChatClient client = new ChatClient("raspberrypi", 6342);
        if (!client.connect()){
            System.err.println("Connect failed");
        }else{
            System.out.println("Connected");
        }
    }

    private boolean connect(){
        try{
            this.socket = new Socket("raspberrypi", 6342);
            this.serverOut = socket.getOutputStream();
            this.serverIn = socket.getInputStream();
            return true;
        }catch (IOException e){
            e.printStackTrace();
        }
        return false;

    }
}

I have a server which I am running which is waiting for an client to join. However since I am using the client on my computer and the server on my raspberry pi, I keep getting the unknown host exception. I have tried to put the hostname of the raspberry pi in but it doesn't seem to work. Ps I might be getting the host name wrong.

It does not work, because the value you provide to the first argument (raspberrypi) is not a valid host name.

You need to pass there the IP address of your raspberry in your network. If you are using home router, you can even set static IP for your raspberry (it's common, because without that, every time you restart your raspberry, you may get different address from DHCP server).

After you know the address of your raspberry, simply replace "raspberrypi" with the address (in form of "192.168.0.10" depending on your configuration).

Here's how to set static IP: https://www.raspberrypi.org/learning/networking-lessons/rpi-static-ip-address/

Edit the file /etc/dhcpcd.conf as follows:

Type sudo nano /etc/dhcpcd.conf at the command prompt.

Scroll to the bottom of the script, and add the following lines:

  interface eth0 static ip_address=192.168.0.2/24 static routers=192.168.0.1 static domain_name_servers=192.168.0.1 interface wlan0 static ip_address=192.168.0.2/24 static routers=192.168.0.1 static domain_name_servers=192.168.0.1 
  1. Save the file with ctrl + o and then exit nano with ctrl + x. Your Raspberry Pi will now boot up with the IP address 192.168.0.2 every time; we didn't use 192.168.0.1 as this is reserved for the router. You can of course use any address you like, but in the configuration above the range must be between 192.168.0.2 and 192.168.0.255.

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