recv function always gives me the same buffer

350 views Asked by At

This question is kind of a follow-up of a question that I previously asked. Basically I had a c++ server running in ubuntu and I'm now trying to make this code works in windows. I am having some issues with windows socket as it's my first time using them. After some troubles just receiving data on the socket I am now faced with something weird: the buffer that I pass to recv() always contains the same data.

Of course, I have checked my client and it does send something different. So I've been trying my best to get something that would help me figure out what was wrong. I printed on the console the number of bytes received by recv() and guess what it is changing as expected ! So I'm a little puzzled now, I don't quite get whu the number of bytes received would be different if the content of the buffer that I pass to recv() is always the same. Would like some help to figure out why.

Here is my code:

int tcp_server::acceptConns()
{
    sockaddr_in from;
    bool infinite = true ;
    int fromlen=sizeof(from);
    /* Infinte loop to echo
 the IP address of the client */

    int readsize;
    char* message;
    char* clientmessage = (char*) malloc(256*sizeof(char));
    string smatrix ;
    int ind ;
    string tok;
    int i = 0 ;
    int bytesSent ;

    float matrix[16] ;
     do {
        readsize = recv(ClientSocket, clientmessage, 256, 0);
        if (readsize > 0) {
            printf("Bytes received: %d\n", readsize);
            message = "ack";
            bytesSent = send(ClientSocket, message, strlen(message),0);
            if(bytesSent == 0){
                std::cerr << "Error sending ACK" << endl ;
            }
            smatrix = clientmessage ;
            std::stringstream ss(smatrix);
            while(getline(ss, tok, ',') && i < 16 ){
                matrix[i] = static_cast<float>(::atof(tok.c_str()));
                i++ ;
            }
            coutMessage(matrix);
            message ="ok";
            bytesSent = send(ClientSocket, message, strlen(message),0);
            if(bytesSent == 0){
                std::cerr << "Error sending OK" << endl ;
            }
        }
        else if (readsize == 0)
            printf("Connection closing...\n");
        else  {
            printf("recv failed with error: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

    } while (readsize > 0);

    // shutdown the connection since we're done
     readsize = shutdown(ClientSocket, SD_SEND);
     if (readsize == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ClientSocket);
    WSACleanup();
    return 0 ;
}

Thansk in advance for the help you will provide me.

1

There are 1 answers

1
ElderBug On BEST ANSWER
while(getline(ss, tok, ',') && i < 16 ){
    matrix[i] = static_cast<float>(::atof(tok.c_str()));
    i++ ;
}

Since you never reset i, matrix will never be updated after the first time. The recv is fine.