Python: Binding Socket: "Address Already in Use"

Python: Binding Socket: Address already in use

Try using the SO_REUSEADDR socket option before binding the socket.

comSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Edit:
I see you're still having trouble with this. There is a case where SO_REUSEADDR won't work. If you try to bind a socket and reconnect to the same destination (with SO_REUSEADDR enabled), then TIME_WAIT will still be in effect. It will however allow you to connect to a different host:port.

A couple of solutions come to mind. You can either continue retrying until you can gain a connection again. Or if the client initiates the closing of the socket (not the server), then it should magically work.

Socket error: Address already in use

You can try the following

from socket import *

sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# then bind

From the docs:

The SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.

Here's the complete explanation:

Running an example several times with too small delay between executions, could lead to this error:

socket.error: [Errno 98] Address already in use

This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.

There is a socket flag to set, in order to prevent this, socket.SO_REUSEADDR:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))

Python Binding Socket: “Address already in use”, after closing socket

Closing a socket just releases the handle to any underlying connection. It can still take the implementation some amount of time to complete the orderly shutdown of the connection and, during that time, the address is still in use.

For example, if you have an active connection and the other side isn't reading from it, the implementation will give it time to read the data that was sent. During that time, the address is still in use.

Address already in use when restarting socket

You should tell to reuse the socket as it is marked as used until the end of you process.

You can do this easily by adding this after creating your socket and before the bind:

self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

You can find a full explanation in this comment

Python socket programming: Address already in use after exception

Set SO_REUSEADDR socket option before binding the socket.

the SO_REUSEADDR flag tells the kernel to reuse a local socket in
TIME_WAIT state, without waiting for its natural timeout to expire.

from socket documentation


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 5005))
...

NOTE: You will get an error even if you apply this, if there's TIME_WAIT socket is remained from the previous run (without the SO_REUSEADDR option).

Python socket bind reports port already in used but socket connect_ex return 0

I think there is a misunderstanding. The return of the function is 0 when the connection is successful so that the port is accessible.

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("test using connect_ex:")
print(s.connect_ex(("127.0.0.1", 3389)))

test using connect_ex:
0 # <- the port is busy because the connection success
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("test using connect_ex:")
print(s.connect_ex(("127.0.0.1", 1234)))

test using connect_ex:
61 # <- [Errno 61] Connection refused (the port is free)


Related Topics



Leave a reply



Submit