'Transfer large packets in UDP

Hi thanks for reviewing my question, I am trying to transfer large packets using simple UDP sockets in C. OS is windows 10; IDE Visual Studio. System build-up:

  1. UDP client - Send 6250 packets of 8K.
  2. UDP server - receive 8K packets count them, and print to the screen.

When I run the server and start to receive packets, I succeed to receive between 1500-2000 packets the rest are seen in WireShark but my program lost them.

I know that it can be that the sender side is writing much faster than the server reads but it looks like a large amount of packet loss. How can I improve or change my server-side code to be able to stand the rate? Server code:

SOCKET s;
struct sockaddr_in server, si_other;
int slen, recv_len;
uint8_t buf[BUFLEN] = {0} //8200B buffer;       
WSADATA wsa;
slen = sizeof(si_other);
int countwrite = 0;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    
    printf("Failed. Error Code : %d", WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("Initialised.\n");

if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
    printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);

//Bind
if (bind(s, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
    printf("Bind failed with error code : %d", WSAGetLastError());
    exit(EXIT_FAILURE);
}
puts("Bind done");
while (1)
{
    printf("Waiting for data...");
    fflush(stdout);

    //clear the buffer by filling null, it might have previously received data
    memset(buf, 0, BUFLEN);
    //try to receive some data, this is a blocking call
    if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen)) == SOCKET_ERROR)
    {
        printf("recvfrom() failed with error code : %d", WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    countwrite += 1;
    printf("Packet Number : %d\n", countwrite);
}

Thank you for helping.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source