#include
#define PORT 6666
#define IP "192.168.125.118"
int keyboardEvents(void);
int cliconnectEvents(int sfd,struct sockaddr_in* pCin,fd_set *preadfds,int *pmaxfd);
int cliDealEvents(int newfd,struct sockaddr_in pCin[],fd_set *preadfds,int* pmaxfd);
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 success\n");
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;
sin.sin_port = ntohs(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
}
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd=sfd;
struct sockaddr_in saveCin[1024-3];
char buf[128]="";
ssize_t res = 0;
int newfd = -1;
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 == res)
{
printf("time out ---------------");
break;
}
printf("__%d__\n",__LINE__);
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0)
continue;
if(0==i)
{
printf("触发键盘输入事件\n");
keyboardEvents();
}
else if(sfd==i)
{
printf("触发客户端连接事件\n");
cliconnectEvents(sfd,saveCin,&readfds,&maxfd);
}
else
{
printf("触发客户端交互事件\n");
cliDealEvents(i,saveCin,&readfds,&maxfd);
}
}
}
close(sfd);
return 0;
}
int cliDealEvents(int newfd,struct sockaddr_in pCin[],fd_set *preadfds,int* pmaxfd)
{
ssize_t res -1;
char buf[128]="";
res = recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] newfd=%d 客户端下线 __%d__",\
inet_ntoa(pCin[newfd -3].sin_addr),ntohs(pCin[newfd -3].sin_port),newfd,__LINE__);
close(newfd);
FD_CLR(newfd,preadfds);
for(;*pmaxfd>=0;*pmaxfd++)
if(FD_ISSET(*pmaxfd,preadfds))
break;
//while(!FD_ISSET(*pmaxfd,preadfds) && *pmaxfd -- >=0);
return 0;
}
printf("[%s:%d] newfd=%d :%s__%d__",\
inet_ntoa(pCin[newfd -3].sin_addr),ntohs(pCin[newfd -3].sin_port),newfd,buf,__LINE__);
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
return 0;
}
int cliconnectEvents(int sfd,struct sockaddr_in* pCin,fd_set *preadfds,int *pmaxfd)
{
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd;
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 客户端连接成功 __%d__",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
pCin[newfd-3] = cin;
FD_SET(newfd,preadfds);
*pmaxfd = *pmaxfd ? *pmaxfd : newfd;
return 0;
}
int keyboardEvents(void)
{
char buf[128]="";
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
printf(":%s\n",buf);
return 0;
}
#include
#define SER_PORT 6666 //服务器绑定的端口号
#define SER_IP "192.168.125.118" //服务器绑定的IP
int main(int argc, const char *argv[])
{
//socket 创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
//允许端口快速复用
int reuse = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速复用成功\n");
//地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr =inet_addr(SER_IP);
//非必须绑定 bind 绑定客户端的ip和端口
//connect 连接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect server success\n");
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
char buf[128]="";
ssize_t res = 0;
int newfd=-1;
int s_res = 0;
int maxfd = sfd;
while(1)
{
tempfds = readfds;
//调用IO多路复用函数,阻塞监测集合
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;
}
printf("__%d__\n",__LINE__);
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0) //若i代表的文件描述符不存在,则遍历下一个文件描述符
continue;
if(0 == i)
{
printf("触发键盘输入事件\n");
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
}
else
{
printf("触发服务器交互事件\n");
//send 发送信息给服务器
if(send(cfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
if(strcmp(buf,"quit")==0)
break;
bzero(buf,sizeof(buf));
//recv 接收服务器的信息
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] cfd=%d 服务器下线__%d__\n",\
SER_IP,SER_PORT,cfd,__LINE__);
break;
}
printf("[%s:%d] cfd=%d :%s __%d__\n",\
SER_IP,SER_PORT,cfd,buf,__LINE__);
}
}
//close 关闭套接字
close(cfd);
return 0;
}