Kyle Burns
Kyle Burns

Reputation: 167

python socket bind invalid argument was supplied

im working on a project that allows me to connect to my client application but when I disconnect the server and then host again the program throws this error:

[WinError 10022] An invalid argument was supplied

this is thrown by the socket.bind command:

self.Server.bind((str(StrHost),int(IntPort)))

I've checked that the StrHost and IntPort are the right values and they are fine. it only happens when I disconnect and re-connect using the server program.

thank you for any help.

Upvotes: 1

Views: 4849

Answers (2)

Kyle Burns
Kyle Burns

Reputation: 167

ok so, the reason i had the connection issue is because the program got stuck waiting for a response. heres the code i was using to connect:

def ConnectS(self,IntPort, StrName,E):
    self.BlnCon = False
    StrHost = socket.gethostname()
    self.StrName = StrName
    try:
        self.Server.bind((str(StrHost),int(IntPort)))
    except Exception as Error:
        print(Error)
    self.Connect = socket.socket()
    self.Server.listen(1)
    self.Connect, addr = self.Server.accept()
    self.BlnCon = True
    while self.BlnCon:
        self.RecvMsg(E)
    self.BlnCon = False
    self.Connect.close()
    self.Server.close()

if the server disconnected then the function RecvMsg() would still run. the way to fix this is to have the close method in a separate method. so you would have a Disconnect function which will just run the 2 close calls. like this:

def Handler(Btn,E,IntZ):
    E.Networking.SendMsg('Exit',E)
    E.Networking.Connect.close()
    E.Networking.Server.close()
    E.Networking.Connect = socket.socket()
    E.Networking.Server = socket.socket()

one thing to note in my code is i just added this to a button handler. and I'm not sure but i just set both my variable to socket.socket() just in case it causes issues. i also have the send method to let the other user know that the server has disconnected.

if you want i will post my networking code somewhere (probably github).

Upvotes: 0

Abd Azrad
Abd Azrad

Reputation: 311

According to MIcrosoft, WinError 10022:

Invalid argument. Some invalid argument was supplied (for example, >specifying an invalid level to the setsockopt (Windows Sockets) function). >In some instances, it also refers to the current state of the socket — for >instance, calling accept (Windows Sockets) on a socket that is not listening.

So as mike.k suggested, it does not necessarily mean an invalid argument was supplied (the error message can be misleading).

Upvotes: 1

Related Questions