Client Socket Class

The client socket class library module has the file name csocket.cpp. This class allows a client socket application to communicate with a socket 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(string, int)
- constructor(char *, int)
- set_active
- get_active
- send_receive(string, string)
- send_receive(char *, string)
- close_socket
- is_init
- recv_data
- send_data
- client_connect

# Class Data Members

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

This data member is the client socket structure or handle.

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

This data member is the client socket address structure.

## hostent(Unix) LPHOSTENT(Windows) lpHostEnt - private

This data member holds the host socket server information.

## string client_hostname - private

This data member stores the host name of the current socket server.

## int client_port - private

This data member holds the TCP port number of the current socket server.

## bool client_init - private

This data member indicates whether the client 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
- csocket.h

# Class Methods

## empty constructor

    Prototype  : csocket::csocket()

This empty constructor initializes the client socket class data members client_hostname, client_port and client_init to empty or zero values.

## constructor(string, int)

    Prototype  : csocket::csocket(string& host_name, int port)
    Parameters :
          Name : host_name
    Description: socket server host name

          Name : port
    Description: socket server TCP port number

This constructor will attempt to load the class data members by calling the class method set_active. The status of the class initialization flag should be checked after using this constructor by calling the class method is_init.

## constructor(char *, int)

    Prototype  : csocket::csocket(char *host_name, int port)
    Parameters :
          Name : host_name
    Description: socket server host name

          Name : port
    Description: socket server TCP port number

This constructor will attempt to load the class data members by calling the class method set_active. The status of the class initialization flag should be checked after using this constructor by calling the class method is_init.

## set_active

    Prototype  : int csocket::set_active(string& host_name, int port_number)
    Parameters :
          Name : host_name
    Description: socket server host name

          Name : port_number
    Description: socket server TCP port number

    Returns    : TRUE upon success, FALSE otherwise

This method will define the passed socket server host name and socket server TCP port number as the current socket server. No attempt will be made to perform a connection test. This method should be used to load or change socket server details and when using the empty constructor.

## get_active

    Prototype  : int csocket::get_active(string& host_name, int& port_number)
    Parameters :
          Name : host_name
    Description: returned socket server host name

          Name : port_number
    Description: returned socket server TCP port number

    Returns    : TRUE upon success, FALSE otherwise

This method will obtain the current socket server host name and port number. The client socket interface must have already been initialized by calling the method set_active before using this method.

## send_receive(string, string)

    Prototype  : int csocket::send_receive(string& sbuf, string& rbuf)
    Parameters :
          Name : sbuf
    Description: send string

          Name : rbuf
    Description: returned received string

    Returns    : TRUE upon success, FALSE otherwise

This method will communicate with the currently defined socket server by first sending the contents of the send string then waiting for a reply and loading the reply into the returned received string.

## send_receive(char *, string)

    Prototype  : int csocket::send_receive(char *sbuf, string& rbuf)
    Parameters :
          Name : sbuf
    Description: send string

          Name : rbuf
    Description: returned received string

    Returns    : TRUE upon success, FALSE otherwise

This method will communicate with the currently defined socket server by first sending the contents of the send string then waiting for a reply and loading the reply into the returned received string.

## close_socket

    Prototype  : int csocket::close_socket(void)

    Returns    : TRUE upon success, FALSE otherwise

This method will close the client socket. This method should not normally have to be called since the method send_receive automatically closes the client socket after communication is complete.

## is_init

    Prototype  : bool csocket::is_init(void)

    Returns    : initialization flag

This inline method indicates whether the client socket class has been properly initialized.

## recv_data

    Prototype  : int csocket::recv_data(string& buf)
    Parameters :
          Name : buf
    Description: returned received string

    Returns    : number of bytes received upon success, zero otherwise

This private method will receive data. The client socket is assumed to be open and functioning.

## send_data

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

    Returns    : number of bytes sent upon success, zero otherwise

This private method will send data. The client socket is assumed to be open and functioning.

## client_connect

    Prototype  : int csocket::client_connect(void)

    Returns    : TRUE upon success, FALSE otherwise

This private method will attempt to connect to the currently defined socket server and open the client socket.

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

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