#include <header.h>
#define SER_PORT 8888
#define SER_IP "192.168.122.49"
int main(int argc, const char *argv[])
{
//1、为通信创建一个端点
int sfd = socket(AF_INET, SOCK_STREAM, 0);
//参数1:说明使用的是ipv4通信域
//参数2:说明使用的是TCP面向连接的通信方式
//参数3:由于参数2中已经指定通信方式,填0即可
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success sfd = %d\n", sfd); //3
//2、绑定ip和端口号
//2.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); //ip地址
//2.2 绑定工作
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3、将套接字设置成被动监听状态
if(listen(sfd, 128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
//4、阻塞等待客户端的连接
//4.1 定义用于接受客户端信息的容器
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
fd_set readfds,rteval;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
int newfd=0;
int maxsfd=sfd;
struct sockaddr_in cin_addr[1024];
while(1)
{
rteval=readfds;
int res=select(maxsfd,&rteval,NULL,NULL,NULL);
if(res==-1)
{
perror("select error");
return -1;
}
else if(res==0)
{
printf("time out\n");
return -1;
}
for(int i=0;i<=maxsfd;i++)
{
if(!FD_ISSET(i,&rteval))
{
continue;
}
if(i==sfd)
{
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("[%s:%d]:发来连接请求\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
FD_SET(newfd,&readfds);
if(newfd>=maxsfd)
{
maxsfd=newfd;
}
cin_addr[newfd]=cin;
}
else if(i==0)
{
char rbuf[128]="";
fgets(rbuf,sizeof(rbuf),stdin);
rbuf[strlen(rbuf)-1]=0;
printf("%s\n",rbuf);
for(int k=4;k<=maxsfd;k++)
{
send(k,rbuf,strlen(rbuf),0);
}
}
else
{
//5、与客户端进行相互通信
char rbuf[128] = ""; //读取消息内容的容器
//清空容器
bzero(rbuf, sizeof(rbuf));
//从套接字中读取数据
//int res = read(newfd, rbuf, sizeof(rbuf));
int res = recv(i, rbuf, sizeof(rbuf), 0);
if(res == 0)
{
printf("客户端已经下线\n");
close(i);
FD_CLR(i,&readfds);
//更新
for(int j=maxsfd;j>=sfd;j--)
{
if(FD_ISSET(j,&readfds))
{
maxsfd=j;
break;
}
}
}
continue;
//将读取的消息展示出来
printf("[%s:%d]:%s\n", inet_ntoa(cin_addr[i].sin_addr), ntohs(cin_addr[i].sin_port), rbuf);
for(int k=4;k<=maxsfd;k++)
{
if(k!=i)
{
send(k,rbuf,strlen(rbuf),0);
}
}
}
}
}
close(sfd);
return 0;
}
#include <header.h>
#define CLI_PORT 6666
#define CLI_IP "192.168.125.101"
#define SER_PORT 8888
#define SER_IP "192.168.125.101"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return 1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&sin,sizeof(cin))==-1)
{
perror("bind");
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))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
struct pollfd fdskl[2];
fdskl[0].fd=0;
fdskl[0].events=POLLIN;
fdskl[1].fd=cfd;
fdskl[1].events=POLLIN;
/* int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n", cfd); //3
//2、绑定IP地址和端口号 ?
//2.1 填充客户端地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
//2.2 绑定
if( bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) ==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
//3、连接服务器
//3.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);
//3.2 连接服务器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
//使用poll完成多个阻塞任务的并发执行
struct pollfd fdskl[2];
fdskl[0].fd = 0; //表示检测0好文件描述符
fdskl[0].events = POLLIN; //表示检测读事件
fdskl[1].fd = cfd; //表示检测cfd文件描述符
fdskl[1].events = POLLIN; //表示检测读事件*/
char wbuf[128] = "";
char rbuf[128] = "";
while(1)
{
int res = poll(fdskl, 2, -1);
if(res == -1)
{
perror("poll error");
return -1;
}else if(res == 0){
printf("time out\n");
return -1;
}
if(fdskl[0].revents == POLLIN)
{
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1] = '\0';
send(cfd, wbuf, strlen(wbuf), 0);
printf("发送成功\n");
}
if(fdskl[1].revents == POLLIN)
{
bzero(rbuf, sizeof(rbuf));
recv(cfd, rbuf, sizeof(rbuf), 0);
printf("服务器发来的消息为:%s\n", rbuf);
}
}
close(cfd);return 0;
}