在Python中使用套接字库时极度滞后

时间:2019-04-07 21:35:59

标签: python sockets wifi

我正在尝试通过wifi从主机向客户端发送Xbox One控制器数据。它们都在Linux上运行,有时代码可以完美工作,而其他两个代码之间则存在极大的滞后性。

这是我现在为客户端和服务器提供的代码,它们都连接到没有Internet连接的同一网络。任何帮助将不胜感激

Server.py

from inputs import get_gamepad
import socket
import time

HOST='192.168.1.198'
PORT=5002
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(2)
conn, addr=s.accept()

def gamepad():
    while 1:
        events = get_gamepad()
        for event in events:
            if(event.code == "ABS_Y"): #ABS_Z
                if (event.state > 9000):
                    percentage = float(float(event.state)/maxThrottle)
                    escValLeft.value = (((maxESC-minESC)/(maxThrottle-minThrottle))*float(event.state) + minESC)
                elif (event.state < -9000):
                    escValLeft.value = (((maxESCRev-minESCRev)/(maxThrottle-minThrottle))*float(abs(event.state)) + minESCRev+10)
                else:
                    escValLeft.value = minESC

            if(event.code == "ABS_RZ"):
                percentage = float(event.state/maxThrottle)
                escValRight.value = (((maxESC-minESC)/(maxThrottle-minThrottle))*float(event.state) + minESC)


def sendtoclient():
    while True:
        val = str(int(escValLeft.value))+","+str(int(escValRight.value))+sep
        print("Sending: " + val)
        conn.send(val)

client.py

import socket

HOST = '192.168.2.62'
PORT = 5002
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

dataL = 2550
dataR = 2550

def get_controller():
    global dataL
    global dataR

    while True:
        buf = ''
        hold = ''
        while len(buf) < 9:
            hold = s.recv(1)
            #print (hold)
            if hold == '[':
                pass
            elif hold == ']':
                pass
            else:
                buf += hold
        left,right = buf.split(',')
        dataL = int(left)
        dataR = int(right)
        print("L,R ",dataL,dataR)

0 个答案:

没有答案