服务器端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> // See NOTES
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/select.h>
/*服务器端*/
// perror的宏定义
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d ", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.31.225" // 本机IP地址的宏定义
#define PORT 1024 // 端口号,1024~49151
int main(int argc, const char *argv[])
{
// 第一步创建流式套接字
// 通讯域名协议:ipv4,套接字:tcp,protocol:0为默认协议
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd=%d\n", sfd);
// 允许端口快速重用
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("端口快速重用成功\n");
// 填充服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; // 地址族,必须填 AF_INET
sin.sin_port = htons(PORT); // 端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); // 本机ip
// 第二步,将IP和端口绑定到套接字上
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
// 将套接字设置为监听状态
if (listen(sfd, 128) < 0) // 参数1:设置为监听状态的套接字,参数2;最大队列长队
{
ERR_MSG("listen");
return -1;
}
printf("监听成功:__%d__\n", __LINE__);
// 设置一个读集合
fd_set fds_read, fds_temp;
// 由于fds_read中需要防止要检测的文件描述符,所以不能让它是随机值
FD_ZERO(&fds_read);
FD_ZERO(&fds_temp);
// 将需要的文件描述符添加到集合中
FD_SET(0, &fds_read); // 将0号文件文件描述符添加到集合中
FD_SET(sfd, &fds_read); // 将客户端连接事件添加到集合中
struct sockaddr_in cin; // 储存连接成功的客户端地址信息
socklen_t addrlen = sizeof(cin);
struct sockaddr_in cin_save[1024 - 4]; // 将连接的客户端存储到 cin_save 中,从4号描述符开始
ssize_t res = 0;
int s_res = 0;
char buf[128] = "";
int newfd = -1; // 设置新连接的客户端初始化文件描述符
int maxfd = sfd; // 设置当前最大的文件描述符
while (1)
{
fds_temp = fds_read; // 防止fds_read被清空,所以复制一份
// 第一个参数:最大的文件描述符+1,第二个:检查可读性,第三个:检查可写性,第四个:检查外部数据,第四个:设置超时时间
s_res = select(maxfd + 1, &fds_temp, NULL, NULL, NULL); // 返回值 >0:就绪描述字的正数目,<0错误,==0超时
if (s_res < 0) // 运行错误
{
ERR_MSG("select");
return -1;
}
else if (0 == s_res) // 判断是否超时
{
printf("超时\n");
return -1;
}
// 运行到这里,代表有文件描述符准备就绪,则走触发事件对应的处理函数
for (int i = 0; i <= maxfd; i++) // 遍历从0开始,到最大文件描述符
{
// 如果不在集合中,直接往后继续
if (!FD_ISSET(i, &fds_temp))
{
continue;
}
// 如果在集合中,则触发对应事件
if (0 == i) // 设定的键盘触发事件
{
printf("键盘触发事件>>>> ");
// 向客户端发送消息
int sndfd = -1; // 指定要发送到的客户端 newfd
res = scanf("%d %s", &sndfd, buf); // 定义re接scanf的返回值
while (getchar() != 10)
; // 过滤垃圾字符
if (res != 2) // 格式输入不正确
{
fprintf(stderr, "请输入正确格式: int string\n");
continue;
}
// 能运行到当前位置,则代表输入的格式整数
if (sndfd <= sfd || sndfd >= 1024 || !FD_ISSET(sndfd, &fds_read))
{
fprintf(stderr, "sndfd=%d 文件描述符错误\n", sndfd);
continue;
}
if (send(sndfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
else if (sfd == i) // 客户端连接触发事件
{
printf("客户端连接事件>>>> \n");
fflush(stdout); // 刷新输出缓冲区
newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen); // 客户端连接到新的文件描述符
if (newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功__%d__\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
//
cin_save[newfd - 4] = cin; // 存储到抛开0,1,2,3的位置,所以是-4
// 将 newfd 添加到读集合中
FD_SET(newfd, &fds_read);
// 更新maxfd
maxfd = maxfd > newfd ? maxfd : newfd;
}
else // 客户端消息接收事件
{
bzero(buf, sizeof(buf));
/***** 以阻塞方式接收消息 *****/
res = recv(i, buf, sizeof(buf), 0);
if (res < 0) // recv函数运行失败
{
ERR_MSG("recv");
return -1;
}
else if (0 == res) // 客户端下线
{
printf("[%s:%d] newfd=%d 客户端下线\n",
inet_ntoa(cin_save[i - 4].sin_addr), ntohs(cin_save[i - 4].sin_port), i);
// 关闭下线的文件描述符
close(i);
// 从集合中剔除文件描述符
FD_CLR(i, &fds_read);
// 更新maxfd,从目前最大的文件描述符往小了判断,直达判断在集合中
// 那么该文件描述符就是最大的
int j = 0;
for (j = maxfd; j >= 0; j--)
{
if (FD_ISSET(j, &fds_read))
{
maxfd = j;
printf("%d\n", maxfd);
break;
}
}
if (j < 0) // 如果集合中没有要检测的文件描述符,则让maxfd=-1
maxfd = -1;
continue;
}
printf("[%s:%d] newfd=%d : %s\n", inet_ntoa(cin_save[i - 4].sin_addr), ntohs(cin_save[i - 4].sin_port), i, buf);
// send(i, buf, sizeof(buf), 0);
// 发送 将数据拼接字符串后发送回去
strcat(buf, "*_*");
if (send(i, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
}
}
close(sfd);
return 0;
}
客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <arpa/inet.h>
#define PORT 1024
#define IP "192.168.31.225" // 服务器IP
// perror的宏定义
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d ", __LINE__); \
perror(msg); \
} while (0)
// IO多路复用客户端
int main(int argc, const char *argv[])
{
// 创建流式套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0)
{
perror("socket");
return -1;
}
// 允许端口快速重用
int reuse = 1;
if (setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("端口快速重用成功\n");
// 填充客户端地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET; // 地址族
cin.sin_port = htons(PORT); // 网络字节序端口
cin.sin_addr.s_addr = inet_addr(IP); // 网络字节序地址
if (connect(cfd, (struct sockaddr *)&cin, sizeof(cin)) < 0) // 连接服务器
{
perror("connect");
return -1;
}
printf("__%d__服务器连接成功\n", __LINE__);
int maxfd = cfd; // 当前最大文件描述符号
fd_set readfds; // 设置一个读集合
FD_ZERO(&readfds); // 初始化readfds,不让它为随机值
FD_SET(0, &readfds); // 将0号添加到读集合中,stdin
FD_SET(cfd, &readfds); // 将客户端信息添加到读集合中
int res = -1; // select返回值
char buf[128] = "";
while (1)
{
res = select(maxfd + 1, &readfds, NULL, NULL, NULL);
printf("%d %d\n", maxfd, cfd);
if (res < 0)
{
perror("select");
return -1;
}
else if (0 == res) // 超时
{
printf("超时\n");
return -1;
}
if (FD_ISSET(0, &readfds)) // 检测到发送事件
{
/*发送消息*/
// ssize_t send(int sockfd, const void *buf, size_t len, int flags);
bzero(buf, sizeof(buf));
printf("触发键盘输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (send(cfd, buf, sizeof(buf), 0) < 0)
{
perror("send");
return -1;
}
printf("发送成功\n");
}
if (FD_ISSET(cfd, &readfds)) // 检测到接收事件
{
//bzero(buf, sizeof(buf)); // 清空buf
res = recv(cfd, buf, sizeof(buf), 0); // 以阻塞方式接收消息
if (res < 0)
{
perror("recv");
return -1;
}
else if (0 == res)
{
printf("client_fd=%d 服务器下线:__%d__\n", cfd, __LINE__);
break;
}
printf("client_fd=%d: %s\n", cfd, buf);
}
}
return 0;
}