Edit AllPages

Example code for server and client threads communication using DistributedObjects.

The code is based on Apple article ( and DistributedObjectsForInterThreadCommsCrash.



//// Server.h


Creating a server thread

Client call [Server connectionToServerThreadForClient:self] to create a server thread and get back a connection to the thread. When the thread is ready, it will call the client setServer: method. After the client set the server, it can communicate with the server over DO.

Destroying a server thread

Client call [server terminate], then release the server as usual.


@protocol Server

// Terminate the server thread

// Add other methods that servers must have


@protocol Client

// Called from the server when its ready

// Add other methods that clients must have


@interface Server : NSObject { BOOL running; }

// Create new thread and get back a connection the thread

// Private method used only by the Server class to connect to a new created thread


//// Server.m

@implementation Server


//// Client.h

#import Server.h // for Client protocol

@interface Client : [[NSObject { id server; NSConnection *serverConnection; } @end

//// Client.m

@implementation Client