Java Client-Server。 有必要在后台接收来自客户端的消息。 我正在尝试在后台接收消息:
.BAT
Bacground部分:
public Server() throws IOException {
try {
serverSocket=new ServerSocket(1234);
fromclient= serverSocket.accept();
System.out.println("Socket created");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Socket not created");
}
try {
in = new BufferedReader(new
InputStreamReader(fromclient.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
out = new PrintWriter(fromclient.getOutputStream(),true);
String input,output;
}
并在客户端部分发送消息:
public void run(ServerSocket welcomeSocket) throws IOException {
Socket connectionSocket = welcomeSocket.
accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println(clientSentence);
capitalizedSentence =
clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
但没有任何反应。
答案 0 :(得分:1)
您在端口1234上打开了一个ServerSocket,然后在Socket上打开了6789.尝试使用相同的端口:)