Where can I find the hostname of my server in Java Socket Programming

Viewed 19

I'm trying to connect two MacBooks using Java network programming, one is a server and the other is a client. In server, I wrote something like

ServerSocket s = new ServerSocket(8008);

and then in my client computer I wrote

hostname = "Alex";
Socket socket = new Socket(hostname, 8008);

where "Alex" is the computer name of my server, found in System Preference -> Sharing -> Computer Name, as shown in the screenshot. However when I run the script on client I got the following error message:

error: Alex
java.net.UnknownHostException: Alex
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:564)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at java.base/java.net.Socket.connect(Socket.java:583)
        at java.base/java.net.Socket.<init>(Socket.java:507)
        at java.base/java.net.Socket.<init>(Socket.java:287)
        at EchoClient.main(EchoClient.java:9)

It seems that the hostname of my server is not recognized. Then where should I look up the hostname of my server computer?

Thanks!

1 Answers

If you are a Linux system, add a record in /etc/hosts. If you are a Windows system, add a record in C:\windows\system32\drivers\etc\hosts 127.0.0.1 Alex ::1 Alex

Related