SimpleCocoaServer

SimpleCocoaServer is a basic server class. It makes it possible to run a server with basic setup options such as port and listening address. The server needs a delegate in order to function as it will be sent messages when a client has connected, disconnected or sent a message.

Tasks

Creating a SimpleCocoaServer Object

Initializing a SimpleCocoaServer Object

Controlling the Server

Accessing Variables

Processing methods

Handling connections

Sending Data


SimpleCocoaConnection

A SimpleCocoaConnection object holds information about the client as well as the file handle it uses to communicate. The delegate is notified when data has been received by the connetion and it will be sent messages in order to close the connection. Therefore the delegate is usually the server.

Tasks

Creating a SimpleCocoaConnection Object

Initializing a SimpleCocoaConnection Object

Getting Information about a Connection


Class Methods

Instance Methods

init

Returns a SimpleCocoaServer object with standard setup.

- (id)init

Return Value

A SimpleCocoaServer object with no port and delegate set. Set to listen on all addresses.

Availability

Available in SimpleCocoaServer v0.1.1

Declared In

SimpleCocoaServer.h

initWithPort:delegate:

Returns a SimpleCocoaServer object with a given port and delegate.

- (id)initWithPort:(int)initPort delegate:(id)initDelegate

Parameters
initPort

The port which the server is supposed to listen on.

initDelegate

The delegate will be sent the processing messages.

Return Value

A SimpleCocoaServer object, fully initialized, listening on all addresses.

Discussion

For help on port numbers, please see http://www.iana.org/assignments/port-numbers.

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

isListening

Returns the Receiver's listening state as aBOOL value.

- (BOOL)isListening

Return Value

Returns YES if the server is listening (running). Otherwise returns NO.

Special Considerations

Changes to the servers listening port, address as well as delegate may be only made when it is not listening.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

startListening

Starts the Server, if it was initialized correctly, and returns the status as a SCSInit value.

- (SCSInit)startListening

Return Value

Returns a SCSInit value, see constants for details.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

stopListening

Closes all connections and stops listening for new connections.

- (void)stopListening

Discussion

The given port number is freed and may now be used by another program. If another program binds to the address and port this server has been listening on, SCSInitError_Bind will be returned when - startListening is called again.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

setServerPort:

Sets the port of the server, and returns a BOOL value indicating success or failure.

- (BOOL)setServerPort:(int)newPort

Parameters
newPort

The port which the server is supposed to listen on.

Return Value

Returns YES if the port could be changed (server not listening), otherwise NO

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

serverPort

Returns the port on which the server is set up to listen.

- (int)serverPort

Return Value

Returns an int value representing the port number.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

listenAddress

Returns the listen address represented by a SCSListenAddress value.

- (SCSListenAddress)listenAddress

Return Value

Returns a SCSListenAddress value representing the general listen address. See constants for details.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

listenAddressAsString

Returns the listen address represented by a NSString object.

- (NSString *)listenAddressAsString

Return Value

Returns the IP Address the Server is listening on as a NSString object.

Examples

Examples of setups and results of calling listenAddressAsString as it would be in e.g. an AppController.

SimpleCocoaServer *server = [[SimpleCocoaServer alloc] initWithPort:55599 delegate:self];
NSString *addr = [server listenAddressAsString]; //returns 0.0.0.0
SimpleCocoaServer *server = [[SimpleCocoaServer alloc] initWithPort:60012 delegate:self];
[server setListenAddress:SCSListenLoopback];
NSString *addr = [server listenAddressAsString]; //returns 127.0.0.1
SimpleCocoaServer *server = [[SimpleCocoaServer alloc] initWithPort:51253 delegate:self];
[server setListenAddressByString:@"192.168.2.101"];
NSString *addr = [server listenAddressAsString]; //returns 192.168.2.101
Availability

Available in SimpleCocoaServer v0.1.1

Declared In

SimpleCocoaServer.h

setListenAddress:

Sets the address of the server with one of the preset SCSListenAddress values. Returns BOOL value indicating success or failure.

- (BOOL)setListenAddress:(SCSListenAddress)newLAddr

Parameters
newLAddr

An SCSListenAddress value representing the address which the server is supposed to listen on. See constants for details.

Return Value

Returns YES if the address could be changed (server not listening), otherwise NO

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

setListenAddressByString:

Sets the listen address of the server to be the specified. Returns BOOL value indicating success or failure.

- (BOOL)setListenAddressByString:(NSString *)newStrAddr

Parameters
newStrAddr

An NSString object representing the address which the server is supposed to listen on in the typical dotted decimal format.

Examples

Example of setting up the server to listen only to the address given by the local network.

[server setListenAddressByString:@"192.168.123.110"];

