• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reading a string from a socket
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading a string from a socket


  • Subject: Re: Reading a string from a socket
  • From: Daniel Smith <email@hidden>
  • Date: Fri, 18 Jan 2008 13:07:51 -0600

I'm looking for a simple way to read a string from a specific TCP host & port. I've implemented the server, and it works correctly when I access it via telnet, and now I'm trying to implement a simple client.

Here's my current code:
NSSocketPort* socket = [[[NSSocketPort alloc] initRemoteWithTCPPort:4777 host: @"127.0.0.1"] autorelease];
if (socket == nil) { NSLog(@"Unable to create NSSocketPort"); }
else if ([socket socket] == -1) { NSLog(@"NSSocketPort contains invalid file descriptor"); }
else {
NSFileHandle* file = [[[NSFileHandle alloc] initWithFileDescriptor:[socket socket] closeOnDealloc:YES] autorelease];
NSString message = [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUnicodeStringEncoding];
...
}


I found an online discussion somewhere suggesting something like this would work, but unfortunately I find that, invariably, "[socket socket]" evaluates to -1. (I was confused for awhile that when I used a bad host name, "[socket socket]" would evaluate to 0, which represents STDIN. Then I noticed that "socket" was nil -- hence the additional check. Note that the init documentation doesn't bother to mention that the result may be nil.)

I believe this line from the init documentation describes the problem with my code: "A connection is not opened to the remote host until data is sent." I think that means that "[socket socket]" won't given me a meaningful result until I send something. Unfortunately, I don't know how to send something until I can get a meaningful result...

Any suggestions? Can I make this code work? Or should I be trying a different approach (note that I don't want to change the server in any way)? Perhaps I'd be better off using lower-level APIs?

Thanks,
Dan

_______________________________________________

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


  • Follow-Ups:
    • Re: Reading a string from a socket
      • From: "Hamish Allan" <email@hidden>
  • Prev by Date: Re: Starting an alert sheet from an NSWindowController that owns a user-editable sheet
  • Next by Date: Re: Cursor duplication problem
  • Previous by thread: Re: NSATSTypeSetter exception
  • Next by thread: Re: Reading a string from a socket
  • Index(es):
    • Date
    • Thread