#include<myhead.h>
#define MYIP "192.168.125.154"
#define MYPORT 9999
#define YOUIP "192.168.125.154"
#define YOUPORT 1888
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("socket success %d\n",cfd);int reuse=1;
int reuse = 1;
if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(YOUPORT);
cin.sin_addr.s_addr=inet_addr(YOUIP);if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(MYPORT);
sin.sin_addr.s_addr=inet_addr(MYIP);if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perro("connect error");
return -1;
}
printf("connect success\n");
//
struct poldfd pfds[2];pfds[0].fd==0;
pfds[0].events=POLLIN;pfds[1].fd=cfd;
pfds[1].events=POLLIN;
char wbuf[128]="";
while(1)
{
int res=poll(pfds,2,-1);
if(res==0)
{
printf("time out\n");
return -1;
}
else if(res==-1)
{
perror("poll error");
return -1;}
if(pfds[0].revents==NULL)
{
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen[wbuf]-1]=0;
if(strcmp(wbuf,"quit")==0)
{
break;
}
send(cfd,wbuf,sizeof(wbuf),0);}
if(pfds[1].revents==POLLIN)
{
bzero(wnuf,sizeof(wnuf));
recv(cfd,wbuf,sizeof(wbuf),0);
printf("收到服务器的消息为:%s\n",wbuf);
}
}
close(cfd);
return 0;
}
#include<myhead.h>
#define MYIP "192.168.243.131"
#define MYPORT 9999int main(int argc, const char *argv[])
{
//1、套接字的创建,也就是载体
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return 1;
}
printf("socket success\n");//2、绑定
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(MYPORT);
sin.sin_addr.s_addr=inet_addr(MYIP);if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");//3、listen模式
if(listen(sfd,128)==-1)
{
perror("listen erro");
return -1;
}
printf("listen success\n");//4、接受终端的申请
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
int newfd =-1;
/**********************///
fd_set readfds,tempfds;
FD_ZERO(&readfds);FD_SET(0,&readfds);
FD_SET(sfd,&readfds);//
int maxfd=sfd;
struct sockaddr_in cin_arr[1024];while(1)
{
//
tempfds =readfds;
int res =select(sfd+1,&tempfds,NULL,NULL);
if(res==0)
{
printf("time out\n");
return -1;
}
else if(res ==-1)
{
perror("slect error");
return -1;
}
//
for(int i=0;i<maxfd;i++)
{
if(!FD_ISSET(i,&tempfds))
{
continue;
}
//
if(i===sfd)
{
newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1)
{
perror("accept error");
return -1;
}
printf("[%s:%d]:已连接,%d",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);FD_SET(newfd,&readfds);
cin_arr[newfd]=cin;if(maxfd<newfd)
{
maxfd=newfd;
}
}
}
//
else if(i==0)
{
char buf[128]="";
fgets(buf7,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
printf("触发了键盘输入事件\n");//
for(int i=4;i<maxfd;i++)
{
send(i,buf,strlen(buf),0);
}}
else
{
//5、读写
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
// int ret=recv(newfd,rbuf,sizeof(rbuf),0);
int ret=recv(i,rbuf,sizeof(rbuf),0);
if(ret==0)
{
printf("客户端以下线\n");
close(i);
FD_CLR(i,&readfds);
for(int j=maxfd;j>sfd;i++)
{
if(FD_ISSET(i,&readfds))
{
maxfd=j;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);/* strcat(rbuf,"*_*");
send(newfd,rbuf,sizeof(rbuf),0);
printf("发送成功\n");
*/for(int i=4;i<maxfd;i++)
{
send(i,rbuf,strlen(rbuf),0);
}
}
}
}
//6、关闭
close(sfd);return 0;
}