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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to