python多线程tcp套接字服务器,客户端和损坏的管道

时间:2018-10-24 18:31:36

标签: python sockets tcp python-multithreading

我正在使用树莓派pi3创建一个可从客户端访问的远程电源开关的项目。我希望客户端能够读取和设置开关的状态。我已经汇编了一些python(2.7)代码。我已经关闭,但是随机遇到错误32管道破裂错误,并且我认为我没有在线程之间正确共享变量,或者计时不正确,并导致超时关闭连接。任何帮助,将不胜感激。

服务器

import socket
import threading
from threading import Thread 
from SocketServer import ThreadingMixIn
import time
import RPi.GPIO as GPIO

POWER = 5 # GPIO for SS Relay
LOCKOUT = "lcoff"
state = "PowerOff"

def setup():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(POWER, GPIO.OUT)

# Multithreaded Python server : TCP Server Socket Thread Pool
class ClientThread(Thread):
    Thread.LOCKOUT = 'lcoff'
    Thread.state = 'PowerOff'

    def __init__(self,ip,port): 
        Thread.__init__(self) 
        self.ip = ip 
        self.port = port 
        print ("[+] New server socket thread started for " + ip + ":" + str(port))

    def run(self):
        while True :
            cmd = conn.recv(40) 
            print ("Server received data:"), cmd
            if GPIO.input(5) == 0:
                Thread.state = "PowerOff"
            else:
                Thread.state = "PowerOn"  
            if cmd == "query":
                print("query")
            if cmd == "lcoff":
                Thread.lockout="lcoff"
                Thread.state="LockoutOff"
            if cmd == "lcon":
                Thread.lockout="lcon"
                Thread.state="LockoutOn"
            if cmd == "setoff" and LOCKOUT == "lcoff":
                Thread.state="PowerOff"
                GPIO.output(5,False)
                print(Thread.state)
            if cmd == "seton" and LOCKOUT == "lcoff":
                Thread.state="PowerOn"
                GPIO.output(5,True)
                print(Thread.state)
            if cmd == 'exit':
                break
            print ("MT-Python Server - Reporting current state:", Thread.state)
            conn.send(Thread.state)  # echo

# Multithreaded Python server : TCP Server Socket Program Stub
setup()
TCP_IP = '192.168.0.40'
TCP_PORT = 59115 
BUFFER_SIZE = 40  # Usually 1024, but we need quick response 

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
tcpServer.bind((TCP_IP, TCP_PORT)) 
threads = [] 

while True: 
    tcpServer.listen(4) 
    print ("MT-Python server: Waiting for connections from TCP clients...")
    (conn, (ip,port)) = tcpServer.accept() 
    newthread = ClientThread(ip,port)
    newthread.start() 
    threads.append(newthread)

for t in threads: 
    t.join()

客户

# Python TCP Client A
import socket
import RPi.GPIO as GPIO

def setup():

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #GPIO for local Switch
    GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #GPIO for local Switch

host = '192.168.0.40'
port = 59115
BUFFER_SIZE = 40
cmd = "OK" 


setup() 
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpClientA.connect((host, port))

cmd = raw_input("tcpClientA: Enter message/ Enter exit:")

while cmd != 'exit':
    if GPIO.input(23) == 1:
        cmd = "setoff"
        tcpClientA.send(cmd)     
        data = tcpClientA.recv(BUFFER_SIZE)
        print " Client2 received data:", data
    if GPIO.input(24) == 1:
        cmd = "seton"
        tcpClientA.send(cmd)     
        data = tcpClientA.recv(BUFFER_SIZE)
        print " Client2 received data:", data

    #cmd = raw_input("tcpClientA: Enter message to continue/ Enter exit:")

tcpClientA.close() 

0 个答案:

没有答案