Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
It sends actually all bytes and files successfully. The problem is when it is received on the server-side. It receives the whole data byte but It is not receiving the whole packet data. Help me, please Here is my code.

What I have tried:

Sender side
while (!feof(fp))
{
    // Read the data from file
    int read_len = fread(buffer, 1, block_size, fp);
    printf("rd len %d\n", read_len);
    read_size += read_len;

    memcpy(data[i], buffer, s);

    if (i == k - 1 || read_size == file_size)
    {
        // Perform XOR encoding
        enc_data = xorENC(data, k, s);

        
        // Transmit encoded data
        for (j = 0; j <= k; j++)
        {
            // make packet
            packet.num = j;

            if ((file_size - read_size + 1) <= PACKLEN) // final string of package ?
            packet.len = file_size - read_size + 1;
            else // length PACKage LENgth
            packet.len = PACKLEN;

            if (j < k)
                packet.type = 0; // Source packet
            else
                packet.type = 1; // Redundant packet
            
            memcpy(packet.data, enc_data[j], s);
            //printf("%s - %d\n",packet.data,packet.num);
            // transmit packet
            sendto(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr_in));
            //usleep(100000);
        }

        // Reset data
        for (j = 0; j < k; j++)
        {
            memset(data[j], 0, s);
            // printf("reset data\n");
        }
    }

    i = (i + 1) % k; // 0, 1 2, 3, 0, 1, 2, 3  (k =4)
}


Receiver side

while (recv_blocks <= total_blocks)
{
    int counter = 0;

    // receive packet
    recvfrom(sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&server_addr, &addr_size);
    //printf("%s - %d\n",packet.data,packet.num);
    printf("recv_blocks= %d ################### \n",recv_blocks);
    // set receive packet
    recv_packet[packet.num] = 1;
    memcpy(enc_data[packet.num], packet.data, s);

    // cnt = fread(*data, 1, row * col, rp) ;
    //  Check wheter all packets are received
    for (i = 0; i <= k; i++)
    {
        printf("recv_packet[%d]=%d \n", i, recv_packet[i]);
        if (recv_packet[i] == 1)
            counter++;
    }

    // Check decodable condition
    printf("packet.num=%d, k=%d, counter=%d \n", packet.num, k, counter);
    if (packet.num == k || counter == k + 1)
    {
        // Find lost packet number
        idx = k;
        printf("lost idx =%d ####\n", idx);
        if (counter < k + 1)
        {
            for (i = 0; i <= k; i++)
            {
                if (!recv_packet[packet.num])
                {
                    idx = i;
                    printf("lost idx =%d ##############\n", idx);
                    break;
                }
            }
        }

        

        // Perform XOR decoding
        xorDEC(enc_data, idx, k, s);

        // write to file

        /*for (i = 0; i < k; i++)
        {

            sprintf(buff, "%s", enc_data[i]);
            // printf("modified with %s\n",buff);
            fwrite(buff, 1, s, fp);
        }*/

        for (i = 0; i < k; i++)
            fwrite(enc_data[i], s, 1, fp);

        /*for (int i = 0; i < k + 1; i++)
            free(enc_data[i]);

        free(enc_data);*/

        for (i = 0; i < k+1; i++)
            recv_packet[i] = 0;

        recv_blocks++;           
    }
}
Posted
Updated 29-Mar-22 6:46am
v2
Comments
Richard MacCutchan 29-Mar-22 3:49am    
It is not clear what the problem is. Socket data is sent in bursts and the number of blocks received can be different from the number sent. If you want to manage it by packets, then each packet needs a start and end marker which both sides can check.
jake_jane 29-Mar-22 22:34pm    
Hello Mr.Richard could you please show some example of creating start and end mark.
Thank you
Richard MacCutchan 30-Mar-22 3:43am    
I do not have any examples, that is something you need to design for yourself. But if you want a reliable transfer you should switch to TCP.

1 solution

If you're writing code at this level, you need to debug it yourself. It's very unlikely that anyone here will do it for you. Even if they wanted to, they'd first have to create the parts of your code that are missing, like socket allocation.

If you're creating a TCP socket (SOCK_STREAM) but using sendto and recvfrom as if it were UDP, you're still going to get TCP behavior. And like Richard said, TCP can bundle or segment your packets, so the number of packets sent and received could differ.

Where does the receive side get this magic total_packets? Could its value be wrong?

To help with debugging, check what sendto and recvfrom return. You're not checking for failures, and a lot of things can go wrong in networking code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900