我正在尝试使用 java.net 在 Java 中的服务器和客户端之间建立连接。我设法连接,但没有发送任何数据。我已经粘贴了代码和输出:
服务器代码:
ServerSocket welcomeSocket = new ServerSocket(999);
Socket connectionSocket = welcomeSocket.accept();
System.out.println("Connected to: " + connectionSocket.toString());
DataOutputStream outToClient = newDataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes("Testing connection");
客户代码:
Socket clientSocket = new Socket("130.236.248.52", 999);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
modifiedSentence = inFromServer.readLine();
System.out.println("[From server:] " + modifiedSentence);
服务器输出:连接到:Socket[addr=/130.236.248.54,port=51535,localport=999]
客户端输出: