首先,我很抱歉我的英语。英语不是我的母语。 所以,首先我创建了客户端。
import java.net.*;
import java.io.*;
public class Main {
public static void main (String [] args) {
try (Socket socket = new Socket ("IP", 4747)){
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader (input));
System.out.println(reader.readLine());
} catch (IOException ex) {
System.out.println("Client message " + ex.getMessage());
ex.printStackTrace();
}
}
}
and I create Server Side as well.
import java.net.*;
import java.io.*;
public class Main {
public static void main (String [] args) {
try (ServerSocket serverSocket = new ServerSocket (4747)){
System.out.println("Server is listing client");
while (true) {
System.out.println("Connection with new client");
Socket socket = serverSocket.accept();
OutputStream output = socket.getOutputStream();
PrintWriter sendMessage = new PrintWriter (output);
sendMessage.println("Hello from server");
}
} catch (IOException ex) {
System.out.println("Server message " + ex.getMessage());
ex.printStackTrace();
}
}
}
关于IP。 我使用命令ipconfig从命令行获取IP。 我的问题。 服务器见客户端。但不向客户端发送消息,客户端无法从服务器获取消息。 我的错误是什么?