From: AK on
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
From: AK on
On 06/17/2010 07:21 PM, AK wrote:
> 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
From: Grant Edwards on
On 2010-06-17, AK <ak(a)nothere.com> wrote:
> 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.

--
Grant

From: AK on
On 06/17/2010 08:19 PM, Grant Edwards wrote:
> On 2010-06-17, AK <ak(a)nothere.com> wrote:
>> 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.

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
From: Grant Edwards on
On 2010-06-18, AK <ak(a)nothere.com> wrote:

> 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?

--
Grant