The server will now not even handle request on the loopback address (127.0.0.1).

Return Value

Returns YES if the address could be changed (server not listening), otherwise NO

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

processMessage:orData:fromConnection:

Sent to the delegate after data has been received by one of the servers connections.

- (void)processMessage:(NSString *)message orData:(NSData *)data fromConnection:(SimpleCocoaConnection *)con

Parameters
message

The String that has been received.

data

The raw data as an NSData object.

con

The SimpleCocoaConnection object which represents the connection the message has been received from.

Discussion

If the ONLY the old processMessage:fromConnection: is implemented by the delegate and not this method, the old processMessage:fromConnection: will be called. Otherwise this methods is called.

Availability

Available in SimpleCocoaServer v1.0

Declared In

SimpleCocoaServer.h

processNewConnection:

If implemented, will be sent the delegate after a new connection has been successfully established.

- (void)processNewConnection:(SimpleCocoaConnection *)con

Parameters
con

The newly established SimpleCocoaConnection object.

Examples

Example of how to use - processNewConnection:

- (void)processNewConnection:(SimpleCocoaConnection *)con {
    [server sendString:@"Welcome to SimpleCocoaServer" toConnection:con];
    //Replies to the new client.
Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

processClosingConnection:

If implemented, will be sent the delegate just before the connection will be closed.

- (void)processClosingConnection:(SimpleCocoaConnection *)con

Parameters
con

The SimpleCocoaConnection object, that represents the connection being closed.

Examples

Example of how to use - processClosingConnection:

- (void)processClosingConnection:(SimpleCocoaConnection *)con {
    NSLog(@"Connection to %@ is being closed", con);
    //Logs something like "Connetion to 127.0.0.1:6378 is being closed".
Discussion

In v0.1 this method was called processClosedConnection:.

Availability

Available in SimpleCocoaServer v0.1.1

See also
Declared In

SimpleCocoaServer.h

connections

Returns an Array containing all connections.

- (NSArray *)connections

Return Value

Returns an NSArray object containing all connections in form of SimpleCocoaConnection objects.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

closeConnection:

Closes the given connection.

- (void)closeConnection:(SimpleCocoaConnection *)con

Parameters
con

The SimpleCocoaConnection object, that the connection to is supposed to be closed.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

sendData:toConnection:

Sends data in form of an NSData object to the given connection. Returns BOOL value indicating success or failure.

- (BOOL)sendData:(NSData *)data toConnection:(SimpleCocoaConnection *)con

Parameters
data

An NSData object containing what shall be sent.

conThe SimpleCocoaConnection object representing the client the data shall be sent to.

Return Value

Returns YES if the data was sent, otherwise NO

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

sendString:toConnection:

Sends a String of an NSData object to the given connection. Returns BOOL value indicating success or failure.

- (BOOL)sendString:(NSString *)string toConnection:(SimpleCocoaConnection *)con

Parameters
string

An NSString object containing the string to be sent.

conThe SimpleCocoaConnection object representing the client the string shall be sent to.

Return Value

Returns YES if the string could be sent, otherwise NO

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

sendDataToAll:

Sends the given Data in form of an NSData object to all connected clients.

- (void)sendDataToAll:(NSData *)data

Parameters
data

An NSData object containing the data to be sent.

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h

sendStringToAll:

Sends the given String in form of an NSString object to all connected clients.

- (void)sendStringToAll:(NSString *)string

Parameters
string

An NSString object containing the string to be sent.

Availability

Available in SimpleCocoaServer v0.1

See also
Declared In

SimpleCocoaServer.h


Class Methods

Instance Methods

initWithFileHandle:delegate:

Returns a SimpleCocoaConnection connected by the given file handle, waiting for data, sending it to the delegate.

- (id)initWithFileHandle:(NSFileHandle *)fh delegate:(id)initDelegate

Return Value

A SimpleCocoaConnection object already connected by the given file handle and set up to listen for data.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

fileHandle

Returns file handle used by the connection.

- (NSFileHandle *)fileHandle

Return Value

A NSFileHandle object used by the connection.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

remoteAddress

Returns Address of the client.

- (NSString *)remoteAddress

Return Value

A NSString object containing the IP Address of the client in dotted decimal number format.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h

remotePort

Returns the Port number the client is waiting for data on.

- (int)remotePort

Return Value

An Integer representing the port number the client is waiting on for messages by the server.

Availability

Available in SimpleCocoaServer v0.1

Declared In

SimpleCocoaServer.h


Documentation for SimpleCocoaServer and SimpleCocoaConnection, by David J. Koster

http://sourceforge.net/projects/simpleserver/

http://www.1-more-thing.de/code/simpleserver/