Re: NSSocketPort initRemoteWithTCPPort is never valid
Re: NSSocketPort initRemoteWithTCPPort is never valid
- Subject: Re: NSSocketPort initRemoteWithTCPPort is never valid
- From: Ken Thomases <email@hidden>
- Date: Wed, 15 Jul 2009 01:36:10 -0500
On Jul 14, 2009, at 1:40 AM, Christopher J Kemsley wrote:
This is just a quick question for anyone who may know (or, at least
I hope it's quick)
If I declare a port:
port = [ [NSSocketPort alloc] initRemoteWithTCPPort:portNumber
host:hostName ] ;
And use it with an NSConnection, it works.
If I use that same port in any other way (such as NSFileHandle) it
doesn't work. The error it gives is:
[NSConcreteFileHandle writeData:]: Bad file descriptor
(when I use initWithFileDescriptor:port.socket )
An NSSocketPort is intended for use with Distributed Objects and
NSConnection or, at least, to communicate with another NSSocketPort at
the other end. You would use the NSPortMessage class for sending data
in the latter case.
I don't think it's suitable for creating an arbitrary socket
connection to a remote port.
The particular problem you're encountering is probably due to this
sentence in the documentation of -initRemoteWithTCPPort:host:
"A connection is not opened to the remote host until data is sent."
It's quite possible that the socket file descriptor isn't even
allocated until then, either. Even if the file descriptor has been
created, connect() hasn't been called at the point where you're trying
to init your NSFileHandle object with it. And since you're trying to
use the file descriptor rather than the NSSocketPort object from that
point on, there's no opportunity for NSSocketPort to establish the
connection. It can't very well hook into the system calls and
magically connect the socket when something tries to write through the
file descriptor.
In other words, what you're trying to do isn't a supported usage
pattern.
Ideally, I'd like to be able to use NSFileHandle (or something very
similar) on both ends of the connection.
Any thoughts/suggestions?
Look into using streams (NSStream and friends). In particular, +
[NSStream getStreamsToHost:port:inputStream:outputStream:].
http://developer.apple.com/documentation/Cocoa/Conceptual/Streams/
Cheers,
Ken
_______________________________________________
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