Socket an invalid argument was supplied in python

218 Views Asked by At
import socket
from _thread import *
import pickle

server = "192.168.0.171"
port = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    s.bind((server, port))
except socket.error as e:
    str(e)

s.listen(2)
print("Waiting for connection, Server Started")


def read_pos(str):  # Get position
    str = str.split(",")
    return int(str[0]), int(str[1]), int(str[2]), int(str[3]), int(str[4]), int(str[5])


def make_pos(tup, n1, n2, n3):  # Make a position
    return str(tup[0]) + "," + str(tup[1]) + "," + str(n1) + "," + str(n2) + "," + str(n3)


pos = [(200, 225, 0, 0, 0), (750, 225, 0, 0, 0)]


def threaded_client(conn, player):
    # Make sure it indeed connect
    conn.send(str.encode(make_pos(pos[player])))
    reply = ""
    while True:
        try:
            # Amount of info receive = 2048
            data = read_pos(conn.recv(2048).decode())
            pos[player] = data
            if player == 1:
                reply = pos[0]
            else:
                reply = pos[1]


            if not data:
                print("Disconnected")
                break
            else:
                print("Received: ", data)
                print("Sending: ", reply)
            conn.sendall(str.encode(make_pos(reply)))
        except:
            break
            # No connection

    print("Lost connection")
    conn.close()

currentPlayer = 0
while True:
    conn, addr = s.accept()
    print("Connected to:", addr)

    start_new_thread(threaded_client, (conn, currentPlayer))
    currentPlayer += 1

Traceback (most recent call last): File "C:\Users\HP\PycharmProjects\Online_minigame_01\server.py", line 15, in s.listen(2) OSError: [WinError 10022] An invalid argument was supplied

So, I tried to make a online 2 player space ship shooting game. But this part(server) could't even run. I had tried many ways and it still won't work. How should I fix it?

0

There are 0 best solutions below