0
我有TCPServer.java:这种TCP交互有什么问题?
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
运行py的终端每秒thonClient.py。不过,我得到的只是在java控制台中的“Request count:1”,没有其他的东西。在终端,我得到:
received:
s
received:
end another request please
为什么这不按预期工作?为什么在一次迭代后全部被锁定,为什么服务器的第一个响应被接受为两个不同的消息?