Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 27, 2008, 01:10 PM   #1
Xino
macrumors member
 
Join Date: Sep 2008
How to check if a port is open?

How can I check if a port of a given host is open?

I've searched the developer reference but couldn't find anything useful...
Application is written in Objective-C / Cocoa.

Thanks for your help
Xino is offline   0 Reply With Quote
Old Oct 27, 2008, 01:45 PM   #2
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
You will have to try connecting to the remote address and port (I'm assuming you want to connect to a remote TCP Server).

At the lowest level BSD sockets C API, the connect() call returns a -1 on failure and sets the global errno variable to ECONNREFUSED when no one is listening on the remote address.

NSStream and CFSocket will return an error code or error object, but although these may be enough to say that the connection failed, I'm not sure they'll tell you why it failed.

The good news is you can assume that both Cocoa and Core Foundation use BSD sockets underneath the hood and so you can check the errno variable after a failed connection attempt as you would had you written this using the sockets API versus Cocoa/CoreFoundatation.

(errno should be thread-safe on OS-X)

or maybe NSError will translate errno for you:

Handling Stream Errors

try it and find out
__________________
all Jarvis, all the time

Last edited by SilentPanda; Jul 27, 2009 at 06:39 AM. Reason: post merge
yeroen is offline   0 Reply With Quote
Old Oct 27, 2008, 07:21 PM   #3
ChrisA
macrumors G4
 
Join Date: Jan 2006
Location: Redondo Beach, California
[QUOTE=Xino;6508541]How can I check if a port of a given host is open?

I've searched the developer reference but couldn't find anything useful...
Application is written in Objective-C / Cocoa.

Thanks for your help[/QUOTE

From the terminal the "telnet" works well. For example I know port 80 is open on my computer. I can test this

telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.

but I know port 81 is not
telnet localhost 81
Trying ::1...
telnet: connect to address ::1: Connection refused


From C. Simply try to connect() and see what you get. read the "connect"
man page. The man page tells you about the return codes returned by
connect(). if you get ECONNREFUSED then the port is not open.
ChrisA is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
/Is it possible to port forward to my laptop to test if a port is open infobleep OS X 10.8 Mountain Lion 5 Aug 11, 2013 03:15 PM
How can I open port 80 manually on OS X ? XPcentric Mac Programming 2 May 26, 2013 02:10 PM
Port 443 is open oldhifi Mac OS X Server, Xserve, and Networking 18 Apr 18, 2013 01:41 PM
Open Port in Airport Utility 6 prnoct90 Mac Peripherals 0 Jan 22, 2013 07:31 AM
How to find an open UDP port (ShareMouse App) zonatigre Mac Basics and Help 0 Jun 7, 2012 05:18 PM

Forum Jump

All times are GMT -5. The time now is 08:55 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC