Re: How can I tell if a port is already being used?
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