1

I am trying to send Xbox One controller data over wifi from a host computer to the client. They are both running Linux and sometimes the code works flawlessly and others there is extreme lag between the two of them.

Here is the code I have right now for both the client and server, they are both connected to the same network which has no internet connection. Any help is greatly appreciated

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)
  • Try using a profiler to understand your own code, [here's](https://stackoverflow.com/questions/1896032/using-cprofile-results-with-kcachegrind) a good start. Or use some simple `print(time() - start_time)` to figure out where your bottle necks are. My guess is `len(buf)` or wherever you're using `sendtoclient`, seeing as your code is incomplete. – Torxed Apr 07 '19 at 21:45
  • Don't receive a byte at a time. This is horrifically inefficient. Receive into a large buffer and traverse it – user207421 Apr 08 '19 at 00:12
  • @Torxed I used your advice for checking the time for each action and discovered that it was in my code where I was using multiprocessing after I removed the multiprocessing it worked perfectly. user207421 I will definitely be sure to change that so I receive all the data at once. I was just worried that by iterating through the string on the client side I would bog it down. However I will give your advice a try because it seems like the better way of doing it. Thank you for the advice I appreciate it greatly – Robert Killkelley Apr 08 '19 at 00:21
  • 1
    This is why it's extremely important to give us all the relevant code. There's nothing in here that has to do with multiprocessing so we would never be able to solve it :P – Torxed Apr 08 '19 at 06:05

0 Answers0