我正在尝试通过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/