1
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
1
  • Is there anything unclear with my answer, or any other reason why you didn't mark it as accepted? Commented Oct 28, 2014 at 14:46

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.