Search code examples
c++socketstcppacketrecv

Recv function for TCP Socket programming


I am new in Socket Programming. I am trying to create a client application. The server is a camera which communicates using TCP. The camera is sending continuous data. Using Wireshark, I can see that the camera is sending continuous packets of different sizes, but not more than 1514 bytes. But my recv function is always returning 2000 which is the size of my buffer.

unsigned char buf[2000];
int bytesIn = recv(sock, (char*)buf, sizeof(buf) , 0);
if (bytesIn > 0)
{
    std::cout << bytesIn << std::endl;
}

The first packet I receive is of size 9 bytes, which recv returns correct, but after that it always returns 2000.

Can anyone please tell me the solution so that I can get the correct size of the actual data payload?

EDIT

int bytesIn = recv(sock, (char*)buf, sizeof(buf) , 0);
if (bytesIn > 0)
{
    while (bytes != 1514)
    {
        if (count == 221184)
        {
            break;
        }
        buffer[count++] = buf[bytes++];
    }
    std::cout << count;
}

EDIT:

Here is my Wireshark capture:

WireShark Image

My Code to handle packets

int bytesIn = recv(sock, (char*)&buf, sizeof(buf) , 0);
        if (bytesIn > 0)
        {
            if (flag1 == true)
            {
                while ((bytes != 1460 && (buf[bytes] != 0)) && _fillFlag)
                {
                    buffer[fill++] = buf[bytes++];

                    if (fill == 221184)
                    {
                        flag1 = false;
                        _fillFlag = false;
                        fill = 0;
                        queue.Enqueue(buffer, sizeof(buffer));
                        break;
                    }
                }
            }
            if ((strncmp(buf, _string2, 10) == 0))
            {
                flag1 = true;
            }
        }

For each frame camera is sending 221184 bytes and after each frame it sends a packet of data 9 bytes which I used to compare this 9 bytes are constant.

This 221184 bytes send by camera doesn't have 0 so I use this condition in while loop. This code is working and showing the frame but after few frame it shows fully black frame. I think the mistake is in receiving the packet.


Solution

  • Size of per frame is : 221184 (fixed)   
    Size of per recv is : 0 ~ 1514
    

    My implementation here :

    DWORD MakeFrame(int socket)
    {
        INT nFrameSize = 221184;
        INT nSizeToRecv = 221184;
        INT nRecvSize = 2000;
        INT nReceived = 0;
        INT nTotalReceived = 0;
        BYTE byCamera[2000] = { 0 };    // byCamera size = nRecvSize
        BYTE byFrame[221184] = { 0 };   // byFrame size = nFrameSize
    
        while(0 != nSizeToRecv)
        {
            nRecvSize = min(2000, nSizeToRecv);
            nReceived = recv(socket, (char*)byCamera, nRecvSize, 0);
    
            memcpy_s(byFrame + nTotalReceived, nFrameSize, byCamera, nReceived);
    
            nSizeToRecv -= nReceived;
            nTotalReceived += nReceived;
        }
    
        // byFrame is ready to use.
        // ...
        // ...
    
        return WSAGetLastError();
    }
    

    enter image description here