从bind
系统调用的参数来看,一个socket
只能与一个socket
地址绑定,即一个socket
只能用来监听一个端口。因此,服务器如果要同时监听多个端口,就必须创建多个socket,并将它们分别绑定到各个端口上。这样一来,服务器程序就需要同时管理多个监听socket,I/O复用技术就有了用武之地。
即使是同一个端口,如果服务器要同时处理该端口上的TCP和UDP请求,则也需要创建两个不同的socket:一个是流socket,另一个是数据报socket,并将它们都绑定到该端口上。
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.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 old_option;
}
/* 添加epoll事件,ET边缘触发模式 */
void addfd(int epollfd, int fd)
{
struct epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
int main()
{
const char *ip = "127.0.0.1";
int port = 8080;
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
address.sin_port = htons(port);
/*创建TCP socket,并将其绑定到端口port上*/
int tcpfd = socket(PF_INET, SOCK_STREAM, 0);
ret = bind(tcpfd, (struct sockaddr *)&address, sizeof(address));
assert(ret != -1);
ret = listen(tcpfd, 5);
assert(ret != -1);
/*创建UDP socket,并将其绑定到端口port上*/
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
address.sin_port = htons(port);
int udpfd = socket(PF_INET, SOCK_DGRAM, 0);
ret = bind(udpfd, (struct sockaddr *)&address, sizeof(address));
assert(ret != -1);
/* 创建epoll事件表 */
struct epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(256);
assert(epollfd != -1);
/* 注册TCP socket和UDP socket上的可读事件 */
addfd(epollfd, tcpfd);
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;
// 如果是tcp连接事件,那么将其注册到epoll事件表中
if (sockfd == tcpfd)
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(tcpfd, (struct sockaddr *)&client_address, &client_addrlength);
addfd(epollfd, connfd);
}
// 如果是udp连接事件,那么
else if (sockfd == udpfd)
{
char buf[UDP_BUFFER_SIZE];
memset(buf, '\0', UDP_BUFFER_SIZE);
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
// 将udp中的数据读取到buf中
ret = recvfrom(udpfd, buf, UDP_BUFFER_SIZE - 1, 0,
(struct sockaddr *)&client_address, &client_addrlength);
// 如果收到了数据,打印
if (ret > 0)
{
printf("%s\n",buf);
}
}
// 如果是一个socket有数据输入
else if (events[i].events & EPOLLIN)
{
char buf[TCP_BUFFER_SIZE];
while (1)
{
memset(buf, '\0', TCP_BUFFER_SIZE);
ret = recv(sockfd, buf, TCP_BUFFER_SIZE - 1, 0);
if (ret < 0)
{
// 如果当前操作非阻塞,操作无法立即完成,那么先不做处理
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
// 如果是出现了其他错误,那么关闭socket
close(sockfd);
break;
}
// 如果数据已经读完
else if (ret == 0)
{
close(sockfd);
}
// 如果是其他情况,那么返回收到的数据
else
{
printf("%s\n",buf);
}
}
}
else
{
printf("something else happened\n");
}
}
}
close(tcpfd);
return 0;
}
主要的精髓在这里
int tcpfd = socket(PF_INET, SOCK_STREAM, 0);
int udpfd = socket(PF_INET, SOCK_DGRAM, 0);
看一下客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8080
#define BUFFER_SIZE 1024
int main() {
int tcp_socket, udp_socket;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
ssize_t bytes_sent;
// 创建TCP socket
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_socket == -1) {
perror("TCP socket creation failed");
exit(EXIT_FAILURE);
}
// 创建UDP socket
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_socket == -1) {
perror("UDP socket creation failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址信息
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
perror("Invalid server address");
close(tcp_socket);
close(udp_socket);
exit(EXIT_FAILURE);
}
// 连接到TCP服务器
if (connect(tcp_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
perror("TCP connection failed");
close(tcp_socket);
close(udp_socket);
exit(EXIT_FAILURE);
}
printf("Connected to server\n");
// 发送TCP消息给服务器
const char *tcp_message = "Hello, this is TCP message!\n";
bytes_sent = send(tcp_socket, tcp_message, strlen(tcp_message), 0);
if (bytes_sent == -1) {
perror("TCP send failed");
close(tcp_socket);
close(udp_socket);
exit(EXIT_FAILURE);
}
// 发送UDP消息给服务器
const char *udp_message = "Hello, this is UDP message!\n";
bytes_sent = sendto(udp_socket, udp_message, strlen(udp_message), 0, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (bytes_sent == -1) {
perror("UDP sendto failed");
close(tcp_socket);
close(udp_socket);
exit(EXIT_FAILURE);
}
// 关闭连接
close(tcp_socket);
close(udp_socket);
return 0;
}
仿真