Hello, I am playing around with writing a program to bind to socket / port in my machine. Often time, I got the message: bind(): Address already in use
So my question is, how do I find out which process bind to a certain port number ? For example, how do I find out what process use port 2345? Sometimes 'ps ax' does not really show the process that I though bind that port number, although bind() gave the error message. Plus 'ps ax' does not really help unless you know the process name anyway.
Thanks for any help. RDB
The command lsof run as root will tell you a lot of things including who is bound to a port. For example: lsof | grep 2345
Note that on various flavors unix, I have seen the "already in use" message for a short time after a program exits. That is, start a program which listens on a certain port, exit the program, immediately restart the program. A quick test says FC4 does not seem to do this.
-- Bob Styma
Maybe it's more easy to use "netstat -tunap". The -p flag will tell you wich program is running at that port.
Cheers.
-------------------------- Gaston Martres Tech IT - Soluciones IT Cel: 15-5614-3012 http://www.tech-it.com.ar --------------------------
STYMA, ROBERT E (ROBERT) wrote:
Hello, I am playing around with writing a program to bind to socket / port in my machine. Often time, I got the message: bind(): Address already in use
So my question is, how do I find out which process bind to a certain port number ? For example, how do I find out what process use port 2345? Sometimes 'ps ax' does not really show the process that I though bind that port number, although bind() gave the error message. Plus 'ps ax' does not really help unless you know the process name anyway.
Thanks for any help. RDB
The command lsof run as root will tell you a lot of things including who is bound to a port. For example: lsof | grep 2345
Note that on various flavors unix, I have seen the "already in use" message for a short time after a program exits. That is, start a program which listens on a certain port, exit the program, immediately restart the program. A quick test says FC4 does not seem to do this.
-- Bob Styma
On Wednesday 30 November 2005 12:54, STYMA, ROBERT E (ROBERT) wrote:
Hello, I am playing around with writing a program to bind to socket / port in my machine. Often time, I got the message: bind(): Address already in use
So my question is, how do I find out which process bind to a certain port number ?
The command lsof run as root will tell you a lot of things including who is bound to a port. For example: lsof | grep 2345
Great. Thanks.
Note that on various flavors unix, I have seen the "already in use" message for a short time after a program exits. That is, start a program which listens on a certain port, exit the program, immediately restart the program. A quick test says FC4 does not seem to do this.
The reason I am asking is because I am seeing exactly this on FC 4. It seems that after exiting, the program does not release the port immediately, and hence restarting the program get the "already in use" message, while 'ps aux | grep progname' shows nothing. Maybe I am doing something wrong, ie. the program does not release the port cleanly on exit or something like that ? I'm not sure...
Thanks a lot for the answer though . RDB
On Wed, 2005-11-30 at 13:34 -0500, Reuben D. Budiardja wrote:
On Wednesday 30 November 2005 12:54, STYMA, ROBERT E (ROBERT) wrote:
Hello, I am playing around with writing a program to bind to socket / port in my machine. Often time, I got the message: bind(): Address already in use
So my question is, how do I find out which process bind to a certain port number ?
The command lsof run as root will tell you a lot of things including who is bound to a port. For example: lsof | grep 2345
Great. Thanks.
Note that on various flavors unix, I have seen the "already in use" message for a short time after a program exits. That is, start a program which listens on a certain port, exit the program, immediately restart the program. A quick test says FC4 does not seem to do this.
The reason I am asking is because I am seeing exactly this on FC 4. It seems that after exiting, the program does not release the port immediately, and hence restarting the program get the "already in use" message, while 'ps aux | grep progname' shows nothing. Maybe I am doing something wrong, ie. the program does not release the port cleanly on exit or something like that ? I'm not sure...
Is this an RPC client-server program? If it is, it's done intentionally so that any messages that may still float around have a destination to rest in peace.
Use "select()" socket utility. It may mean a little extra programming, but it is really rewarding.
On Wednesday 30 November 2005 14:14, Ezra Nugroho wrote:
On Wed, 2005-11-30 at 13:34 -0500, Reuben D. Budiardja wrote:
On Wednesday 30 November 2005 12:54, STYMA, ROBERT E (ROBERT) wrote:
Hello, I am playing around with writing a program to bind to socket / port in my machine. Often time, I got the message: bind(): Address already in use
<snip>
Note that on various flavors unix, I have seen the "already in use" message for a short time after a program exits. That is, start a program which listens on a certain port, exit the program, immediately restart the program. A quick test says FC4 does not seem to do this.
The reason I am asking is because I am seeing exactly this on FC 4.<snip>
| grep progname' shows nothing. Maybe I am doing something wrong, ie. the
program does not release the port cleanly on exit or something like that ? I'm not sure...
Is this an RPC client-server program? If it is, it's done intentionally so that any messages that may still float around have a destination to rest in peace.
No RPC (if I understand what that means correctly), just 'regular' socket programming.
Use "select()" socket utility. It may mean a little extra programming, but it is really rewarding.
Not sure how select() would help here. In this case the "server" side just do really simple stuff, basically read and write to socket descriptor. It could be that when server is interupted, there is still I/O that makes it not immediately release the port. Are you saying using select() to clean up ?
RDB
<snip>
Is this an RPC client-server program? If it is, it's done intentionally so that any messages that may still float around have a destination to rest in peace.
No RPC (if I understand what that means correctly), just 'regular' socket programming.
Use "select()" socket utility. It may mean a little extra programming, but it is really rewarding.
Not sure how select() would help here. In this case the "server" side just do really simple stuff, basically read and write to socket descriptor. It could be that when server is interupted, there is still I/O that makes it not immediately release the port. Are you saying using select() to clean up ?
You are right, you don't need select() if you just want to clean up. You might want to consider it since it gracefully manages resources when there is some network error/interrupt. Look at the chat server example: http://beej.us/guide/bgnet/output/html/advanced.html#select
This is how you clean up the socket.
// lose the pesky "address already in use" error message if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); }
On Wednesday 30 November 2005 20:36, Ezra Nugroho wrote:
<snip>
You are right, you don't need select() if you just want to clean up. You might want to consider it since it gracefully manages resources when there is some network error/interrupt. Look at the chat server example: http://beej.us/guide/bgnet/output/html/advanced.html#select
Nice page. Thanks for the pointer.
RDB