4

I wrote this code.

import socket
host = 'localhost'
port = 3794
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))
while 1:
 print 'Type message you want to send...'
 msg = raw_input()
 if msg == '':
 s.close()
 break
 s.sendall(msg)

and next execute this code.

Traceback (most recent call last):
 File "socket.py", line 11, in ?
 s.bind((host, port))
 File "<string>", line 1, in bind
socket.error: (99, 'Cannot assign requested address')

What's wrong?

Do you know solutions?

asked Aug 20, 2009 at 9:36
3
  • I tried the code it says: UDP sockets don't have sendall() method. Commented Aug 20, 2009 at 10:02
  • 1
    I executed that code and it runs fine (though there's no socket listening). But the indentation is wrong in 's.sendall(msg)'. Commented Aug 20, 2009 at 10:09
  • @ffffff, please show us complete code, specifically where you specify your endpoint (as by connect()). Under py2.4, this fails for me with EDESTADDRREQ ("Destination address required"). Commented Aug 20, 2009 at 13:59

3 Answers 3

9

This means that you already have a socket bound to 3794 port.

It may be another application or it means that port didn't got released yet after the previous run of your own script (it happens, if script terminated improperly).

Simply try to use another port number - I believe everything will work fine.

answered Aug 20, 2009 at 10:54
Sign up to request clarification or add additional context in comments.

Comments

4

I had this same problem and it was caused by trying to listen on the wrong host. When I changed it to an IP that was actually associated with the machine the code was running on (localhost), the problem went away.

answered Jun 3, 2011 at 1:31

Comments

0

This error comes up mostly due to the the port being already used by another application/service . Choose a port number above the range of registered ports , i.e 49151

answered Mar 26, 2013 at 14:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.