select客户端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <poll.h>
//=====客户端代码=======
#define ERR_MSG(msg) do{\
fprintf(stdout,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.2.114"
int main(int argc, const char *argv[])
{
//套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//端口快速被重用
int resue = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(SER_PORT);
cin.sin_addr.s_addr = inet_addr(SER_IP);
//绑定
if(connect(cfd, (struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("connect");
return -1;
}
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
char buf[128] = "";
ssize_t res = -1;
int s_res = 0;
while(1)
{
tempfds = readfds;
s_res = select(cfd+1,&tempfds,NULL,NULL,NULL);
if(s_res < 0)
{
ERR_MSG("select");
return -1;
}else if(0 ==s_res)
{
printf("time out...\n");
break;
}
if(FD_ISSET(0,&tempfds))
{
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;
}
}
if(FD_ISSET(cfd,&tempfds))
{
bzero(buf,sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(0 == res)
{
printf("服务端关闭\n");
break;
}
printf(":%s\n",buf);
}
}
//关闭文件描述符
close(cfd);
return 0;
}
select服务端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888 //1024-49151
#define IP "192.168.2.114" //本机IP ipconfig 可以查看
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d \n",sfd);
//设置允许端口快速被重用
int resue = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充服务器的地址信息结构体
//真实的地址信息结构体根据地址族执行,AF_INET : man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填 AF_INET
sin.sin_port = htons(PORT); // 端口号的网络字节序
sin.sin_addr.s_addr =inet_addr(IP); //IP地址的网络字节
//绑定--必须绑定
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//将套接字设置为被动监听状态
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//功能:阻塞函数,阻塞等待客户端连接成功
//当客户连接成功后,会从已完成链接的队列头中获取一个客户端信息
//获取连接成功的文件描述符
struct sockaddr_in saveCin[1024-4];
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = -1;
char buf[128]="";
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
ssize_t res = 0;
int maxfd =sfd;
int s_res = 0;
while(1)
{
tempfds = readfds;
s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res < 0)
{
ERR_MSG("select");
return -1;
}else if(0 == s_res)
{
printf("time out ....\n");
break;
}
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0)
{
continue; // i所在的文件描述符不在集合中
}
//i代表的文件描述符在集合中
//所以需要判断i所达标的文件描述符是什么
//0==fgets sfd==accpet 其他走recv
if(0 == i)
{
int sndfd;
res = scanf("%d %s",&sndfd,buf);
if(res !=2)
{
fprintf(stderr,"请输入正确格式:文件描述符 内容\n");
continue;
}
//运行到当前位置,代表格式正确
if(sndfd >= 1024||sndfd <= sfd || !FD_ISSET(sndfd,&readfds))
{
fprintf(stderr,"sndfd = %d 没有客户端\n",sndfd);
continue;
}
if(send(sndfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
/*fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
printf("%s\n",buf);*/
}
else if(sfd == i)
{
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),sfd);
saveCin[newfd-4]=cin;
//将新生成的文件描述符添加到集合中
FD_SET(newfd,&readfds);
//更新maxfd
maxfd=newfd>maxfd?newfd:maxfd;
}
else
{
bzero(buf,sizeof(buf));
res = recv(i,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
fprintf(stderr,"[%s : %d] newfd = %d 客户端下线\n",\
inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i);
//关闭文件描述符
close(i);
//将该文件描述符从集合中剔除
FD_CLR(i,&readfds);
//更新maxfd
for(int j=maxfd;j>sfd;j--)
{
if(FD_ISSET(j,&readfds))
{
maxfd=j;
break;
}
}
}else
{
printf("[%s : %d] newfd = %d : %s\n",\
inet_ntoa(saveCin[i-4].sin_addr), ntohs(saveCin[i-4].sin_port), i, buf);
strcat(buf,"*_*");
if(send(i,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
}
}
}
//关闭所有套接字文件描述符
close(sfd);
return 0;
}
poll客户端
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <poll.h>
//=====客户端代码=======
#define ERR_MSG(msg) do{\
fprintf(stdout,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 8888
#define SER_IP "192.168.2.114"
int main(int argc, const char *argv[])
{
//套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//端口快速被重用
int resue = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
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(cfd, (struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
struct pollfd fds[2];
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = cfd;
fds[1].events =POLLIN;
char buf[128] = "";
ssize_t res = -1;
int p_res = 0;
while(1)
{
p_res = poll(fds,2,-1);
if(p_res < 0)
{
ERR_MSG("poll");
return -1;
}else if(0 ==p_res)
{
printf("time out...\n");
break;
}
if(fds[0].events & POLLIN)
{
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;
}
}
if(fds[1].events & POLLIN)
{
bzero(buf,sizeof(buf));
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(0 == res)
{
printf("服务端关闭\n");
break;
}
printf(":%s\n",buf);
}
}
//关闭文件描述符
close(cfd);
return 0;
}