Anuraag Tummanapally
Anuraag Tummanapally

Reputation: 98

Socket Programming in C: UDP send() taking 6 - 260 micro sec, how to make it faster

Using SOCK_DGRAM for UDP sockets

All packets are 22 bytes in length (ie 64 including headers)

client.c

...
    no_of_packets--;
    sprintf(buf, "#:!0 rem");
    sprintf(buf, format , buf);
    sprintf(buf_aux, "#: 0 rem");
    sprintf(buf_aux, format , buf_aux);
    buf[MAX_LINE-1] = '\0';
    buf_aux[MAX_LINE-1] = '\0';
    len = strlen(buf) + 1;
    send(s, buf, len, 0);
    while (no_of_packets-- > 1) {
        nanosleep(&T, NULL);
        send(s, buf, len, 0);
    }
    send(s, buf_aux, len, 0);

server.c

...
while(1) {
        if (len = recv(s, buf, sizeof(buf), 0)){
            // do nothing
        }
}

When I open Wireshark to see avg delay between the packets which are sent,

I can see the following:

How can I achieve this speed?

Upvotes: 3

Views: 1080

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136495

On Linux, you can probably do that with a kernel bypass network stack, such as PF_RING ZC (Zero Copy), and FIFO real-time threads that run on isolated cores that:

  1. Fill a packet to send in the network card buffer.
  2. Busy-wait till the next time point.
  3. Emit the prepared packet into the wire.
  4. Go to 1.

You may also find Understanding PCIe performance for end host networking white-paper useful.

Upvotes: 3

Related Questions