#include<myhead.h>
#define IP "192.168.115.164"
#define PORT 8888
int main(int argc, const char *argv[])
{
int sfd,newsfd;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
return -1;
}
printf("%d\n",sfd);
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
}
struct sockaddr_in sin,cins[1024],cin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
socklen_t addrlen=sizeof(cins[0]);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
int maxfd=sfd;
int res=0;
char buf[128]="";
ssize_t ret=0;//recv的返回值
int i;
while(1)
{
tempfds=readfds;
res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if((res)<0)
{
ERR_MSG("select");
return -1;
}
else if(0==res)
{
printf("timeout\n");
break;
}
else
{
for(i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds))
{
if(0==i)
{
fgets(buf,sizeof(buf),stdin);
printf("有输入事件\n");
}
else if(sfd==i)
{
if((newsfd=accept(sfd,(struct sockaddr*)(&cin),&addrlen))<0)
{
ERR_MSG("accept");
return -1;
}
cins[newsfd]=cin;
printf("%s[%d]客户端已上线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
printf("%d\n",i);
FD_SET(newsfd,&readfds);
if(newsfd>maxfd)
{
maxfd=newsfd;
}
}
else
{
bzero(buf,sizeof(buf));
if((ret=recv(i,buf,sizeof(buf),0))<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==ret)
{
printf("%d\n",i);
printf("%s[%d]客户端已下线\n",inet_ntoa(cins[i].sin_addr),ntohs(cins[i].sin_port));
if(close(i)<0)
{
ERR_MSG("close");
return -1;
}
FD_CLR(i,&readfds);
while(!FD_ISSET(maxfd,&readfds)&&maxfd>=0)
{
maxfd--;
}
}
else
{
printf("%s[%d]:%s\n",inet_ntoa(cins[i].sin_addr),ntohs(cins[i].sin_port),buf);
}
}
}
}
}
}
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
#include<myhead.h>
#define IP "192.168.115.164"
#define PORT 8888
int main(int argc, const char *argv[])
{
int cfd;
if((cfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(connect(cfd,(struct sockaddr*)(&sin),sizeof(sin))<0)
{
ERR_MSG("connect");
}
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
char buf[128]="";
int res=0;
ssize_t ret;
while(1)
{
tempfds=readfds;
res=select(cfd+1,&tempfds,NULL,NULL,NULL);
if(res<0)
{
ERR_MSG("select");
return -1;
}
else if(0==res)
{
printf("timeout\n");
break;
}
else
{
if(FD_ISSET(0,&tempfds))
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if((ret=send(cfd,buf,sizeof(buf),0))<0)
{
ERR_MSG("send");
}
}
else if(FD_ISSET(cfd,&tempfds))
{
bzero(buf,sizeof(buf));
if((res=recv(cfd,buf,sizeof(buf),0))<0)
{
ERR_MSG("recv");
}
else if(0==res)
{
printf("%s[%d]服务器掉线\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
break;
}
else
{
printf("%s[%d]:%s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
}
}
}
}
if(close(cfd)<0)
{
ERR_MSG("close");
}
return 0;
}