- Inherits from:
- Object
- Declared in:
- DTCPServer.h
Object
|
+---DTCPServer
Class Description
The DTCPServer class implements a number of methods for implementing a TCP server.
(Note: on WIN32 DTS_FORKING is not possible; when DTS_FORKING is used, it is switched
to DTS_THREADING)(Note: this class assumes that the socket is blocking.)(Note: on
FreeBSD DTS_THREADING results in errors; when DTS_THREADING is used, it is
switched to DTS_FORKING)
- Example:
#include <stdio.h>
#include "ofc/DTCPServer.h"
#include "ofc/DSocketAddress.h"
@interface MyServer : DTCPServer
{
}
- (BOOL) handleRequest :(DData *) request :(DData *) response;
@end
@implementation MyServer
- (BOOL) handleRequest :(DData *) request :(DData *) response
{
DText *str;
str = [request toText]; // Print the response
printf("Server receives the request:%s\n", [str cstring]);
[str free];
printf(".. and sends: Nice weather.\n");
[response set :"Nice weather" :12]; // Send the response
return YES; // Example stop the server
}
@end
int main(int argc, char *argv[])
{
MyServer *srvr = [MyServer new];
DInetSocketAddress *addr = [DInetSocketAddress new];
[addr any :7000]; // Setup the address for the server
printf("Open the server..\n");
if ([srvr open :[addr family] :[DSocket protocol :"tcp"]])
{
printf("Start the server (and wait for client to connect)..\n");
if (![srvr start :addr :2 :DTS_BLOCKING :1])
printf("Could not start the server ..\n");
}
else
printf("Could not open the server ..\n");
[srvr free]; // Cleanup
[addr free];
return 0;
}
- Last modified:
- 11-Aug-2008 (DTCPServer.h)
Instance Variables
- private DSocket *_local
- the local socket
- private int _mode
- the running mode (DTS_THREADING, DTS_FORKING, DTS_BLOCKING)
- private int _sendFlag
- the send flag (def. 0)
- private int _recvFlag
- the receive flag (def. 0)
- private unsigned _recvLength
- the length used during receiving of requests (def. 1024)
- Constructors
- - (DTCPServer *) init
- Initialise the Tcp Server
- Returns:
- the object
- - (DTCPServer *) init :(int) family :(int) protocol
- Initialise the Tcp Server for a family and protocol
- Parameters:
- family - the family (DSA_AF_INET, DSA_AF_UNIX, DSA_AF_INET6)
protocol - the protocol to be used
- Returns:
- the object
- Deconstructor
- - free
- Free the tcp server object
- Returns:
- the object
- Copy related methods
- - shallowCopy
- Do a shallow copy of the object (not implemented)
- Member methods
- - (int) recvFlag
- Return the receive flag
- Returns:
- the receive flag
- - (DTCPServer *) recvFlag :(int) flag
- Set the receive flag
- Parameters:
- flag - the receive flag
- Returns:
- the object
- - (unsigned) recvLength
- Return the length for receiving of requests
- Returns:
- the length
- - (DTCPServer *) recvLength :(unsigned) length
- Set the length for receiving of requests
- Parameters:
- length - the length
- Returns:
- the object
- - (int) sendFlag
- Return the send flag
- Returns:
- the send flag
- - (DTCPServer *) sendFlag :(int) flag
- Set the send flag
- Parameters:
- flag - the send flag
- Returns:
- the object
- - (DSocket *) socket
- Return the local socket for the TCP server
- Returns:
- a reference to the local socket
- Main methods
- - (void) handleConnection :(DSocket *) peer
- Handle a new connection with a peer (called by start)
- Parameters:
- peer - the connected socket
- Returns:
- the object
- - (BOOL) handleRequest :(DData *) request :(DData *) response
- Handle a new request by the client (called by handleConnection, to be overridden)
- Parameters:
- request - the request made by the client
response - the destination for the response
- Returns:
- should the connection be stopped ?
- - (BOOL) open :(int) family :(int) protocol
- Open the TCP server
- Parameters:
- family - the family (DSA_AF_INET, DSA_AF_UNIX, DSA_AF_INET6)
protocol - the protocol to be used
- Returns:
- success
- - (BOOL) start :(id <DSocketAddressable>) address :(int) backlog :(int) mode :(int) connections
- Wait for connections and call 'handleConnection' on connection. Note: DTS_FORKING does not work
on WIN32; it is converted to DTS_THREADING.
- Parameters:
- address - the address for the server
backlog - the number of pending connnections before refuse
mode - the running mode (DTS_THREADING, DTS_FORKING, DTS_BLOCKING)
connections - the number of connections to be made before returning (or -1 for unlimited)
- Returns:
- success
generated 06-Sep-2008 by ObjcDoc 3.0.0