连接python套接字和java套接字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20913440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
connecting python socket and java socket
提问by carlos palma
I have been trying to send a simple string between a Java client socket and a Python server socket. This is the code for the server socket:
我一直在尝试在 Java 客户端套接字和 Python 服务器套接字之间发送一个简单的字符串。这是服务器套接字的代码:
HOST=''
PORT=12000
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADRR,1)
s.bind((HOST,PORT))
s.listen(5)
device=variador()
while True:
conn,addr=s.accept()
if data=="turn_on":
respuesta=device.send_order(variador.start_order)
conn.send(respuesta+'\n')
conn.close()
and the client code is:
客户端代码是:
try {
Socket socket = new Socket("192.168.10.171", 12000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
out.print(command);
out.close();
in.close();
socket.close();
} catch (UnknownHostException e) {
System.err.println("Unknown Host.");
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection.");
// System.exit(1);
}
Everything works fine until I try to read the server's response, using this:
一切正常,直到我尝试使用以下命令读取服务器的响应:
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
then the code hangs and the Python server does not receive any information, which I tested using print.
然后代码挂起并且 Python 服务器没有收到任何信息,我使用打印测试了这些信息。
Is there a problem trying to send first and then wait for a response from the server in the Java client?
在 Java 客户端中尝试先发送然后等待来自服务器的响应是否有问题?
Any help will be much appreciated.
任何帮助都感激不尽。
回答by carlos palma
Well, I discovered that the Java client hangs because the messages sent by the python server were not explicitly finished with \r\n
, so the Python code should have been something like this:
好吧,我发现 Java 客户端挂起是因为 python 服务器发送的消息没有明确完成\r\n
,所以 Python 代码应该是这样的:
HOST = ''
PORT = 12000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADRR, 1)
s.bind((HOST, PORT))
s.listen(5)
device = variador()
while True:
conn, addr = s.accept()
if data == "turn_on\r\n":
respuesta = device.send_order(variador.start_order)
conn.send(respuesta + '\r\n')
conn.close()
I know it should have been quite obvious from the name of the methods in Java, readline()
and println
, both suggesting that java ends strings with the sequence \r\n
我知道从 Java 中的方法名称中应该很明显,readline()
并且println
,两者都表明 java 以序列结束字符串\r\n
回答by Aksh1801
If you only want to send a single command on the socket connection, then close the OutputStream after writing the command (using Socket.shutdownOutput()). The socket is reading until EOF and you will not receive EOF until you close the socket. hence, your code never proceeds.
如果您只想在套接字连接上发送单个命令,请在写入命令后关闭 OutputStream(使用 Socket.shutdownOutput())。套接字在 EOF 之前一直在读取,并且在关闭套接字之前您将不会收到 EOF。因此,您的代码永远不会继续。
Source - java socket InputStream hangs/blocks on both client and server
源 - java 套接字 InputStream 在客户端和服务器上挂起/阻塞
Hope it helps!
希望能帮助到你!