Python socket error occurred

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 execute the following 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 the solutions?

+4
source share
3 answers

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

This may be another application, or it means that the port has not yet been released after the previous launch of your own script (this happens if the script did not complete correctly).

Just try using a different port number - I believe that everything will work fine.

+9
source

I had the same problem and it was caused by trying to listen on the wrong host. When I changed it to IP, which was actually connected to the machine on which the code was running (localhost), the problem disappeared.

+4
source

This error occurs mainly because the port is already in use by another application / service. Select a port number above the range of registered ports, i.e. 49151

0
source

All Articles