我有 TCPServer.java:
import java.util.*;
import java.net.*;
import java.io.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket tcp = new ServerSocket(1387);
while (true) {
Socket request = tcp.accept();
Scanner sc = new Scanner(request.getInputStream());
DataOutputStream out = new DataOutputStream(request.getOutputStream());
System.out.println("Request count: " + sc.nextLine());
out.writeBytes("send another request please");
}
}
}
和pythonClient.py
:
#!/usr/bin/python
import socket, time, sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((sys.argv[1], 1387))
count = 0
while True:
count += 1
sock.sendall(str(count) + "\n")
time.sleep(1)
data = sock.recv(1024)
print "received:\n", str(data)
我在同一台主机上运行,并期望每秒收到“请求计数:x”,x
并在 TCPServer 的控制台中增加一个值,并且
received:
send another request please
在运行 pythonClient.py 的终端中的每一秒。但是,我得到的只是 java 控制台中的“请求计数:1”,仅此而已。在终端中,我得到:
received:
s
received:
end another request please
为什么这没有按预期工作?为什么在一次迭代后它全部被锁定,为什么来自服务器的第一个响应被接受为两个不同的消息?