windows - python socket: winerror 10056 -
i've read throught introduction python sockets: http://docs.python.org/3.3/howto/sockets.html
this server
import socket serversocket=socket.socket(socket.af_inet,socket.sock_stream) serversocket.bind(("localhost",8000)) serversocket.listen(5) while true: (client,addr)=serversocket.accept() data=serversocket.recv(1024) print(data.decode("utf-8"))
and client
import socket s=socket.socket(socket.af_inet, socket.sock_stream) s.connect(("localhost",8000))
the idea server prints data sent client. can see, intended encode message strings bytes utf-8. never came far.
with server script running, typed client lines 1 one idle python shell. after third line, error prompted up. since i'm german, vague translation. error message sound different if can reproduce error
traceback (most recent call last): file "", line 1, in s.connect(("localhost",8000)) oserror: [winerror 10056] connection attempt targeted connected socket.
how can solve error ? server adapted, client exact code tutorial. , error seens rather strange, after want socket connected - server . @ first thought somehow there sockets connected server restarting , typing client code again lead same result.
you want receive on client socket, , close client socket when client closes. process 1 client @ time, note needs message protocol implemented know has complete message decode:
import socket serversocket = socket.socket() serversocket.bind(('',8000)) serversocket.listen(5) while true: client,addr = serversocket.accept() while true: data = client.recv(1024) if not data: break print(data.decode('utf8') # note might not contain complete utf-8 char. client.close()
Comments
Post a Comment