Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære UDP Communication | Building Network Applications
C Networking Basics

bookUDP Communication

When you build network applications in C, you can choose between different protocols depending on your needs. Two of the most commonly used protocols are TCP and UDP. Understanding their differences will help you decide when to use each one.

TCP (Transmission Control Protocol)
expand arrow

TCP is a connection-oriented protocol. This means that it establishes a connection between the sender and receiver before any data is transferred. TCP guarantees that data will arrive in order and without errors, making it a good choice for applications like file transfers, web browsing, and email, where reliability is essential.

UDP (User Datagram Protocol)
expand arrow

UDP is connectionless. It does not establish a dedicated end-to-end connection before sending data. UDP simply sends datagrams, packets of data, directly to the receiver. There is no guarantee that packets will arrive, arrive in order, or even arrive at all. However, UDP is much faster and has less overhead than TCP. This makes it ideal for applications where speed is more important than reliability, such as real-time audio or video streaming, online gaming, or simple query-response services like DNS.

udp_example.c

udp_example.c

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 void run_sender(const char *ip) { int sockfd; struct sockaddr_in dest_addr; char buffer[BUFFER_SIZE] = "Hello, UDP receiver!"; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Sender socket creation failed"); exit(EXIT_FAILURE); } memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(PORT); dest_addr.sin_addr.s_addr = inet_addr(ip); ssize_t sent = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (sent < 0) { perror("sendto failed"); close(sockfd); exit(EXIT_FAILURE); } printf("Sender: Sent message '%s' to %s:%d\n", buffer, ip, PORT); close(sockfd); } void run_receiver() { int sockfd; struct sockaddr_in recv_addr, sender_addr; char buffer[BUFFER_SIZE]; socklen_t addr_len = sizeof(sender_addr); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Receiver socket creation failed"); exit(EXIT_FAILURE); } memset(&recv_addr, 0, sizeof(recv_addr)); recv_addr.sin_family = AF_INET; recv_addr.sin_port = htons(PORT); recv_addr.sin_addr.s_addr = INADDR_ANY; if (bind(sockfd, (struct sockaddr *)&recv_addr, sizeof(recv_addr)) < 0) { perror("bind failed"); close(sockfd); exit(EXIT_FAILURE); } printf("Receiver: Waiting for messages on port %d...\n", PORT); ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&sender_addr, &addr_len); if (recv_len < 0) { perror("recvfrom failed"); close(sockfd); exit(EXIT_FAILURE); } buffer[recv_len] = '\0'; printf("Receiver: Received message '%s' from %s:%d\n", buffer, inet_ntoa(sender_addr.sin_addr), ntohs(sender_addr.sin_port)); close(sockfd); } int main(int argc, char *argv[]) { if (argc == 2 && strcmp(argv[1], "receiver") == 0) { run_receiver(); } else if (argc == 3 && strcmp(argv[1], "sender") == 0) { run_sender(argv[2]); } else { printf("Usage:\n"); printf(" %s receiver\n", argv[0]); printf(" %s sender <receiver_ip>\n", argv[0]); return 1; } return 0; }

You should use UDP when:

  • Low latency and speed are more important than reliability;
  • Applications can tolerate some data loss;
  • You want to broadcast or multicast messages to multiple recipients;
  • The protocol or application layer can handle missing or out-of-order data.
question mark

Which of the following is a key characteristic of UDP compared to TCP?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookUDP Communication

Stryg for at vise menuen

When you build network applications in C, you can choose between different protocols depending on your needs. Two of the most commonly used protocols are TCP and UDP. Understanding their differences will help you decide when to use each one.

TCP (Transmission Control Protocol)
expand arrow

TCP is a connection-oriented protocol. This means that it establishes a connection between the sender and receiver before any data is transferred. TCP guarantees that data will arrive in order and without errors, making it a good choice for applications like file transfers, web browsing, and email, where reliability is essential.

UDP (User Datagram Protocol)
expand arrow

UDP is connectionless. It does not establish a dedicated end-to-end connection before sending data. UDP simply sends datagrams, packets of data, directly to the receiver. There is no guarantee that packets will arrive, arrive in order, or even arrive at all. However, UDP is much faster and has less overhead than TCP. This makes it ideal for applications where speed is more important than reliability, such as real-time audio or video streaming, online gaming, or simple query-response services like DNS.

udp_example.c

udp_example.c

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #define PORT 12345 #define BUFFER_SIZE 1024 void run_sender(const char *ip) { int sockfd; struct sockaddr_in dest_addr; char buffer[BUFFER_SIZE] = "Hello, UDP receiver!"; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Sender socket creation failed"); exit(EXIT_FAILURE); } memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(PORT); dest_addr.sin_addr.s_addr = inet_addr(ip); ssize_t sent = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); if (sent < 0) { perror("sendto failed"); close(sockfd); exit(EXIT_FAILURE); } printf("Sender: Sent message '%s' to %s:%d\n", buffer, ip, PORT); close(sockfd); } void run_receiver() { int sockfd; struct sockaddr_in recv_addr, sender_addr; char buffer[BUFFER_SIZE]; socklen_t addr_len = sizeof(sender_addr); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Receiver socket creation failed"); exit(EXIT_FAILURE); } memset(&recv_addr, 0, sizeof(recv_addr)); recv_addr.sin_family = AF_INET; recv_addr.sin_port = htons(PORT); recv_addr.sin_addr.s_addr = INADDR_ANY; if (bind(sockfd, (struct sockaddr *)&recv_addr, sizeof(recv_addr)) < 0) { perror("bind failed"); close(sockfd); exit(EXIT_FAILURE); } printf("Receiver: Waiting for messages on port %d...\n", PORT); ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&sender_addr, &addr_len); if (recv_len < 0) { perror("recvfrom failed"); close(sockfd); exit(EXIT_FAILURE); } buffer[recv_len] = '\0'; printf("Receiver: Received message '%s' from %s:%d\n", buffer, inet_ntoa(sender_addr.sin_addr), ntohs(sender_addr.sin_port)); close(sockfd); } int main(int argc, char *argv[]) { if (argc == 2 && strcmp(argv[1], "receiver") == 0) { run_receiver(); } else if (argc == 3 && strcmp(argv[1], "sender") == 0) { run_sender(argv[2]); } else { printf("Usage:\n"); printf(" %s receiver\n", argv[0]); printf(" %s sender <receiver_ip>\n", argv[0]); return 1; } return 0; }

You should use UDP when:

  • Low latency and speed are more important than reliability;
  • Applications can tolerate some data loss;
  • You want to broadcast or multicast messages to multiple recipients;
  • The protocol or application layer can handle missing or out-of-order data.
question mark

Which of the following is a key characteristic of UDP compared to TCP?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt