Server Socket Class

The server socket class library module has the file name ssocket.cpp. This class allows an application to easily become a socket server. This class implements a TCP connection oriented iterative server. This C++ class can be compiled and used in any Linux/Unix/QNX system as well as 32-bit Windows. When using this class under 32-bit Windows it is your responsibility to call the WinSock functions WSAStartup and WSACleanup from within your application. Your C++ compiler must implement and support the full ANSI C++ string class in addition to the C socket interface. Here is a list of methods in the client socket class:

- empty constructor
- constructor(int)
- init
- server_wait
- is_init
- close_client
- close_all
- recv_data
- send_data

# Class Data Members

## int(Unix) SOCKET(Windows) serversocket - private

This data member is the server socket structure or handle.

## int(Unix) SOCKET(Windows) srvclientSocket - private

This data member is the client socket structure or handle.

## sockaddr_in(Unix) SOCKADDR_IN(Windows) sockServerAddr - private

This data member is the server socket address structure.

## sockaddr_in(Unix) SOCKADDR_IN(Windows) sockClientAddr - private

This data member is the client socket address structure.

## string server_hostname - private

This data member stores the server host name.

## int server_port - private

This data member holds the server TCP port number.

## bool server_init - private

This data member indicates whether the server socket has been successfully and completely initialized.

# Module Dependencies

This class does not depend on any other Future Lab module and may be used by itself. This class does, however, use the C++ standard library string class.

# Class Header Files

This class requires the header files:

- stdhead.h
- flsocket.h
- ssocket.h

# Class Methods

## empty constructor

    Prototype  : ssocket::ssocket()

This empty constructor initializes the server socket class data members server_hostname, server_port and server_init to empty or zero values.

## constructor(int)

    Prototype  : ssocket::ssocket(int port)
    Parameters :
          Name : port
    Description: socket server TCP port number

This constructor will attempt to load the class data members and call the init method to initialize the server. The server will not automatically listen to the TCP port by calling this method. You must call the server_wait method to allow client connections.

The status of the class initialization flag should be checked after using this constructor by calling the class method is_init.

## init

    Prototype  : bool ssocket::init(int port)
    Parameters :
          Name : port_number
    Description: socket server TCP port number

    Returns    : TRUE upon success, FALSE otherwise

This method will initialize the socket server class. The socket server will not start listening to the socket server TCP port number after calling this method. To allow the socket server to accept client connections, you must call the server_wait method. Since this method is called by the constructor(int), it should only be called by your application when using the empty constructor.

## server_wait

    Prototype  : bool ssocket::server_wait(void)

    Returns    : TRUE if a client connection was successful, FALSE otherwise

This method will cause the socket server to wait for a client connection. The socket server application will become receive blocked until a client connection is detected. The socket server class must have already been successfully initialized before calling this method.

## is_init

    Prototype  : bool ssocket::is_init(void)

    Returns    : TRUE if the socket server class is initialized, FALSE otherwise

This inline method will return the value of the class initialization flag.

## close_client

    Prototype  : bool ssocket::close_client(void)

    Returns    : TRUE upon success, FALSE otherwise

This method will close the client socket. This method should be called after each client connection when a reply has been successfully sent back to the client.

## close_all

    Prototype  : bool ssocket::close_all(void)

    Returns    : TRUE upon success, FALSE otherwise

This method will close the client socket (if open) and the server socket. This method should be called upon the termination of the socket server.

## recv_data

    Prototype  : int ssocket::recv_data(string& buf)
    Parameters :
          Name : buf
    Description: returned receive buffer

    Returns    : number of bytes received upon success, zero otherwise

This method will receive data from the client. The client socket is assumed to be open. This method should be called right after a client connection is detected using the server_wait method.

## send_data

    Prototype  : int ssocket::send_data(string& buf)
    Parameters :
          Name : buf
    Description: send buffer

    Returns    : number of bytes sent upon success, zero otherwise

This method will send the contents of the send buffer to the client. The client socket is assumed to be open. This method should be called after receiving client data and processing to form the reply.

Goto Top | C++ GPL Library Overview | C++ Overview
Future Lab Home | Contact Webmaster | Feedback

Copyright © 2000-2006 Future Lab, Last Updated Jun 29, 2006