l8-d9 UDP通信实现

一、函数接口扩展与UDP通信实现流程

1.write/read到send/recv

函数原型:

        ssize_t send(int sockfd, const void *buf, size_t len, int flags);

        ssize_t recv(int sockfd, void *buf, size_t len, int flags);

前三个参数同read/write一样;

         ssize_t read(int fd, void *buf, size_t count);

        ssize_t write(int fd, const void *buf, size_t count);

常见flags:

        1. 一般设置为0

        2. MSG_PEEK:窥视传入的数据。数据被复制到缓冲区中,但不会从输入队列中删除。

         3. MSG_OOB:处理带外(OOB)数据

2.sendto与recvfrom

函数原型:

        ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);

        ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);

1. 前四个参数同recv/send一样;

2. 后两个参数是通信结构体和结构体的宽度; 

3.UDP通信的实现过程l8-d9 UDP通信实现_第1张图片

二、UDP服务器代码 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
	int fd;
	struct sockaddr_in addr;
	char buf[BUFSIZ] = {};
	if(argc < 3){
		fprintf(stderr, "%s\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	/*创建套接字*/
	if( (fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0){
		perror("socket");
		exit(EXIT_FAILURE);
	}
	/*设置通信结构体*/
	bzero(&addr, sizeof(addr) );
	addr.sin_port = htons( atoi(argv[2]) );
	if(inet_aton(argv[1], &addr.sin_addr) == 0) {
		fprintf(stderr, "Invalid address\n");
		exit(EXIT_FAILURE);
	}
	/*绑定通信结构体*/
	if(bind(fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1){
		perror("bind");
		exit(EXIT_FAILURE);
	}
	while(1){
		bzero(buf, BUFSIZ);
		recvfrom(fd, buf, BUFSIZ, 0, NULL, NULL);
		printf("buf=%s\n", buf);
	}
	close(fd);
	return 0;
}

三、客户端

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
	int fd;
	struct sockaddr_in addr;
	char buf[BUFSIZ] = {};
	socklen_t addrlen = sizeof(addr);
	if(argc < 3){
		fprintf(stderr, "%s\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	/*创建套接字*/
	if( (fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0){
		perror("socket");
		exit(EXIT_FAILURE);
	}
	/*设置通信结构体*/
	bzero(&addr, sizeof(addr) );
	addr.sin_port = htons( atoi(argv[2]) );
	if(inet_aton(argv[1], &addr.sin_addr) == 0) {
		fprintf(stderr, "Invalid address\n");
		exit(EXIT_FAILURE);
	}
	while(1){
		bzero(buf, BUFSIZ);
		printf("Input->");
		fgets(buf, BUFSIZ, stdin);
		sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&addr, addrlen);
	}
	close(fd);
	return 0;
}

Makefile

CC=gcc
CFLAGS=-Wall
all:server client

clean:
    rm server client
 

你可能感兴趣的:(udp,网络协议,网络)