1 /** Class IoTSocketClient is a communication class
2 * that provides interfaces to connect to either
3 * Java or C++ socket endpoint. It inherits the
4 * methods from IoTSocket.
6 * Adapted from Java/C++ socket implementation
8 * @see <a href="https://www.keithv.com/software/socket/</a>
10 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
14 #ifndef _IOTSOCKETCLIENT_HPP__
15 #define _IOTSOCKETCLIENT_HPP__
17 #include "IoTSocket.hpp"
19 class IoTSocketClient final : public IoTSocket
22 IoTSocketClient(int iPort, const char* pStrHost, bool bReverse, bool* pResult);
27 IoTSocketClient::IoTSocketClient(int iPort, const char* pStrHost, bool bReverse, bool* pResult) :
28 IoTSocket(iPort, pResult) {
30 struct hostent* he = NULL;
33 if ((he = gethostbyname(pStrHost)) == NULL) {
34 perror("IoTSocketClient: Gethostbyname error!");
37 if ((m_iSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
39 perror("IoTSocketClient: Socket error!");
43 m_addrRemote.sin_family = AF_INET;
44 m_addrRemote.sin_port = htons(m_iPort);
45 m_addrRemote.sin_addr = *((struct in_addr *) he->h_addr);
46 memset(&(m_addrRemote.sin_zero), 0, 8);
48 // Make socket client wait for socket server to be ready
49 if (connect(m_iSock, (struct sockaddr *) &m_addrRemote, sizeof(struct sockaddr)) == -1) {
50 perror("IoTSocketServer: Accept connection error!");
54 // Send out request for reversed bits or not
59 if (send(m_iSock, temp, 1, 0) == -1)
61 perror("IoTSocketClient: Send 1 error!");
66 if (send(m_iSock, temp, 1, 0) == -1)
68 perror("IoTSocketClient: Send 2 error!");