Example of UDP Socket program for Linux network programming

  • 2020-04-02 02:37:47
  • OfStack

In the network transport protocol, TCP provides a reliable, complex, connection-oriented data stream (SOCK_STREAM) service that establishes connections through a three-stage handshake. TCP has a "retransmission confirmation" mechanism, that is, the receiving end will send a positive confirmation signal after receiving the data. If the sending end receives the positive confirmation signal, it will continue to send other data, if not, it will re-send.

In contrast, UDP is a connectionless, unreliable datagram (SOCK_DGRAM) transport service. Instead of establishing a connection using the UDP socket interface, the server can communicate (the recvfrom function and sendto function) after calling socket() to generate a socket and calling bind() to bind the port. After the client generates a socket with socket(), it can send and receive data to the server address.

Special note here: TCP USES a stream socket (SOCK_STREAM), UDP USES a datagram socket (SOCK_DGRAM)

UDP socket programming example:

The server code is as follows:


 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<unistd.h> 
#include<netinet/in.h> 
#include<arpa/inet.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<errno.h> 
#include<netdb.h> 
#include<stdarg.h> 
#include<string.h> 
 
#define SERVER_PORT 8000 
#define BUFFER_SIZE 1024 
#define FILE_NAME_MAX_SIZE 512 
 
int main() 
{ 
  
 struct sockaddr_in server_addr; 
 bzero(&server_addr, sizeof(server_addr)); 
 server_addr.sin_family = AF_INET; 
 server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
 server_addr.sin_port = htons(SERVER_PORT); 
 
  
 int server_socket_fd = socket(AF_INET, SOCK_DGRAM, 0); 
 if(server_socket_fd == -1) 
 { 
  perror("Create Socket Failed:"); 
  exit(1); 
 } 
 
  
 if(-1 == (bind(server_socket_fd,(struct sockaddr*)&server_addr,sizeof(server_addr)))) 
 { 
  perror("Server Bind Failed:"); 
  exit(1); 
 } 
 
  
 while(1) 
 {  
   
  struct sockaddr_in client_addr; 
  socklen_t client_addr_length = sizeof(client_addr); 
 
   
  char buffer[BUFFER_SIZE]; 
  bzero(buffer, BUFFER_SIZE); 
  if(recvfrom(server_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&client_addr, &client_addr_length) == -1) 
  { 
   perror("Receive Data Failed:"); 
   exit(1); 
  } 
 
   
  char file_name[FILE_NAME_MAX_SIZE+1]; 
  bzero(file_name,FILE_NAME_MAX_SIZE+1); 
  strncpy(file_name, buffer, strlen(buffer)>FILE_NAME_MAX_SIZE?FILE_NAME_MAX_SIZE:strlen(buffer)); 
  printf("%sn", file_name); 
 } 
 close(server_socket_fd); 
 return 0; 
} 

The client code is as follows:


 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<unistd.h> 
#include<netinet/in.h> 
#include<arpa/inet.h> 
#include<stdio.h> 
#include<stdlib.h> 
#include<errno.h> 
#include<netdb.h> 
#include<stdarg.h> 
#include<string.h> 
 
#define SERVER_PORT 8000 
#define BUFFER_SIZE 1024 
#define FILE_NAME_MAX_SIZE 512 
 
int main() 
{ 
  
 struct sockaddr_in server_addr; 
 bzero(&server_addr, sizeof(server_addr)); 
 server_addr.sin_family = AF_INET; 
 server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
 server_addr.sin_port = htons(SERVER_PORT); 
 
  
 int client_socket_fd = socket(AF_INET, SOCK_DGRAM, 0); 
 if(client_socket_fd < 0) 
 { 
  perror("Create Socket Failed:"); 
  exit(1); 
 } 
 
  
 char file_name[FILE_NAME_MAX_SIZE+1]; 
 bzero(file_name, FILE_NAME_MAX_SIZE+1); 
 printf("Please Input File Name On Server:t"); 
 scanf("%s", file_name); 
 
 char buffer[BUFFER_SIZE]; 
 bzero(buffer, BUFFER_SIZE); 
 strncpy(buffer, file_name, strlen(file_name)>BUFFER_SIZE?BUFFER_SIZE:strlen(file_name)); 
 
  
 if(sendto(client_socket_fd, buffer, BUFFER_SIZE,0,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0) 
 { 
  perror("Send File Name Failed:"); 
  exit(1); 
 } 
 
 close(client_socket_fd); 
 return 0; 
} 

Readers can refer to the previous post: (link: #) and note the comparison between UDP and TCP workflows. To deepen the understanding of the principles of the program.


Related articles: