I am attempting to make a simple client and server in Python. The server seems to be completely fine but the client throws the following error:
File "C:\Users\me\Desktop\Client.py", line 6, in <module>
connection, address = client.accept()
File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 292, in accept
fd, addr = self._accept()
OSError: [WinError 10022] An invalid argument was supplied
Here is the server code:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((socket.gethostname(), 8080))
server.listen(5)
while True:
connection, address = server.accept()
fromClient = ''
while True:
data = connection.recv(2048)
if not data:
break
fromClient = fromClient + data
print(fromClient)
connection.send(str.encode('yo'))
connection.close()
print('client disconnected')
Here is the client code:
import time
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((socket.gethostname(), 8080))
client.settimeout(3)
time.sleep(3)
#client.send(str.encode('client'))
connection, address = client.accept()
fromServer = client.recv(2048)
fromServer = fromServer.decode('utf-8')
client.close()
print(fromServer)
The server binds to the ip and port so they are OK, but the client errors out when attempting the same thing. Is there anything I can do to fix this problem?
I have also tried sleeping before the client.accept() line and this has not worked.
You are invoking accept
method on client socket which is not set to passive mode by calling listen
. Moreover, the socket is already in connected state (connect
is called prior), now you cant expect your socket to listen and accept connections. But the long story short, just remove the code where you called accept
in client and it runs fine.