【米尔王牌产品MYD-Y6ULX-V2开发板试用体验】创建TCP通信 - 米尔科技 - 电子技术论坛 - 广受欢迎的专业电子论坛! (elecfans.com)
前面创建了socket 单线程的通信。如果客端连接断开后,主服务端也就断开。学习了博客园的@liangf27的帖子来实现单线程服务多个客户端。
修改main.c代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/epoll.h>
#include "ssd1306.h"
const int PORT = 8888;
void listen_loop();
void accept_conn(unsigned int sock_fd, unsigned int epollfd);
void recv_message(unsigned int sock_fd);
int main(void) {
int sock_fd;
struct sockaddr_in server_addr;
SSD1306_init();
SSD1306_clearDisplay();
SSD1306_setBrightness(255);
SSD1306_setPageMode();
SSD1306_setTextXY(0,0);
SSD1306_putString("HELLO MYD-Y6ULX!");
SSD1306_setTextXY(1,0);
SSD1306_putString("SSD1306 DEMO");
SSD1306_setTextXY(2,0);
SSD1306_putString("2022-11-07");
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0) {
perror("socket:");
return 0;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind:");
return 0;
}
if (listen(sock_fd, 10) < 0) {
perror("listen");
return 0;
}
listen_loop(sock_fd);
return 0;
}
void accept_conn(unsigned int sock_fd, unsigned int epollfd) {
struct sockaddr_in clientaddr;
struct epoll_event event;
socklen_t len = sizeof(struct sockaddr);
int accept_fd = 0;
accept_fd = accept(sock_fd, (struct sockaddr*)&clientaddr, &len);
if (accept_fd <= 0) {
perror("accept error");
return;
}
event.data.fd = accept_fd;
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
epoll_ctl(epollfd, EPOLL_CTL_ADD, accept_fd, &event);
return;
}
void recv_message(unsigned int sock_fd) {
char recv_buf[1024], send_buf[1024];
memset(recv_buf, 0, sizeof(recv_buf));
memset(send_buf, 0, sizeof(send_buf));
recv(sock_fd, recv_buf, sizeof(recv_buf), 0);
fputs(recv_buf, stdout);
strcpy(send_buf, recv_buf);
SSD1306_clearDisplay();
SSD1306_setTextXY(3,0);
SSD1306_putString(send_buf);
send(sock_fd, send_buf, sizeof(send_buf), 0);
return;
}
void listen_loop(unsigned int sock_fd)
{
int epollfd, i, ret;
int timeout = 300;
struct epoll_event event;
struct epoll_event eventList[10];
epollfd = epoll_create(10);
event.events = EPOLLIN | EPOLLET;
event.data.fd = sock_fd;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock_fd, &event) < 0) {
printf("register epoll event err !");
return;
}
while (1) {
ret = epoll_wait(epollfd, eventList, 10, timeout);
if (ret < 0) {
printf("epoll event err!");
break;
}
else if (ret == 0) {
continue;
}
for (i = 0; i < ret; i++) {
if ((eventList[i].events & EPOLLERR) || (eventList[i].events & EPOLLHUP) || !(eventList[i].events & EPOLLIN)) {
printf("epoll error\n");
close(eventList[i].data.fd);
exit(-1);
}
if (eventList[i].events & EPOLLRDHUP) {
printf("//one client close the conne.//\n");
close(eventList[i].data.fd);
}
if (eventList[i].data.fd == sock_fd) {
accept_conn(sock_fd, epollfd);
}
else {
recv_message(eventList[i].data.fd);
}
}
}
close(epollfd);
close(sock_fd);
return;
}
【实验现象】:
可以实现多客户端连接,详见底部视频。