Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
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?



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



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.