python - Python套接字errno 32损坏的管道

标签 python sockets

我正在尝试通过python通过TCP/IP发送消息,但已收到第一条消息,但是当我尝试发送另一条消息时,它返回:“套接字错误32损坏的管道”

我的代码:

import socket
from RPi import GPIO
from time import sleep

TCP_IP = '192.168.178.29'
TCP_PORT = 45335
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

clk = 17
dt = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

counter = 0
clkLastState = GPIO.input(clk)

try:
      while True:
            clkState = GPIO.input(clk)
            dtState = GPIO.input(dt)
            if clkState != clkLastState:
                    if dtState != clkState:
                            counter += 1
                    else:
                            counter -= 1
                    s.send(str(counter))
                    print counter
            clkLastState = clkState
            sleep(0.01)
finally:
    GPIO.cleanup()

我尝试搜索此问题,但找不到解决方案,尝试发送第二条消息时套接字仍处于打开状态。
有人对此有解决方案吗?

最佳答案

可能是SIGPIPE错误:

Your server process has received a SIGPIPE writing to a socket. This usually happens when you write to a socket fully closed on the other (client) side. This might be happening when a client program doesn't wait till all the data from the server is received and simply closes a socket (using close function).

In a C program you would normally try setting to ignore SIGPIPE signal or setting a dummy signal handler for it. In this case a simple error will be returned when writing to a closed socket. In your case a python seems to throw an exception that can be handled as a premature disconnect of the client.



How to prevent errno 32 broken pipe?

How to handle a broken pipe (SIGPIPE) in python?

关于python - Python套接字errno 32损坏的管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46533224/

相关文章:

python - 如何更改 QTableWidget 垂直标题的颜色

python - 如何在动画结束时自动关闭图形?

PHP 脚本作为 Debian 中的守护进程

java - 如何接收从服务器发送的字节数组并一次读取 4 个单字节

c++ - 尝试创建 UDP 服务器

java - 中断被阻塞的线程,等待来自套接字的输入

python - QRegExp 和 QSyntaxHighlighter 的单引号文本

python - sklearn 上的逻辑回归函数

python - 在 MacOSX 上为 Eclipse 多次安装 Python

c# - 如何使用udp从C#客户端与C服务器通信?