Using NSSocketPort, NSConnection, Distributed Objects, and threads...
Using NSSocketPort, NSConnection, Distributed Objects, and threads...
- Subject: Using NSSocketPort, NSConnection, Distributed Objects, and threads...
- From: Allan Dushan <email@hidden>
- Date: Tue, 8 Nov 2005 10:20:32 -0800
When setting up a client connection to a server I am getting the
exception NSObjectInaccessibleException when I try to send a message
to the root object, which I was successfully able to obtain.
I understand that you need to send the message enableMultipleThreads:
to your NSConnection to allow threading to be handled correctly, and
I do send this message to both the server and client NSConnection
objects when I set them up, but... when I manually setup an
NSSocketPort using
initWithProtocolFamily:socketType:protocol:socket:,
enableMutlipleThreads: no longer works. If I use initWithTCPPort:,
everything works fine. So you might be asking why then don't I just
use initWithTCPPort:? Because, I need to set socket options
especially the SO_REUSEADDR option so that I don't have to wait
forever to reconnect to a server were the connection was not shut
down properly, due to the server crashing or some other issue.
When I use initWithProtocolFamily:socketType:protocol:socket: on the
server side, and connect to the server from the client through a
detached thread call, the connection is successful. But, when I try
to send a message to the root object, from the main thread of the
client, I get the NSObjectInaccessibleException. If I connect to the
server from the client without making the connection call through a
detached thread, everything works fine.
The reason I am trying to make a connection to the server through a
thread is so that the UI is still responsive, and I can display a
dialog allowing the user to cancel the connection process if it is
taking too long.
Has anyone experienced this problem, and if so, what was your
solution to fix the problem?
Below is my code snippet for vending the servers object and
establishing the client connection:
SERVER:
-------
NSSocketPort *receivePort;
NSConnection *theConnection;
int sockfd;
int flag = 1;
struct sockaddr_in serverAddress;
int namelen = sizeof(serverAddress);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = kReceivePortNumber;
fcntl( sockfd, F_SETFD, 1 );
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
bind(sockfd, (struct sockaddr *)&serverAddress, namelen);
listen(sockfd, 10);
receivePort = [[[NSSocketPort alloc] initWithProtocolFamily:PF_INET
socketType:SOCK_STREAM protocol:0 socket:sockfd] autorelease];
theConnection = [[NSConnection alloc] initWithReceivePort:receivePort
sendPort:nil];
[theConnection enableMultipleThreads];
[theConnection setRootObject:mRemoteUIMgr];
CLIENT:
-------
NSSocketPort *sendPort = nil;
NSString *hostName = nil; // IP address obtained from the user
responding to a dialog...
sendPort = [[[NSSocketPort alloc]
initRemoteWithTCPPort:kSendPortNumber host:hostName] autorelease];
mRemoteConnection = [[NSConnection alloc] initWithReceivePort:nil
sendPort:sendPort];
[mRemoteConnection enableMultipleThreads];
[mRemoteConnection setRequestTimeout:10.0];
[mRemoteConnection setReplyTimeout:15.0];
CALL FROM CLIENT THREAD:
------------------------
mServerRemoteUIMgr = [[mRemoteConnection rootProxy] retain];
Sending a message to mServerRemoteUIMgr from the main thread results
in the NSObjectInaccessibleException exception.
Thanks for the help,
Allan Dushan
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden