import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 8021))
s.listen(10)
while True:
conn, addr = s.accept()
print 'got connected from', addr
s.send("sending to client")
conn.close()
s.close()
The problem is as soon i run my client code, it shows "got connected from ('127.0.0.1', 52764)" but then it shows the error
Traceback (most recent call last):
File "D:\Python27\server", line 13, in <module>
s.send("sending to client")
error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
How can I correct it?
My client code is:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 8021
s.connect((host, port))
revieved = s.recv(1024)
print "Recieved: ", recieved
-
Is there anything unclear with my answer, or any other reason why you didn't mark it as accepted?Aida Paul– Aida Paul2014年10月28日 14:46:03 +00:00Commented Oct 28, 2014 at 14:46
1 Answer 1
Well, the answer is simple and I may snag some reputation here!
You are confusing your general socket with the specific client connection. What you receive from running s.accept() is a tuple which consists of: remote socket connection object and remote socket address. This way you can speak to specific client, by referring to the right connection object.
So the fixed could looks like this:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 8021))
s.listen(10)
while True:
conn, addr = s.accept()
print 'got connected from', addr
conn.send("sending to client")
conn.close()
s.close()
Assuming that everything else is working fine!
Comments
Explore related questions
See similar questions with these tags.