• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How can I tell if a port is already being used?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I tell if a port is already being used?


  • Subject: Re: How can I tell if a port is already being used?
  • From: Chris Hanson <email@hidden>
  • Date: Wed, 27 Dec 2006 16:08:34 -0800

On Dec 26, 2006, at 4:21 PM, Brian Stibi wrote:

   Does anyone know of a (simple)method for checking if a port is
already used.

There isn't one, short of actually attempting to bind to it. This is because finding out whether a port is used and using that information to determine whether or not to bind to it can lead to a race condition.


  Race condition
  http://en.wikipedia.org/wiki/Race_condition

For a simple example, let's say that your code can check to see if the port is in use. It isn't, but then a context switch occurs and another process or thread binds to the port. The next time your code is scheduled, it will believe the port is available even though it's now in use -- and you'll have to handle the "port already in use" error anyway.

Thus the only safe thing that can be done is to actually bind to the port and handle the failure. You have to handle that case anyway and you avoid the race condition.

  -- Chris

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How can I tell if a port is already being used? (From: Brian Stibi <email@hidden>)

  • Prev by Date: Re: MySQL and Cocoa - looking for samples, kits, docs, etc
  • Next by Date: Re: ffmpeg and NSTask
  • Previous by thread: How can I tell if a port is already being used?
  • Next by thread: Re: How can I tell if a port is already being used?
  • Index(es):
    • Date
    • Thread