Fixing a bug in C++ socket server-client: message length was represented just in...
[iot2.git] / iotjava / iotrmi / C++ / IoTSocket.hpp
index 642882f8a63afbcd5e5cf9ee555f801f33148c51..cf62ec1a2d6af143703d807a888f2254fe615013 100644 (file)
@@ -17,6 +17,9 @@
 #define DEBUG_ACK
 
 static const int SOCKET_BUFF_SIZE = 64000;
+// Before, it was too short as we were just using 1 byte to receive the length
+// Now, we allocate 4 bytes (a size of integer) to receive the message length
+static const int MSG_LEN_SIZE = 4;
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -92,7 +95,7 @@ bool IoTSocket::sendBytes(char* pVals, int _iLen) {
        int iLen = _iLen;
        size[0] = iLen;
 
-       if (send(m_iSock, size, 1, 0) == -1) {
+       if (send(m_iSock, size, MSG_LEN_SIZE, 0) == -1) {
                perror("IoTSocket: Send size error!");
                return false;
        }
@@ -125,16 +128,18 @@ char* IoTSocket::receiveBytes(char* pVals, int* len)
 
        int iTotal = 0;
        int iResult = 0;
-       char size[1];
+       int size[1];
+
        while ((iTotal < 1) && (iResult != -1)) {
-               iResult = recv(m_iSock, size, 1, 0);    
+               iResult = recv(m_iSock, size, MSG_LEN_SIZE, 0);         
                iTotal += iResult;
        }
        if (iResult == -1) {
                perror("IoTSocket: Receive size error!");
                return NULL;
        }
-       int iLen = (int) size[0];
+       int iLen = size[0];
+
        // To be returned from this method...
        *len = iLen;
        pVals = new char[iLen];