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;
35 if ((he = gethostbyname(pStrHost)) == NULL) {
37 perror("IoTSocketClient: Gethostbyname error!");
41 if ((m_iSock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
43 perror("IoTSocketClient: Socket error!");
47 m_addrRemote.sin_family = AF_INET;
48 m_addrRemote.sin_port = htons(m_iPort);
49 m_addrRemote.sin_addr = *((struct in_addr *) he->h_addr);
50 memset(&(m_addrRemote.sin_zero), 0, 8);
52 // Make socket client wait for socket server to be ready
53 while (connect(m_iSock, (struct sockaddr *) &m_addrRemote, sizeof(struct sockaddr)) == -1) { }
55 // Send out request for reversed bits or not
60 if (send(m_iSock, temp, 1, 0) == -1)
62 perror("IoTSocketClient: Send 1 error!");
67 if (send(m_iSock, temp, 1, 0) == -1)
69 perror("IoTSocketClient: Send 2 error!");