#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define CLI_PORT 6666
#define CLI_IP "192.168.0.161"
#define SER_PORT 8888
#define SER_IP "192.168.0.161"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
fd_set readfds;
fd_set tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd = sfd;
int s_res = 0;
ssize_t res = 0;
char buf[128]= "";
while(1)
{
tempfds = readfds;
s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res < 0)
{
ERR_MSG("select");
return -1;
}
for(int i = 0;i <= maxfd;i++)
{
if(FD_ISSET(i,&tempfds) == 0)
{
continue;
}
if(0 == i)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(send(sfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
}
if(sfd == i)
{
bzero(buf,sizeof(buf));
res = recv(i,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
maxfd = 0;
close(i);
return -1;
}
else
{
strcpy(buf,"^_^");
printf("%s\n",buf);
}
}
}
}
return 0;
}
select客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.0.161"
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//如果不加这段代码,则服务器异常退出后,会导致端口号在30s~3min内释放不出来
//当程序结束后,想要让其他进程能够快速复用端口号,就可以使用下面的代码
//允许端口快速被重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
//填充地址信息结构体,真是的地址信息结构体根据地址族制定
//AF_INET : man 7 ip
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,ifconfig查看;
//注意,切换网络环境后,IP地址是会改变的
//将地址信息结构体绑定到套接字上
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
//将套接字设置为被动监听状态
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
// 创建一个读集合
fd_set readfds;
fd_set tempfds;
//清空集合
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
//将需要的文件描述符添加到集合中
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
//存储最大temp件描述符
int maxfd = sfd;
int s_res = 0;
ssize_t res = 0;
char buf[128] = "";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = 0;
struct sockaddr_in save_cin [1024-4];
//循环监测集合中是否有事件产生,
while(1)
{
tempfds = readfds;
s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res < 0)
{
ERR_MSG("select");
return -1;
}
printf("***********\n");
for(int i = 0;i<=maxfd; i++)
{
if(FD_ISSET(i,&tempfds) == 0)
{
continue;
}
//
//
if(i == 0)
{
printf("触发键盘输入事件\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
printf(":%s\n",buf);
}
else if(sfd == i)
{
printf("触发客户端链接事件\n");
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd = %d 连接成功\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
save_cin[newfd-4] = cin;
//将生成的newfd添加到集合中
FD_SET(newfd,&readfds);
//更新maxfd
maxfd = maxfd > newfd ? maxfd:newfd;
}
else
{
printf("触发客户端交互事件");
bzero(buf,sizeof(buf));
//接收
res = recv(i,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] newfd = %d 客户端断开链接\n",\
inet_ntoa(cin.sin_addr),ntohs(save_cin[i-4].sin_port), i);
close(i);
//从集合readfds中剔除i
FD_CLR(i,&readfds);
//更新maxfd
int j = 0;
for(j=maxfd;j>0;j--)
{
if(FD_ISSET(j,&readfds))
{
maxfd = j;
break;
}
}
}
else
{
printf("[%s:%d] newfd = %d : %s\n",\
inet_ntoa(save_cin[i-4].sin_addr),ntohs(save_cin[i-4].sin_port), i,buf);
}
}
}
}
close(newfd);
close(sfd);
return 0;
}
select服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 7777
#define SER_IP "192.168.31.28"
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体,因为connect函数中要通过地址信息结构体连接服务器;
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(SER_PORT); //服务器绑定的端口
sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器绑定的IP
//注意,切换网络环境后,IP地址是会改变的
//连接服务器
if(connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
//创建集合
struct pollfd fds[2];
//将需要监测的文件描述符以及事件添加到集合中
fds[0].fd = 0; //要监测0号文件描述符
fds[0].events = POLLIN; //要监测读事件
fds[1].fd = sfd; //要检测sfd文件描述符
fds[1].events = POLLIN; //要监测读事件
ssize_t res = 0;
char buf[128] = "";
int pres = 0;
//让内核去循环监测集合中的文件描述符;
//哪个文件描述符准备就绪就走对应的处理函数
while(1)
{
pres = poll(fds, 2, -1);
if(pres < 0)
{
ERR_MSG("poll");
return -1;
}
else if(0 == pres)
{
printf("time out...\n");
break;
}
//能运行到当前位置,则代表有文件描述符产生事件了;
//由于产生的事件可能是多个,其中包含了POLLIN,所以不能直接用==判断是否有POLLIN
//而是要通过按位与的方式,提取出POLLIN所代表的那一位是1还是0;
if(fds[0].revents & POLLIN) //0号文件描述符有事件产生
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//发送
if(send(sfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("sen");
return -1;
}
printf("send success\n");
}
if(fds[1].revents & POLLIN) //sfd有事件产生,即服务器发送信息给客户端
{
//接收
res = recv(sfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器断开连接\n");
break;
}
printf(":%s\n", buf);
}
}
close(sfd);
return 0;
}
poll客户端