select实现多路复用服务器
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, " line__%d__\n", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.0.112"
int main(int argc, char const* argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
return -1;
}
// 填充服务器地址信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(5555);
cin.sin_addr.s_addr = inet_addr(IP);
// 绑定
if (bind(sfd, (struct sockaddr*)&cin, sizeof(cin)) < 0) {
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
// 监听
if (listen(sfd, 128) < 0) {
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
fd_set nefd, readfds; // 创建集合
FD_ZERO(&readfds);
FD_ZERO(&nefd);
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int maxfd = sfd; // 存储集合中最大的文件描述符
struct sockaddr_in savebin[1024 - 4]; // 另存链接成功的客户端地址信息
struct sockaddr_in bin; // 存储客户端地址信息
socklen_t addrlen = sizeof(bin);
int newfd = -1;
char buf[1024] = { 0 };
size_t ret = 0;
int s_res = 0;
while (1) {
nefd = readfds;
s_res = select(maxfd + 1, &nefd, NULL, NULL, NULL);
if (s_res < 0) {
ERR_MSG("select");
return -1;
} else if (s_res == 0) {
printf("time out...\n");
break;
}
printf("__%d__\n", __LINE__);
for (int i = 0; i <= maxfd; i++) {
if (FD_ISSET(i, &nefd) == 0) { // i所代表的文件描述符不在集合中
continue;
}
if (0 == i) {
printf("触发键盘输入>>>\n");
int numfd;
ret = scanf("%d %s", &numfd, buf);
while (getchar() != '\n')
;
if (ret != 2) {
printf("请输入正确格式:fd string\n ");
continue;
}
if (numfd <= sfd || numfd >= 1024 || !FD_ISSET(numfd, &readfds)) {
fprintf(stderr, "sndfd =%d是非法的文件描述符\n", numfd);
continue;
}
if (send(numfd, buf, sizeof(buf), 0) < 0) {
ERR_MSG("send");
return -1;
}
printf("send to %d success\n", numfd);
} else if (sfd == i) {
printf("触发客户端连接事件>>>\n");
newfd = accept(sfd, (struct sockaddr*)&bin, &addrlen);
if (newfd < 0) {
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d 连接成功__%d__\n", inet_ntoa(bin.sin_addr), ntohs(bin.sin_port), newfd, __LINE__);
savebin[newfd - 4] = bin;
FD_SET(newfd, &readfds);
// 更新maxfd
maxfd = maxfd > newfd ? maxfd : newfd;
} else {
fprintf(stderr, "触发客户端交互>>>\n");
bzero(buf, sizeof(buf));
// 接收
ret = recv(i, buf, sizeof(buf), 0);
if (ret < 0) {
ERR_MSG("recv");
return -1;
} else if (0 == ret) {
printf("客户端下线\n");
close(i);
FD_CLR(i, &readfds);
int j = maxfd;
for (; j >= 0; j--) {
if (FD_ISSET(j, &readfds)) {
break;
}
}
maxfd = j;
continue;
}
printf("[%s : %d] newfd = %d :%s__%d__\n", inet_ntoa(savebin[i - 4].sin_addr), ntohs(savebin[i - 4].sin_port), i, buf, __LINE__);
// 发送
strcat(buf, "*_*");
if (send(i, buf, sizeof(buf), 0) < 0) {
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
}
}
close(sfd);
return 0;
}
select实现客户端多路复用
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do { \
fprintf(stderr, " line__%d__\n", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.0.112"
int main(int argc, char const* argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
ERR_MSG("socket");
return -1;
}
// 填充服务器地址信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(5555);
cin.sin_addr.s_addr = inet_addr(IP);
if (connect(sfd, (struct sockaddr*)&cin, sizeof(cin)) < 0) {
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
fd_set nefd, readfds; // 创建集合
FD_ZERO(&readfds);
FD_ZERO(&nefd);
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int maxfd = sfd; // 存储集合中最大的文件描述符
// struct sockaddr_in savebin[1024 - 4]; // 另存链接成功的客户端地址信息
struct sockaddr_in bin; // 存储客户端地址信息
socklen_t addrlen = sizeof(bin);
char buf[1024] = { 0 };
size_t ret = 0;
int s_res = 0;
while (1) {
nefd = readfds;
s_res = select(sfd + 1, &nefd, NULL, NULL, NULL);
if (s_res < 0) {
ERR_MSG("select");
return -1;
} else if (s_res == 0) {
printf("time out...\n");
break;
}
printf("__%d__\n", __LINE__);
if (FD_ISSET(0, &nefd)) { // i所代表的文件描述符不在集合中
printf("触发键盘输入>>>\n");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
strcat(buf, "*_*");
if (send(sfd, buf, sizeof(buf), 0) < 0) {
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
if (FD_ISSET(sfd, &nefd)) {
fprintf(stderr, "触发服务器交互>>>\n");
bzero(buf, sizeof(buf));
// 接收
ret = recv(sfd, buf, sizeof(buf), 0);
if (ret < 0) {
ERR_MSG("recv");
return -1;
} else if (0 == ret) {
printf("服务器下线\n");
break;
}
printf("__%s__%d__\n", buf, __LINE__);
}
}
close(sfd);
return 0;
}
poll实现多复路
#include <arpa/inet.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP "192.168.0.112"
#define ERR_MSG(msg) \
do { \
fprintf(stderr, "line_%d_", __LINE__); \
perror(msg); \
} while (0)
int main(int argc, char const* argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0) {
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(5555);
sin.sin_addr.s_addr = inet_addr(IP);
if (connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
ERR_MSG("connect");
return -1;
}
printf("连接成功\n");
struct pollfd nfds[2];
nfds[0].fd = 0;
nfds[1].events = POLLIN;
nfds[1].fd = cfd;
nfds[1].events = POLLIN;
char buf[1024] = { 0 };
size_t ret = 0;
int p_res = -1;
while (1) {
p_res = poll(nfds, 2, -1);
if (p_res < 0) {
ERR_MSG("poll");
return -1;
} else if (0 == p_res) {
printf("time out ....\n");
break;
}
if (nfds[0].revents & POLLIN) {
printf("触发键盘输入事件\n");
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
// 发送
if (send(cfd, buf, sizeof(buf), 0) < 0) {
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
if (nfds[1].revents & POLLIN) {
printf("触发服务器交互事件\n");
bzero(buf, sizeof(buf));
ret = recv(cfd, buf, sizeof(buf), 0);
if (ret < 0) {
ERR_MSG("recv");
return -1;
} else if (ret == 0) {
printf("服务器下线\n");
break;
}
printf("%s__%d__\n", buf, __LINE__);
}
}
close(cfd);
return 0;
}