Exemple #1
0
    def handle_client (self, client, addr):
        self.handshake(client)
        try:
            while 1:
                delayTime = 1 #the delay between the packages for making the car move
                data = self.recv_data(client)
                print("received [%s]" % (data,))
                self.broadcast_resp(data)
                switch = data
                print (switch)
                if switch == "forward":
                 print("Forward command received")
                 #code here so motors go forward
                 carCon.runCar(delayTime,"forward")
                elif switch == "back":
                 print("Backward command received")
                 #code here so motors go backward
                 carCon.runCar(delayTime,"back")
                elif switch == "right":
                 print("Right command received")
                 #code here so motors go right
                 carCon.runCar(delayTime,"right")
                elif switch == "left":
                 print("Left command received")
                 #code here so motors go  left
                 carCon.runCar(delayTime,"left")
                elif switch == "stop":
                 print("stop command received")
                 #code here so motors go stop
                 carCon.runCar(delayTime,"stop")
                else:
                 client.send('Error')  #returns "Error" if the command DON'T exists

        except Exception as e:
            print("Exception %s" % (str(e)))
        print('Client closed: ' + str(addr))
        self.LOCK.acquire()
        self.clients.remove(client)
        self.LOCK.release()
        client.close()
Exemple #2
0
s.bind((HOST, PORT))
s.listen(1)
c, addr = s.accept()
c.sendall('connected to server')
print "Connected by", addr

while True:
   c, addr = s.accept()     # Establish connection with client.
   #print 'Got connection from', addr
   inData =  c.recv(1024)
   if(len(inData) > 0):
       switch = inData
       inData = None
       if switch == "forward":
            c.sendall('ok')     #returns "ok" if the command exists
            carCon.runCar(delayTime,"forward")
       elif switch == "back":
            c.sendall('ok')     #returns "ok" if the command exists
            carCon.runCar(delayTime,"back")
       elif switch == "right":
            c.sendall('ok')     #returns "ok" if the command exists
            carCon.runCar(delayTime,"right")
       elif switch == "left":
            c.sendall('ok')     #returns "ok" if the command exists
            carCon.runCar(delayTime,"left")
       elif switch == "reset":
            c.sendall("ok")
            carCon.reset();
       elif switch == "exit":
            c.sendall("exiting the server")
            exit(0)