简体   繁体   中英

Python socket errno 32 broken pipe

I am trying to send a message over TCP/IP via python, the first message has been received but when I try to send another one it returns: "socket error32 broken pipe"

my code:

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()

I tried searching for this issue but I couldn't find a solution, the socket is still open when I try to send the second message. Does anyone have a solution for this?

maybe SIGPIPE error :

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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