java - Java Socket Server 与 Python 客户端通信,程序不会进入“inputLine = in.readLine()”-loop
问题描述
我正在尝试用 Java 创建一个可以与 python 客户端通信的套接字服务器。我遇到的问题是,如果我启动服务器,通过 python 发送消息,然后立即退出,一切都执行得很好,但是如果客户端发送消息然后再次监听套接字,服务器将不会响应。
这是代码,大部分来自oracle的这个例子
public class PrimeServer {
public static void main(String[] args) throws IOException {
int portNumber = 4444;
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()))
) {
String inputLine;
String outputLine;
String rawMessage;
// Initiate conversation with client
PrimeProtocol ppc = new PrimeProtocol();
outputLine = String.format("%1022s", "Ready").replace(' ', '0');
out.println(outputLine);
System.out.println("Over while");
while ((inputLine = in.readLine()) != null) {
System.out.println("In while");
if (ppc.processInput(Integer.parseInt(inputLine))){
rawMessage = "True";
} else {
rawMessage = "False";
}
outputLine = String.format("%1024s", rawMessage).replace(' ', '0');
System.out.println("Outputting " + rawMessage);
out.println(outputLine);
}
System.out.println("Passed while.");
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
以及用于良好测量的python代码:
msg_len = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 4444))
init_message = sock.recv(msg_len).decode("UTF-8")
init_message = init_message.replace('0', '')
print(init_message)
print(len(init_message))
sock.send(b'3')
使用此代码,Java 服务器的输出为:
Over while
In while
Outputting True
Passed while.
客户端输出是:
1024
Ready
之后连接中断。
但是如果我将这一行添加到客户端:new_message = sock.recv(msg_len).decode("UTF-8")
制作新的python:
msg_len = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 4444))
init_message = sock.recv(msg_len).decode("UTF-8")
print(len(init_message))
init_message = init_message.replace('0', '')
print(init_message)
sock.send(b'3')
new_message = sock.recv(msg_len).decode("UTF-8")
服务器卡住了,输出只有:
Over while
而客户端输出:
1024
Ready
同时也卡顿。
有什么想法吗?
解决方案
原来是需要刷新的python套接字。新的python代码:
msg_len = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 4444))
init_message = sock.recv(msg_len).decode("UTF-8")
print(len(init_message))
init_message = init_message.replace('0', '')
print(init_message)
sock.send(b'3\n')
new_message = sock.recv(msg_len).decode("UTF-8")
print(new_message)
(添加\n)到消息出去。
推荐阅读
- r - “不能对不存在的列进行子集化。x 列 `Years` 不存在。”
- java - 了解 Apache HTTP 客户端中由 PoolingHttpClientConnectionManager 管理的连接的生命周期
- javascript - 以角度将“Bearer-Token”添加到http标头
- javascript - AuthGuard 在 AuthService 之前加载
- django - Django 模型的父母之间是否存在父子关系?
- compression - 什么时候进行解压过程以及solidbreak标志在Inno Setup中如何真正起作用?
- powershell - 使用 Where {(...)} 后如何在 Powershell 中执行两个命令
- android - 当我在 android 中的 Recyclerview 上滚动时,项目视图的高度会降低
- android - 使用 Android 模拟器接收时虚拟串行端口崩溃错误:检测到挂起的线程“QEMU2 主循环”
- elasticsearch - 按嵌套地理距离属性排序