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:
This data member is the client socket structure or handle.
This data member is the client socket address structure.
This data member holds the host socket server information.
This data member stores the host name of the current socket server.
This data member holds the TCP port number of the current socket server.
This data member indicates whether the client socket has been successfully and completely initialized.
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.
This class requires the header files:
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.
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.
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.
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.
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.
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.
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.
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.
Prototype : bool csocket::is_init(void) Returns : initialization flag
This inline method indicates whether the client socket class has been properly initialized.
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.
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.
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.