简体   繁体   English

带有Java客户端的python套接字服务器-socket.error:[Errno 32]管道损坏

[英]python socket server with java client - socket.error: [Errno 32] Broken pipe

I'm trying to send commands to a rasberry pi using a python based socket server, where the server will get various string command, do something and then wait for the next command. 我正在尝试使用基于python的套接字服务器将命令发送到rasberry pi,其中服务器将获取各种字符串命令,执行某些操作,然后等待下一个命令。

I have a socket server written in python running on a raspberry pi: 我有一个用python编写的套接字服务器在树莓派上运行:

import socket

HOST = ''   # Symbolic name meaning the local host
PORT = 11113    # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error code: ' + str(msg[0]) + 'Error message: ' + msg[1]
    sys.exit()
print 'Socket bind complete'


    def listen():
        s.listen(1)
        print 'Socket now listening'

        # Accept the connection
        (conn, addr) = s.accept()
        print 'Server: got connection from client ' + addr[0] + ':' + str(addr[1])

        while 1:
            data = conn.recv(1024)
            tokens = data.split(' ', 1)
            command = tokens[0].strip()

            print command

            # Send reply
            conn.send("Ack")
            break

        conn.close()
        # s.close()
        listen()
        print "connection closed"

    listen()

Java Client: Java客户端:

public class Client {

    public static void main(String... args) throws Exception {
        int portNum = 11113;

        Socket socket;

        socket = new Socket("192.168.1.20", portNum);


        DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
        dout.writeUTF("Hello");
        dout.flush();
        dout.close();
        socket.close();


    }
}

Python Server starts ok and waits for a connection, when I run the client code the server outputs the hello text followed by a lot of whitespace and then Python Server启动正常并等待连接,当我运行客户端代码时,服务器输出hello文本,后跟大量空白,然后

edit: the white space is that while 1 loop outputing the incoming data and then looping out of control until it crashes. 编辑:空白是while 1循环输出传入的数据,然后循环失控直到崩溃。 I want to output the text and keep listing for more connections. 我想输出文本并继续列出以获取更多连接。

edit 2: fixed python so it doesn't crash - i leave the loop and restart the listen process - which works. 编辑2:固定的python,因此它不会崩溃-我离开循环并重新启动侦听过程-可行。 If this script can be improved please lmk - it doesn't look like it will scale. 如果可以改进此脚本,请lmk-它看起来像不会扩展。

errno.32 is: Broken pipe(the broken pipe error occurs if one end of the TCP socket closes connection(using disconnect) or gets killed and the other errno.32是:管道断开(如果TCP套接字的一端关闭连接(使用断开连接)或被杀死,而另一端被终止,则发生管道断开错误)

In you java client, you send some data and close the socket right away which may cause the result.I am not familiar with java.But here is two things below you need to follow. 在您的Java客户端中,您发送一些数据并立即关闭套接字,这可能会导致结果。我对java并不熟悉,但是需要注意以下两点。

1.remove sock.close() in java client. 1.在Java客户端中删除sock.close()。

2.make your java client sleep for a while, but exit. 2.让您的Java客户端休眠一段时间,然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM