0

我在android中使用标准java.net.Socket,在python中使用socket模块,当我在java中执行以下操作时:

sk = new Socket(addr, port);
input = new BufferedReader(new InputStreamReader(sk.getInputStream()));
output = new DataOutputStream(sk.getOutputStream());
...
output.writeUTF("connect 216082611046");
String message = input.readLine();
Log.wtf("cst", message);

并在python中遵循:

...
connection, addr = server.accept()
print "Received connection from", addr  
s = connection.recv(1024)
...
connection.sendall(ip) #ip is some string there

我遇到了麻烦:java 接收到“connection.sendall(ip)”发送的消息,但只有当我关闭套接字时,但我需要让它保持活动状态并根据收到的数据继续消息传递。有没有办法在不关闭套接字的情况下获得答案?

4

1 回答 1

0

这种行为的一个原因可能是您的ip字符串没有以换行符结尾。

readLine尝试读取整行,因此如果输入流中没有换行符,它将阻塞直到流关闭。

于 2013-09-06T22:56:44.810 回答