Benches / iPhone
9
Sockets on iPhone
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
9 replies
Sign in or join Beachball Help to reply.
0
IamMightyiPhoneDev ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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...
0
jmillers ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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?
0
IamMightyiPhoneDev ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
NSHost is available on the iPhone and works. It is handy as I can give an IP address OR a hostname in it and it returns an IP in either case...
0
jmillers ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
0
IamMightyiPhoneDev ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
sockaddr is the generalized struct for BSD sockets.
sockaddr_in is the specialized struct used for TCP/IP sockets.
http://www.beej.us/guide/bgnet/output/html/multipage/sockaddr_inman.html
0
cleverapps ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
You could you this wrapper:
http://code.google.com/p/entropydb/wiki/SocketWrapper
It supports sending/receiving objects.
0
narduk ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
I am getting an error when I try to create a socket.
{quote:title=}
// Create a socket
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
{quote}
Does the iPhone Simulator have permissions to do this?
0
narduk ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Disregard last post. I am trying to create a raw socket not a socket stream.
0
vkiller ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Hi,
Is there any way we can push out live images captured from my mac webcam to my iphone? I have a script that already captures frames from the webcam.