标签:ddr 核心 ems 简单 cal ant bit ohs return
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);
//原型
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
//我的代码中的
nselect = select(fd_max + 1, &readfds, nullptr, nullptr, nullptr);
注意:
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);
#include "include/wrap.h"
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h> /* See NOTES */
#include <unistd.h>
#include <wait.h>
#define LOCALIP "127.0.0.1"
#define PORT 6666
void handler(char *in, char *out) {
for (int i = 0; i < (int)strlen(out) + 1; ++i) {
out[i] = toupper(in[i]);
}
}
int workthread(const int &fd_client) {
char recvbuf[2048] = {0};
char sendbuf[2048] = {0};
int ret = 0;
ret = (int)Read(fd_client, recvbuf, 2048);
if (ret <= 0) {
printf("ret==0\n");
return ret;
}
handler(recvbuf, sendbuf);
ret = (int)Write(fd_client, sendbuf, strlen(sendbuf) + 1);
return ret;
}
void startsock(int &fd, struct sockaddr_in &addr, const char *ip,
const int port) {
fd = Socket(AF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
}
int main() {
int fd_server = 0;
int fd_client = 0;
int ret = 0;
struct sockaddr_in sock_client;
struct sockaddr_in sock_server;
socklen_t client_len = (socklen_t)sizeof(sock_client);
int opt = 0;
fd_set readfds;//作为select的参数
fd_set allfds;//保存fds,因为readfds会被修改, 见 1.1 select 注意
int fd_clients[FD_SETSIZE]; //用于保存客户端连接的fd
memset(fd_clients, -1, sizeof(fd_clients)); //用-1表明是没有用过的fd,方便后面找空位
int maxi = 0;
int fd_max = 0;
int nselect = 0;
int i = 0;
startsock(fd_server, sock_server, LOCALIP, PORT);
opt = 1;
Setsockopt(fd_server, SOL_SOCKET, SO_REUSEADDR, &opt,
(socklen_t)sizeof(opt));
Bind(fd_server, (struct sockaddr *)&sock_server, sizeof(sock_server));
Listen(fd_server, 5);
FD_ZERO(&allfds);
FD_ZERO(&readfds);
FD_SET(fd_server, &allfds);//把listenfd加入待监听的
fd_max = fd_server;
while (true) {
readfds = allfds; //见 1.1 select 注意
printf("selecting...\n");
nselect = select(fd_max + 1, &readfds, nullptr, nullptr, nullptr);
if (nselect == -1) {
if (errno == EINTR) {
continue;
} else {
perror_exit("select failed");
}
}
printf("get %d select\n", nselect);
if (FD_ISSET(fd_server, &readfds)) { //看看listenfd有没有信号,就是有没有新连接
fd_client =
Accept(fd_server, (struct sockaddr *)&sock_client, &client_len);
printf("accept: %s: %d\n", inet_ntoa(sock_client.sin_addr),
ntohs(sock_client.sin_port));
for (i = 0; i < FD_SETSIZE; ++i) {
if (fd_clients[i] != -1) continue; //找数组中没有用过的空位存fd_client
fd_clients[i] = fd_client;
break;
}
printf("i: %d, FD_SETSIZE: %d\n", i, FD_SETSIZE);
if (i == FD_SETSIZE) perror_exit("too many clients");//超过1024
if (i > maxi) maxi = i;
if (fd_client > fd_max) fd_max = fd_client;//找最大的fd
FD_SET(fd_client, &allfds);//加入监听集合
nselect--;
}
printf("going to find client, maxi: %d, nselect: %d\n", maxi, nselect);
for (i = 0; (i <= maxi) && (nselect > 0); ++i) {//maxi为了节约时间,不用轮询1024次
if (FD_ISSET(fd_clients[i], &readfds) == 0) continue;//轮询哪个clientfd有信号
printf("find client %d\n", i + 1);
ret = workthread(fd_clients[i]);//处理
if (ret <= 0) {//断开
Close(fd_clients[i]);
FD_CLR(fd_clients[i], &allfds);
fd_clients[i] = -1;
}
nselect--;
}
sleep(3);
}
Close(fd_server);
for (i = 0; i < FD_SETSIZE; ++i) {
if (fd_clients[i] == -1) continue;
Close(fd_clients[i]);
}
}
注意: 代码中maxi和fd_max的操作看上去很繁琐,其实都是为了节约时间,例如
参考网址:https://www.cnblogs.com/alantu2018/p/8612722.html#top
Linux C++ 网络编程学习系列(2)——多路IO之select实现
标签:ddr 核心 ems 简单 cal ant bit ohs return
原文地址:https://www.cnblogs.com/whuwzp/p/linux_cpp_network_2.html