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.
- setServerPort:
- serverPort
- listenAddress
- listenAddressAsString
- setListenAddress:
- setListenAddressByString:
- processMessage:orData:fromConnection:
Delegate method
- processNewConnection:
Delegate method
- processClosingConnection
Delegate method
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.
Returns a SimpleCocoaServer object with standard setup.
- (id)init
A SimpleCocoaServer object with no port and delegate set. Set to listen on all addresses.
Available in SimpleCocoaServer v0.1.1
Returns a SimpleCocoaServer object with a given port and delegate.
- (id)initWithPort:(int)initPort delegate:(id)initDelegate
The port which the server is supposed to listen on.
initDelegateThe delegate will be sent the processing messages.
A SimpleCocoaServer object, fully initialized, listening on all addresses.
For help on port numbers, please see http://www.iana.org/assignments/port-numbers.
Available in SimpleCocoaServer v0.1
Returns the Receiver's listening state as aBOOL value.
- (BOOL)isListening
Returns YES if the server is listening (running). Otherwise returns NO.
Changes to the servers listening port, address as well as delegate may be only made when it is not listening.
Available in SimpleCocoaServer v0.1
Starts the Server, if it was initialized correctly, and returns the status as a SCSInit value.
- (SCSInit)startListening
Returns a SCSInit value, see constants for details.
Available in SimpleCocoaServer v0.1
Closes all connections and stops listening for new connections.
- (void)stopListening
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.
Available in SimpleCocoaServer v0.1
Sets the port of the server, and returns a BOOL value indicating success or failure.
- (BOOL)setServerPort:(int)newPort
The port which the server is supposed to listen on.
Returns YES if the port could be changed (server not listening), otherwise NO
Available in SimpleCocoaServer v0.1
Returns the port on which the server is set up to listen.
- (int)serverPort
Returns an int value representing the port number.
Available in SimpleCocoaServer v0.1
Returns the listen address represented by a SCSListenAddress value.
- (SCSListenAddress)listenAddress
Returns a SCSListenAddress value representing the general listen address. See constants for details.
Available in SimpleCocoaServer v0.1
Returns the listen address represented by a NSString object.
- (NSString *)listenAddressAsString
Returns the IP Address the Server is listening on as a NSString object.
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 |
Available in SimpleCocoaServer v0.1.1
Sets the address of the server with one of the preset SCSListenAddress values. Returns BOOL value indicating success or failure.
- (BOOL)setListenAddress:(SCSListenAddress)newLAddr
An SCSListenAddress value representing the address which the server is supposed to listen on. See constants for details.
Returns YES if the address could be changed (server not listening), otherwise NO
Available in SimpleCocoaServer v0.1
Sets the listen address of the server to be the specified. Returns BOOL value indicating success or failure.
- (BOOL)setListenAddressByString:(NSString *)newStrAddr
An NSString object representing the address which the server is supposed to listen on in the typical dotted decimal format.
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).
Returns YES if the address could be changed (server not listening), otherwise NO
Available in SimpleCocoaServer v0.1
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
The String that has been received.
dataThe raw data as an NSData object.
The SimpleCocoaConnection object which represents the connection the message has been received from.
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.
Available in SimpleCocoaServer v1.0
If implemented, will be sent the delegate after a new connection has been successfully established.
- (void)processNewConnection:(SimpleCocoaConnection *)con
The newly established SimpleCocoaConnection object.
Example of how to use - processNewConnection:
- (void)processNewConnection:(SimpleCocoaConnection *)con { |
[server sendString:@"Welcome to SimpleCocoaServer" toConnection:con]; |
//Replies to the new client. |
Available in SimpleCocoaServer v0.1
If implemented, will be sent the delegate just before the connection will be closed.
- (void)processClosingConnection:(SimpleCocoaConnection *)con
The SimpleCocoaConnection object, that represents the connection being closed.
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". |
In v0.1 this method was called processClosedConnection:.
Available in SimpleCocoaServer v0.1.1
Returns an Array containing all connections.
- (NSArray *)connections
Returns an NSArray object containing all connections in form of SimpleCocoaConnection objects.
Available in SimpleCocoaServer v0.1
Closes the given connection.
- (void)closeConnection:(SimpleCocoaConnection *)con
The SimpleCocoaConnection object, that the connection to is supposed to be closed.
Available in SimpleCocoaServer v0.1
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
An NSData object containing what shall be sent.
SimpleCocoaConnection object representing the client the data shall be sent to.
Returns YES if the data was sent, otherwise NO
Available in SimpleCocoaServer v0.1
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
An NSString object containing the string to be sent.
SimpleCocoaConnection object representing the client the string shall be sent to.
Returns YES if the string could be sent, otherwise NO
Available in SimpleCocoaServer v0.1
Sends the given Data in form of an NSData object to all connected clients.
- (void)sendDataToAll:(NSData *)data
An NSData object containing the data to be sent.
Available in SimpleCocoaServer v0.1
Sends the given String in form of an NSString object to all connected clients.
- (void)sendStringToAll:(NSString *)string
An NSString object containing the string to be sent.
Available in SimpleCocoaServer v0.1
SimpleCocoaServer.h
Returns a SimpleCocoaConnection connected by the given file handle, waiting for data, sending it to the delegate.
- (id)initWithFileHandle:(NSFileHandle *)fh delegate:(id)initDelegate
A SimpleCocoaConnection object already connected by the given file handle and set up to listen for data.
Available in SimpleCocoaServer v0.1
Returns file handle used by the connection.
- (NSFileHandle *)fileHandle
A NSFileHandle object used by the connection.
Available in SimpleCocoaServer v0.1
Returns Address of the client.
- (NSString *)remoteAddress
A NSString object containing the IP Address of the client in dotted decimal number format.
Available in SimpleCocoaServer v0.1
Returns the Port number the client is waiting for data on.
- (int)remotePort
An Integer representing the port number the client is waiting on for messages by the server.
Available in SimpleCocoaServer v0.1
SimpleCocoaServer.h
Documentation for SimpleCocoaServer and SimpleCocoaConnection, by David J. Koster