当前位置:网站首页>Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time

Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time

2022-04-23 14:40:00 m0_ fifty-one million five hundred and fifty-one thousand three

1、I/O Another use of reuse : At the same time processing TCP and UDP service

Same port ( or socket Address ) Can receive UDP Requests can also be received TCP request . We use the same socket Create two addresses socket File descriptor , Respectively used to process the data on this port UDP and TCP request . And then use I/O Multiplexing technology listens to both socket File descriptor events , You can handle the data on one port at the same time TCP and UDP request .

2、 Example program

The following is a server program , He can handle... At the same time TCP and UDP service .

/*  This program implements a simultaneous processing  TCP  Request and  UDP  The requested echo server  */
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#include<sys/epoll.h>
#include<pthread.h>
#include<errno.h>

#define MAX_EVENT_NUMBER 1024
#define TCP_BUFFER_SIZE 512
#define UDP_BUFFER_SIZE 1024

int setnonblocking(int fd)
{
    
	int old_option = fcntl(fd, F_GETFL);
	int new_option  = old_option | O_NONBLOCK;
	fcntl(fd, F_SETFL, new_option);
	return new_option;
}

void addfd(int epollfd, int fd)
{
    
	epoll_event event;
	event.data.fd = fd;
	event.events = EPOLLIN | EPOLLET;
	epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
	setnonblocking(fd);
}

int main(int argc, char* argv[])
{
    
	if (argc <= 2)
	{
    
		return 1;
	}
	const char* ip = argv[1];
	int port = atoi(argv[2]);

	struct sockaddr_in address;
	bzero(&address, sizeof(address));
	address.sin_port = htons(port);
	address.sin_family = AF_INET;
	inet_pton(AF_INET, ip, &address.sin_addr);

	/*  establish  TCP socket, And bind it to the port  port  On  */
	int listenfd = socket(PF_INET, SOCK_STREAM, 0);
	assert(listenfd >= 0);

	int ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
	assert(ret != -1);

	ret = listen(listenfd, 5);
	assert(ret != -1);

	/*  establish  UDP socket, And bind it to the port  port  On  */
	bzero(&address, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_port = htons(port);
	inet_pton(AF_INET, ip, &address.sin_addr);
	
	int udpfd = socket(PF_INET, SOCK_DGRAM, 0);
	assert(udpfd >= 0);

	ret = bind(udpfd, (struct sockaddr*)&address, sizeof(address));
	assert(ret != -1);

	epoll_event events[MAX_EVENT_NUMBER];
	int epollfd = epoll_create(10);
	assert(epollfd != -1);
	/*  register  TCP socket  and  UDP socket  Read events on  */
	addfd(epollfd, listenfd);
	addfd(epollfd, udpfd);

	while (1)
	{
    
		int number = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
		if (number < 0)
		{
    
			printf("epoll failure\n");
			break;
		}

		for (int i = 0; i < number; i++)
		{
    
			int sockfd = events[i].data.fd;
			if (sockfd == listenfd)		/*  If it is  TCP  Listening in  socket */
			{
    
				struct sockaddr_in client_address;
				socklen_t client_addrlength = sizeof(client_address);
				int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
				addfd(epollfd, connfd);
			}
			else if (sockfd == udpfd)	/*  If it is  UDP  Of  socket */
			{
    
				char buf[UDP_BUFFER_SIZE];
				memset(buf, '\0', UDP_BUFFER_SIZE);
				struct sockaddr_in client_address;
				socklen_t client_addrlength = sizeof(client_address);

				ret = recvfrom(udpfd, buf, UDP_BUFFER_SIZE, 0, (struct sockaddr*)&client_address, &client_addrlength);
				if (ret > 0)
				{
    
					sendto(udpfd, buf, UDP_BUFFER_SIZE - 1, 0, (struct sockaddr*)&client_address, client_addrlength);
				}
			}
			else if (events[i].events & EPOLLIN)	/*  If it is  TCP  Can write event  */
			{
    
				char buf[TCP_BUFFER_SIZE];
				while (1)			/*  because  TCP  The transmission is a byte stream , So we need to use  while  Make sure all data is read out ,UDP You don't have to use  while */
				{
    
					memset(buf, '\0', TCP_BUFFER_SIZE);
					ret = recv(events[i].data.fd, buf, TCP_BUFFER_SIZE - 1, 0);
					if (ret < 0)
					{
    
						if (errno == EAGAIN || errno == EWOULDBLOCK)	/*  stay Vxorks and Windows On ,EAGAIN  be called  EWOULDBLOCK */
						{
    
							break;
						}
						close(sockfd);
						break;
					}
					else if (ret == 0)
					{
    
						close(sockfd);
					}
					else
					{
    
						send(sockfd, buf, ret, 0);
					}
				}
			}
			else
			{
    
				printf("something else happened\n");
			}
		}
	}

	close(listenfd);
	return 0;
}

版权声明
本文为[m0_ fifty-one million five hundred and fifty-one thousand three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435333737.html