嗨,我建立項目與春季啓動和使用套接字編程在客戶端和服務器之間進行通信。我開始我的項目在本地主機端口號爲8080 而我使用此代碼運行我的客戶端程序:如何在服務器端創建應該給出響應的監聽器?
String serverName = "localhost";
int port = 8080;
try {
System.out.println("Connecting to " + serverName + " on port "
+ port);
Socket client = new Socket(serverName, port);
System.out.println("Just connected to "
+ client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
System.out.println("Hello from " + client.getLocalSocketAddress());
out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
這「只是連接到127.0.0.1:8080」但線之上拋出異常,程序將打印信息「client.close」:
Connecting to localhost on port 8080
Just connected to localhost/127.0.0.1:8080
Hello from /127.0.0.1:37281
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readUTF(DataInputStream.java:609)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at com.oodles.Demo.App.main(App.java:39)
我的要求是如何創建服務器端監聽可以從客戶端處理該消息併發送回一些響應