Thread: Linux port closing problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Linux port closing problem

    Hello.

    I recently wrote an application which listens on a specified port, but if I stop the application and start it again it cannot bind the socket to the same port. I have to wait some time, after the operating system frees the port. As I know this problem isn`t specific to linux.

    Can somebody tell me if there is any solution to this issue?

    Thank you.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Did you close the socket before exiting?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    To be honest no. The problem is that if I run the application the only way I can stop it is by pressing ctrl+c, and not really sure how to call the close() command before.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Post your code

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    66
    The problem is that the source code is pretty big, and I can`t post it here. Without the source code can`t you help me?

    My question is how should I execute close() before I press ctrl+c. It would be a possibility to intercept the keypress, close the socket, and than stop the application?

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by raczzoli View Post
    To be honest no. The problem is that if I run the application the only way I can stop it is by pressing ctrl+c, and not really sure how to call the close() command before.
    Trap the signal ctrl-c sends?

  7. #7
    1337
    Join Date
    Jul 2008
    Posts
    135
    setsockopt(), getsockopt()
    This could solve. See the "SO_REUSEADDR" option.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by raczzoli View Post
    To be honest no. The problem is that if I run the application the only way I can stop it is by pressing ctrl+c, and not really sure how to call the close() command before.
    Do you have access to the source code?
    If so fix the problem...
    Write an orderly shutdown routine that closes the port on the way out.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    66
    Hi.

    Thank you all for the suggestions.

    I tried the setsockopt() with SO_REUSEADDR and it works.

    Thank you again
    Last edited by raczzoli; 08-07-2011 at 08:04 AM.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I tried the setsockopt() with SO_REUSEADDR and it works.
    All you've managed to do is hide one symptom of a root problem. Fix the root problem.

    How do you know your process has even exited? Have you looked at the running processes with something like ps?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB port in linux
    By yann in forum Networking/Device Communication
    Replies: 7
    Last Post: 09-13-2009, 08:08 AM
  2. USB port in linux
    By yann in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-11-2009, 10:27 AM
  3. Closing a serial port
    By Eirik in forum Windows Programming
    Replies: 8
    Last Post: 02-13-2009, 04:11 PM
  4. Port app from Windows to Linux
    By BobS0327 in forum Linux Programming
    Replies: 12
    Last Post: 02-12-2006, 02:35 AM
  5. Closing & Stopping Port Connections
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 10-21-2001, 11:31 PM