Sockets on iPhone

I need to setup some fairly basic socket communication in my iPhone application and was hoping to get a little help.

I looked at the WiTap example, but it seems way more complicated then I thought it needed to be.

I have a defined IP address and port that I want to send and receive communications on.
Anyone have any pointers or fairly simple sample code that I could reference?

I'm looking through the CFSocketCreate, etc. methods, but don't see how I am supposed to package up my defined IP address and port for use?

Thanks.

Mac Book Pro, Mac OS X (10.5.4)

Posted on Aug 1, 2008 1:19 PM

Reply
9 replies

Aug 1, 2008 1:23 PM in response to jmillers

I've got some fairly simple code pushing out some text via TCP:


- (void)sendcmd:(NSString*)cmd {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *hostname= [defaults stringForKey:@"hostname"];

NSHost *host=[NSHost hostWithName:hostname];

if (host) {

struct sockaddr_in addr;
int sockfd;
// Create a socket
sockfd = socket( AF_INET, SOCK_STREAM, 0 );


addr.sin_family = AF_INET;
addr.sinaddr.saddr = inet_addr([[host address] UTF8String]);
addr.sin_port = htons( 2001 );
int conn = connect(sockfd, &addr, sizeof(addr));

if (!conn) {

NSData* data = [cmd dataUsingEncoding:NSISOLatin1StringEncoding];

ssize_t datasend = send(sockfd, [data bytes], [data length], 0);
datasend++;


//ssize_t send(int, const void *, size_t, int) _DARWIN_ALIASC(send);


close(sockfd);
} else {
// create a popup here!


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[@"Connection failed to host " stringByAppendingString:hostname] message:@"Please check the hostname in the preferences." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}

} else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[@"Could not look up host " stringByAppendingString:hostname] message:@"Please check the hostname in the preferences." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}

}


The hostname I get from my settings bundle, the port I define here...

Aug 1, 2008 1:33 PM in response to jmillers

Thanks, that helps a lot!

I can see how you are packaging up the address and port.
I don't need to use NSHost, but I don't think that the NSHost class is supported on the device. Does your code work on an actual device?

In my case, I need to leave the socket open and receive data on it as well.
Do you have any sample code to set that up with the appropriate callbacks?

Aug 1, 2008 2:11 PM in response to jmillers

That's cool. It doesn't look like it is supported in the documentation, but if it works that is great.

What is the difference between sockaddr and sockaddr_in?

The WiTap example used sockaddr for it's socket communication, but that structure is different than the sockaddr_in that you are using.

And is there any documentation available on these structures, how to pack addresses into them, etc?

Thanks again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Sockets on iPhone

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.