View Full Version : Beginning iPhone socket programming

Aug 19, 2010, 11:27 AM

I am an experienced c/c++ programmer. I have a small windows mobile project that I must convert to the iPhone.

It uses normal sockets to send data over wi-fi.

struct sockaddr_in addrT;
memset(&addrT, 0, sizeof(addrT));
addrT.sin_family = AF_INET;
addrT.sin_port = htons((unsigned short)lPort);
//blah blah
phe = gethostbyname(lpszAscii);
// successfully got host address
addrT.sin_addr.s_addr = *(long *)phe->h_addr;
// connect the socket
int nRet = connect(m_socket, (PSOCKADDR)&addrT, sizeof(addrT));
//blah blah

I am only beginning iPhone Programming so I need some guidance here.
Can I use normal BSD sockets for iPhone?
If so, are there any tutorials or sample code that I can look at??


Aug 19, 2010, 04:08 PM
Can I use normal BSD sockets for iPhone?

Yes you can :)

e.g. http://discussions.apple.com/thread.jspa?messageID=7815634

Aug 19, 2010, 07:25 PM
Yes you can use ordinary BSD sockets. However...

If you use blocking API's, it's recommended that you run them in another thread, so as not to block your app's main UI thread, or convert them to use an non-blocking network API. And connect() will block.

You can also bundle a BSD socket inside an NSSocket to use it with the newer Cocoa APIs.