Socket error 98 "Address already in use"

A

AK

Hi, I'm trying to make a little mp3 server / client and I'm running into
a problem with the Socket error 98 "Address already in use". The error
doesn't happen right away, I can send 3-4 commands, disconnecting and
reconnecting and they work fine and then I get this error and the client
can no longer connect, although the client side doesn't get any errors.
Here's the relevant code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
try: self.player(conn, addr)
except: pass
conn.close()
s.close()
del s


... and on client:


HOST = ''
PORT = 50025
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((HOST, PORT))


Thanks! -ak
 
A

AK

Hi, I'm trying to make a little mp3 server / client and I'm running into
a problem with the Socket error 98 "Address already in use". The error
doesn't happen right away, I can send 3-4 commands, disconnecting and
reconnecting and they work fine and then I get this error and the client
can no longer connect, although the client side doesn't get any errors.
Here's the relevant code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
try: self.player(conn, addr)
except: pass
conn.close()
s.close()
del s


.. and on client:


HOST = ''
PORT = 50025
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((HOST, PORT))


Thanks! -ak


I forgot to add that I'm on Ubuntu 9.10 and using python 2.6. -ak
 
G

Grant Edwards

Hi, I'm trying to make a little mp3 server / client and I'm running into
a problem with the Socket error 98 "Address already in use". The error
doesn't happen right away, I can send 3-4 commands, disconnecting and
reconnecting and they work fine and then I get this error and the client
can no longer connect, although the client side doesn't get any errors.
Here's the relevant code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
try: self.player(conn, addr)
except: pass
conn.close()
s.close()
del s

Always, always, always: post the traceback.

If that's your server code, I don't see how you can disconnect and
reconnect. That code only accepts a single connection, then it's done.

Also always: post minim but real code the shows the problem.
 
A

AK

Always, always, always: post the traceback.

Here it is:

Traceback (most recent call last):
File "./vimp3_player.py", line 112, in <module>
Player().main()
File "./vimp3_player.py", line 35, in main
self.listen()
File "./vimp3_player.py", line 41, in listen
s.bind((HOST, PORT))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

If that's your server code, I don't see how you can disconnect and
reconnect. That code only accepts a single connection, then it's done.

It's in a while 1: loop. self.player() just takes in a single command
and then returns.
Also always: post minim but real code the shows the problem.

It'd be a bit difficult to separate it, I was hoping this is a known
issue and this description would be enough, but if that turns out not to
be the case I'll make a minimal working example.

Thanks for the reply! -ak
 
G

Grant Edwards

Here it is:

Traceback (most recent call last):
File "./vimp3_player.py", line 112, in <module>
Player().main()
File "./vimp3_player.py", line 35, in main
self.listen()
File "./vimp3_player.py", line 41, in listen
s.bind((HOST, PORT))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

Setting the SO_REUSEADDR option should prevent that, and in my
experience it always does. What OS are you using?

Is there some reason you want to rebind each time? Why not just loop
around the conn,addr = accept() .... con.close() section?
It'd be a bit difficult to separate it, I was hoping this is a known
issue and this description would be enough, but if that turns out not to
be the case I'll make a minimal working example.

It is a known issue, but it's solved by setting the REUSEADDR option
on the socket before calling bind(). Are you sure you don't somehow
leave a server running on that port that prevents the bind() from
working?
 
A

AK

Here it is:

Traceback (most recent call last):
File "./vimp3_player.py", line 112, in <module>
Player().main()
File "./vimp3_player.py", line 35, in main
self.listen()
File "./vimp3_player.py", line 41, in listen
s.bind((HOST, PORT))
File "<string>", line 1, in bind
socket.error: [Errno 98] Address already in use

Setting the SO_REUSEADDR option should prevent that, and in my
experience it always does. What OS are you using?

Is there some reason you want to rebind each time? Why not just loop
around the conn,addr = accept() .... con.close() section?

That solved it! Thanks!@ Interesting that closing and rebinding caused
this but not right away but after 3-4 connections. My OS is Ubuntu, by
the way. Thanks again, -ak
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top