dijxtra
dijxtra

Reputation: 2751

who uses a TCP port?

One of gen_servers in my app call gen_tcp:listen(Port, [{active, true}]). First time I run unit test, it returns {ok, Socket}, but second time I run the same unit test, it returns an {error, eaddrinuse}, but

lsof -i TCP

returns nothing. Also, when the same unit_test is run twice on another machine (WinXP), it works as expected (that is, returns {ok, Socket} both times). Therefore, my gen_server obviously releases the port, but Erlang somehow doesn't know that.

So, how can I figure out who does Erlang think uses this address?

Upvotes: 6

Views: 1434

Answers (2)

antlersoft
antlersoft

Reputation: 14786

This is because of details of the implementation of TCP on Unix systems-- when a socket is opened for listening, it will stay unavailable for a few minutes in the CLOSE_WAIT state after the listening process shuts down.

From Lukas' comment above: you can use reuseaddr flag to gen_tcp:listen to avoid this

Upvotes: 9

Bastian
Bastian

Reputation: 10433

if you are on windows you can use the netstat utility to find out which process has which port opened:

http://commandwindows.com/netstat.htm

netstat -a -b -v should do the trick

Linux netstat also supports showing the user, but you need root right for that.

Upvotes: 0

Related Questions