sockets wont send data over LAN

Viewed 49

im trying to make a script that when a WASD button is pressed it uses a socket to send that to a server over a LAN network here is the client:

import socket
from pynput.keyboard import Listener

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

import keyboard 




def On_pressA():
    print("dda")
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
       s.connect((HOST, PORT))
       s.sendall(b'LEFT')
       data = s.recv(1024)
       print('Received', repr(data))

def On_pressW():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((HOST, PORT))
      s.sendall(b'FORWARD')
      data = s.recv(1024)
      print('Received', repr(data))

def On_pressS():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
       s.connect((HOST, PORT))
       s.sendall(b'BACKWARDS')
       data = s.recv(1024)
       print('Received', repr(data))

def On_pressD():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(b'RIGHT')
        data = s.recv(1024)
        print('Received', repr(data))


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    keyboard.add_hotkey('a', On_pressA) 
    keyboard.add_hotkey('d', On_pressD())
    keyboard.add_hotkey('s', On_pressS())
    keyboard.add_hotkey('w', On_pressW())
    
    s.sendall(b'LEFT')
    data = s.recv(1025)

the keypress work it prints when i press A like its supposed to but it wont send that data to the server here is the server:

    import socket

HOST = '127.0.0.1'
PORT = 65432
loop = 0
while loop < 5:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        conn, addr = s.accept()
        with conn:
            print('Done', addr)
            while True:
                data = conn.recv(1024)
                print(data)
                if not data:
                    print("Nothing sent")
                conn.sendall

im probably doing something wrong as im new to python. im trying to use this to make a wifi remote RC car.

1 Answers

Here's the problem :

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    keyboard.add_hotkey('a', On_pressA) 
    keyboard.add_hotkey('d', On_pressD())
    keyboard.add_hotkey('s', On_pressS())
    keyboard.add_hotkey('w', On_pressW())

Instead of keyboard.add_hotkey('a', On_pressA) it should be keyboard.add_hotkey('a', On_pressA()) (Notice the () at the end of statement)

The final code should look somewhat like :

import socket
from pynput.keyboard import Listener

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

import keyboard 




def On_pressA():
    print("dda")
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
       s.connect((HOST, PORT))
       s.sendall(b'LEFT')
       data = s.recv(1024)
       print('Received', repr(data))

def On_pressW():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.connect((HOST, PORT))
      s.sendall(b'FORWARD')
      data = s.recv(1024)
      print('Received', repr(data))

def On_pressS():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
       s.connect((HOST, PORT))
       s.sendall(b'BACKWARDS')
       data = s.recv(1024)
       print('Received', repr(data))

def On_pressD():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        s.sendall(b'RIGHT')
        data = s.recv(1024)
        print('Received', repr(data))


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    keyboard.add_hotkey('a', On_pressA()) 
    keyboard.add_hotkey('d', On_pressD())
    keyboard.add_hotkey('s', On_pressS())
    keyboard.add_hotkey('w', On_pressW())
    
    s.sendall(b'LEFT')
    data = s.recv(1025)
Related