Re: Network communication with NSFileHandle & NSSocketPort
Re: Network communication with NSFileHandle & NSSocketPort
- Subject: Re: Network communication with NSFileHandle & NSSocketPort
- From: "Gary L. Wade" <email@hidden>
- Date: Wed, 19 Mar 2008 17:00:52 -0400
Well, this code works for me whether I have an IP address or domain name in the string:
addr.sin_addr.s_addr = inet_addr (theCStringInternetAddress);
if (INADDR_NONE == addr.sin_addr.s_addr)
{
struct hostent* theHostInfo;
theHostInfo = gethostbyname (theCStringInternetAddress);
if (NULL != theHostInfo)
{
addr.sin_family = theHostInfo->h_addrtype;
memcpy (&addr.sin_addr.s_addr,
theHostInfo->h_addr,
theHostInfo->h_length);
}
}
>
>On Mar 19, 2008, at 4:31 PM, Sherm Pendley wrote:
>
>> Looks reasonable. Now if I could only figure out why I forgot about
>> NSSocketPort...
>>
>> I think maybe I need to write a network app to jog my memory.
>> Obviously I haven't looked at these classes recently enough. :-)
>
>I couldn't get it to work. Apple's documentation makes it sound like
>NSSocketPort can work for general network sockets, but a little
>Googling led me to some old postings that say it was really only ever
>designed for use with DO and doesn't work as advertised as a full-
>service socket wrapper.
>
>I did get the process as you described working fine in a little test
>app, except for one thing - I can only get it to work if I provide the
>address as dot separated octets, like:
>
> NSString *address = @"66.250.146.128";
>
> struct sockaddr_in addr;
> bzero( &addr, sizeof(struct sockaddr_in));
> addr.sin_family = AF_INET;
> addr.sin_addr.s_addr = inet_addr([address UTF8String]);
> addr.sin_port = htons( 119 );
>
> int fd = socket( AF_INET, SOCK_STREAM, 0 );
>
>
> if ( connect( fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 )
> {
> NSLog(@"Error connecting");
> return;
> }
>
> self.fh =[[[NSFileHandle alloc] initWithFileDescriptor:fd]
>autorelease];
> [fh writeData:[@"LIST NEWSGROUPS\r\n" dataUsingEncoding:
>NSUTF8StringEncoding]];
> [fh readInBackgroundAndNotify];
> NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
> [nc addObserver:self selector:@selector(finish:)
>name:NSFileHandleReadToEndOfFileCompletionNotification object:fh];
> [nc addObserver:self selector:@selector(process:)
>name:NSFileHandleReadCompletionNotification object:fh];
>
>But I'm darned if I can figure out how to get it to work with a
>hostname (like @"apple.com"). I've tried using gethostbyname and a few
>other things I've found on the web, but nothing seems to work except
>using the octet string, although I'm sure that's just because I
>haven't worked with sockets in years and am forgetting something
>really stupid. :)
>
>Thanks for all the help.
>Jeff
>_______________________________________________
>
>Cocoa-dev mailing list (email@hidden)
>
>Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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