Duplicate Data Off Socket - Perhaps?

Hi All,

I'm new to C++ programming and found myself, despite much googling very confused into a little bug of mine. I thought I would join this forum and see if anyone else had encountered the same / knows how to help please?

The app essentially reads data from a published TCP stream, then formats, filters and saves it. It's working 99% of the time, but it seems i'm loosing the odd record here and there and i'm at a loss as to why!

The code is 242 lines long, but i believe this is the affected code;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

    while (1 < 2) { // MAIN LOOP


        //Keep Reading from Socket until MSG, present. (note: maybe split across multiple receives)
        while ( tmpBuffer.find("MSG,") == std::string::npos) {
            bytes_recv = recv(sockFD, &reply.front(), reply.size(), 0);
            if (bytes_recv == -1) {
            // REMEMBER TO REESTABLISH CONNECTION HERE
            std::cerr << "Error while receiving bytes\n";
            return -6;
            }
            MyFile2 << reply;
            tmpBuffer = tmpBuffer + reply;
        }

   << DO STUFF>>

}

I'm using the "MyFile2" file when debugging to record the data as it arrives. tmpBuffer is then used for follow up processing.

The bug is that every now and then, it appears that a single instance of reply is added twice to the tmpBuffer.


Sample Data is MyFile2:(Note: MSG3 for A728C1).
MSG,3,0,0,A71E92,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,,40000,,,39.908070,-84.248718,,,,,,
MSG,4,0,0,A71E92,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,,,363.397858,267.318695,,,0,,,,,
MSG,1,0,0,A72745,0,2021/04/21,12:30:28.000,2021/04/21,12:30:27.000,MLN562,,,,,,,,,,,
MSG,3,0,0,A72745,0,2021/04/21,12:30:28.000,2021/04/21,12:30:27.000,,40000,,,38.979996,-81.646271,,,,,,
MSG,4,0,0,A72745,0,2021/04/21,12:30:28.000,2021/04/21,12:30:27.000,,,327.604950,232.815292,,,-128,,,,,
MSG,1,0,0,A728C1,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,AAL1007,,,,,,,,,,,
MSG,3,0,0,A728C1,0,2021/04/21,12:30:28.000,2021/04/21,12:30:21/04/21,128.000,,33500,,,27.876892,-79.949738,,,,,,
MSG,4,0,0,A728C1,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,,,441.774841,164.642502,,,-896,,,,,
MSG,1,0,0,A72BDA,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,JIA5506,,,,,,,,,,,
MSG,3,0,0,A72BDA,0,2021/04/21,12:30:28.000,2021/04/21,12:30:28.000,,32750,,,41.195984,-87.296936,,,,,,

I've analysed the same data stream, by piping netcat into a file, this recording doesn't show instances of this additional data.

Note: Before the Main Loop the Socket is setup as follows;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    //Set Connection Cfg
    addrinfo hints, *p;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags    = AI_PASSIVE;

    //Check Address
    int gAddRes = getaddrinfo(ipAddress, portNum, &hints, &p);
    if (gAddRes != 0) {
        std::cerr << gai_strerror(gAddRes) << "\n";
        return -2;
    }
    if (p == NULL) {
        std::cerr << "No addresses found\n";
        return -3;
    }

    //Create Socket
    int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (sockFD == -1) {
        std::cerr << "Error while creating socket\n";
        return -4;
    }

    //Connect Socket
    int connectR = connect(sockFD, p->ai_addr, p->ai_addrlen);
    if (connectR == -1) {
        close(sockFD);
        std::cerr << "Error while connecting socket\n";
        return -5;
    }

Any help that can be provided would be much appreciated

Many Thanks

Ainsley
Last edited on
Please edit your post for readability.
https://www.cplusplus.com/articles/jEywvCM9/

> bytes_recv = recv(sockFD, &reply.front(), reply.size(), 0);
This might store the data, but you still need to use bytes_recv when you do this.
> tmpBuffer = tmpBuffer + reply;
Thank you Salem C. Post Updated to suit.

I've resolved the issue today and thought I'd update the post for future info.

The 'bytes_recv' was being used to identify how many bytes have been recieved / if the recv function has thrown a wobbly, the data itself was stored in the string 'reply'.

The following change appears to result in a more stable data feed;

1
2
// from bytes_recv = recv(sockFD, &reply.front(), reply.size(), 0);
bytes_recv = recv(sockFD, &reply.front(), reply.size(), MSG_WAITALL);


Hope that helps someone else.
Topic archived. No new replies allowed.