运行效果:服务器并发,客户端并发,服务器可群发消息给客户端
客户端退出有提示
服务端:
#include <stdio.h>
#include "myhead.h"
int main(int argc, char const *argv[])
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("sockfd err.");
return -1;
}
struct sockaddr_in saddr, caddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[1]));
saddr.sin_addr.s_addr = INADDR_ANY;
socklen_t len = sizeof(saddr);
bind(sockfd, (struct sockaddr *)&saddr, len);
if (bind < 0)
{
perror("bind err .");
return -1;
}
puts("bind success");
listen(sockfd, 6);
if (listen < 0)
{
perror("listen err.");
return -1;
}
puts("listen success");
fd_set readfds, tempfds;
FD_ZERO(&readfds);
// 填表
FD_SET(sockfd, &readfds);
FD_SET(0, &readfds);
int maxfd = sockfd;
// 循环监听select
char buf[64] = {0};
while (1)
{
tempfds = readfds;
int ret = select(maxfd + 1, &tempfds, NULL, NULL, NULL);
if (ret < 0)
{
perror("select err.");
return -1;
}
// 判断表
if (FD_ISSET(sockfd, &tempfds))
{
int acceptfd;
if ((acceptfd = accept(sockfd, (struct sockaddr *)&caddr, &len)) < 0)
{
perror("acceptfd err");
return -1;
}
printf("Client%s:%d\n", inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port));
FD_SET(acceptfd, &readfds);
if (acceptfd > maxfd)
{
maxfd = acceptfd;
}
}
for (int i = 4; i <= maxfd; i++)
{
if (FD_ISSET(i, &tempfds))
{
int ret = recv(i, buf, sizeof(buf), 0);
if (ret < 0)
{
perror("recv err.");
return -1;
}
else if (ret == 0)
{
printf("客户端%d已退出\n", i);
close(i);
FD_CLR(i, &readfds);
}
else
{
printf("%d:%s\n", i, buf);
}
}
}
if (FD_ISSET(0, &tempfds))
{
bzero(buf,sizeof(buf));
fgets(buf, sizeof(buf), stdin);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
// printf("sever->%s\n", buf);
for (int i = 4; i <= maxfd; i++)
{
if (FD_ISSET(i, &readfds))
{
send(i, buf, sizeof(buf), 0);
}
}
}
}
close(sockfd);
return 0;
}
客户端
#include <stdio.h>
#include "myhead.h"
int main(int argc, char const *argv[])
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("sockfd err.");
return -1;
}
struct sockaddr_in saddr, caddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[2]));
saddr.sin_addr.s_addr = inet_addr(argv[1]);
socklen_t len = sizeof(saddr);
if(connect(sockfd,(struct sockaddr*)&saddr,len)<0)
{
perror("connect err");
return -1;
}
puts("connect success");
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sockfd,&readfds);
int maxfd=sockfd;
char buf[64]={0};
while(1)
{
tempfds=readfds;
int ret =select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(ret<0)
{
perror("select err");
return -1;
}
if(FD_ISSET(sockfd,&tempfds))
{
int ret=recv(sockfd,buf,sizeof(buf),0);
if(ret<0)
{
perror("recv err.");
return -1;
}else
{
printf("server:%s\n",buf);
}
}
if(FD_ISSET(0,&tempfds))
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
send(sockfd,buf,sizeof(buf),0);
}
}
return 0;
}
myhead.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>
#endif