vvvv
Dependents: 02_DPPU_JUANDA_120 02_DPPU_JUANDA_120_Latest_copy 02_DPPU_JUANDA_120_Latest
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) 00046 { 00047 if (_socket < 0) 00048 return false; 00049 return _mdm->socketIsConnected(_socket); 00050 } 00051 00052 /** Send data to the remote host. 00053 \param data The buffer to send to the host. 00054 \param length The length of the buffer to send. 00055 \return the number of written bytes on success (>=0) or -1 on failure 00056 */ 00057 int send(char* data, int length) { return _mdm->socketSend(_socket, data, length); } 00058 00059 /** Send all the data to the remote host. 00060 \param data The buffer to send to the host. 00061 \param length The length of the buffer to send. 00062 \return the number of written bytes on success (>=0) or -1 on failure 00063 */ 00064 int send_all(char* data, int length) { return send(data,length); } 00065 00066 /** Receive data from the remote host. 00067 \param data The buffer in which to store the data received from the host. 00068 \param length The maximum length of the buffer. 00069 \return the number of received bytes on success (>=0) or -1 on failure 00070 */ 00071 int receive(char* data, int length) { return _mdm->socketRecv(_socket, data, length); } 00072 00073 /** Receive all the data from the remote host. 00074 \param data The buffer in which to store the data received from the host. 00075 \param length The maximum length of the buffer. 00076 \return the number of received bytes on success (>=0) or -1 on failure 00077 */ 00078 int receive_all(char* data, int length) { return receive(data,length); } 00079 00080 }; 00081 00082 #endif
Generated on Sat Jul 16 2022 05:51:53 by 1.7.2