Linux network programming|UDP programming example

UDP programming example

When using UDP protocol to communicate, the server and client do not need to establish a connection, as long as they know the address information of the other's socket, they can send data. The server only needs to create a socket to receive requests from different clients, and then send the results to the corresponding client after processing.
The process of using UDP on the server side and the client side is as follows:
Insert picture description here
server side code

/*****server.c*****/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 128

int main(int argc, char *argv[]){
    
    
	int sockfd;
	struct sockaddr_in servaddr, cliaddr;
	socklen_t peerlen;
	char[BUFFER_SIZE];

	if(argc < 3){
    
    
		printf("Usage: %s <ip> <port>\n",argv[0]);
		exit(-1);
	}

	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
    
    		//建立socket连接
		perror("socket");
		exit(-1);
	}
	print("sockfd = %d\n",sockfd);
	
	//设置sockaddr_in结构体中相关参数
	bzero(&servaddr,szieof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){
    
    	//bind()绑定地址信息
		perror("bind");
		exit(-1);
	}
	printf("bind success!\n");
	
	peerlen = sizeof(cliaddr);		
	while(1){
    
    		//调用recvfrom()函数接收客户端发送的数据
		if(recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&cliaddr, &peerlen) < 0){
    
    
			perror("recvfrom");
			exit(-1);
		}
		printf("Received a message: %s\n",buf);
		strcpy(buf, "Welcome to server");
		sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&cliaddr, peerlen);
	}
	
	close(sockfd);
	exit(0);
}

Client code

/*****client.c*****/
//头文件同server.c
#define BUFFER_SIZE 128

int main(int argc, char *argv[]){
    
    
	int sockfd;
	char buf[BUFFER_SIZE] = "Hello Server";
	struct sockaddr_in servaddr;

	if(argc < 3){
    
    
		printf("Usage: %s <ip> <port>\n",argv[0]);
		exit(-1);
	}
	
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
    
    		//创建socket
		perror("socket");
		exit(-1);
	}
	
	//设置sockaddr_in结构体中相关参数
	bzero(&servaddr,szieof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);

	sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));	//发送消息给服务器端
	if(recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL) < 0){
    
    
		perror("recvfrom");
		exit(-1);
	}
	printf("Recv from server: %s\n",buf);
	close(sockfd);
	exit(0);
}

When running, start the server first, then start the client

linux@linux-virtual-machine:~/andy/net$ ./server 192.168.1.100 9999
listenfd = 3
bind success!
Listening...
Received a message: Hello Server
linux@linux-virtual-machine:~/andy/net$ ./client 192.168.1.100 9999
Recv from server: Welcome to server

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108428049