* add C027_Support fork

Fork of C027_Support by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketConnection.h Source File

TCPSocketConnection.h

00001 #ifndef TCPSOCKET_H
00002 #define TCPSOCKET_H
00003 
00004 #include "Socket.h"
00005 
00006 /** TCP socket connection
00007  */
00008 class TCPSocketConnection: public Socket
00009 {
00010     friend class TCPSocketServer;
00011 
00012 public:
00013     /** TCP socket connection
00014     */
00015     TCPSocketConnection() {}
00016 
00017     /** Connects this TCP socket to the server
00018     \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
00019     \param port The host's port to connect to.
00020     \return 0 on success, -1 on failure.
00021     */
00022     int connect(const char* host, const int port)
00023     {
00024         if (_mdm == NULL)
00025             _mdm = MDMParser::getInstance();
00026         if (_mdm == NULL)
00027             return -1;
00028             
00029         if (_socket < 0) {
00030             _socket = _mdm->socketSocket(MDMParser::IPPROTO_TCP);
00031             if (_socket < 0) {
00032                 return -1;
00033             }
00034         }
00035     
00036         _mdm->socketSetBlocking(_socket, _timeout_ms); 
00037         if (!_mdm->socketConnect(_socket, host, port)) {
00038             return -1;
00039         }
00040         return 0;
00041     }
00042     /** Check if the socket is connected
00043     \return true if connected, false otherwise.
00044     */
00045     bool is_connected(void)                 { return _mdm->socketIsConnected(_socket); }
00046 
00047     /** Send data to the remote host.
00048     \param data The buffer to send to the host.
00049     \param length The length of the buffer to send.
00050     \return the number of written bytes on success (>=0) or -1 on failure
00051      */
00052     int send(char* data, int length)        { return _mdm->socketSend(_socket, data, length); }
00053 
00054     /** Send all the data to the remote host.
00055     \param data The buffer to send to the host.
00056     \param length The length of the buffer to send.
00057     \return the number of written bytes on success (>=0) or -1 on failure
00058     */
00059     int send_all(char* data, int length)    { return send(data,length); }
00060 
00061     /** Receive data from the remote host.
00062     \param data The buffer in which to store the data received from the host.
00063     \param length The maximum length of the buffer.
00064     \return the number of received bytes on success (>=0) or -1 on failure
00065      */
00066     int receive(char* data, int length)     { return _mdm->socketRecv(_socket, data, length); }
00067 
00068     /** Receive all the data from the remote host.
00069     \param data The buffer in which to store the data received from the host.
00070     \param length The maximum length of the buffer.
00071     \return the number of received bytes on success (>=0) or -1 on failure
00072     */
00073     int receive_all(char* data, int length) { return receive(data,length); }
00074     
00075 };
00076 
00077 #endif