用socket实现一个多人聊天室的思路很简单,即在服务器端定义一个fd的int型数组,用来存储已经连接的客户端的socket连接套接字fd(因为发送和接收数据都只需要借助连接套接字fd),当服务器接收到来自某一客户端的数据时,直接转发给其他所有连接着的客户端,即完成了多人聊天室
服务器端:多线程进行数据的转发,记录聊天记录,向客户端发送进入退出聊天室提醒。
客户端:在主线程内发送数据,多线程接收其他客户端发的数据。输入“bye”退出客户端,
TCP.server
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 4567
typedef struct
{
int fd;
char buff[100];
}MSG;
int sockfd;//服务器socket
int fds[10];//客户端的socketfd,10个元素,
int size =10 ;//用来控制进入聊天室的人数为10以内
char buf3[100] ={0};
typedef struct sockaddr SA;
typedef struct sockaddr_in SIN;
//给所有用户发送信息
void SendMsgToAll(MSG* msg)
{
MSG send_msg = *msg;
for (int i = 0;i < size;i++)
{
if (fds[i] != 0)
{
if(fds[i] == send_msg.fd)
continue;
printf("sendto%d\n",fds[i]);
send(fds[i],send_msg.buff,sizeof(send_msg.buff),0);
FILE * fp = fopen("history.txt","a+");
fwrite(send_msg.buff,strlen(send_msg.buff),1,fp);
fclose(fp);
}
}
}
//信息接收线程
void* service_thread(void* p)
{
int fd = (int)p;
printf(“pthread = %d\n”,fd);
while(1)
{
char buf[100] = {0};
memset(buf,0,100);
if (recv(fd,buf,sizeof(buf),0) <= 0) //当用户fd发生变化时遍历所有fd发送退出信息
{
int i;
for (i = 0;i < size;i++)
{
if (fd == fds[i])
{
fds[i] = 0;
break;
}
}
printf(“退出:fd = %dquit\n”,fd);
memset(buf3,0,100);
sprintf(buf3,"%s退出了聊天室",buf);
for (int i = 0;i < size;i++)
{
if (fds[i] != 0)
{
if(fds[i] == fd){
continue;
}
printf(“sendto%d\n”,fds[i]);
send(fds[i],buf3,strlen(buf3),0);
}
}
memset(buf3,0,100);
pthread_exit((void*)i);
}
else
{
if(strcmp(buf,"chat_history") == 0)
{
memset(buf,0,100);
FILE * fp = fopen("history.txt","r");
//fseek(fp,0,SEEK_SET);
fread(buf,1,100,fp);
fclose(fp);
send(fd,buf,sizeof(buf),0);
memset(buf,0,100);
continue;
}
//把服务器接受到的信息发给所有的客户端
MSG msg_info;
msg_info.fd = fd;
strcpy(msg_info.buff,buf);
SendMsgToAll(&msg_info);
}
}
}
//等待客户端连接
void service()
{
printf(“服务器启动,等待连接\n”);
while(1)
{
SIN fromaddr;
socklen_t len = sizeof(fromaddr);
int fd = accept(sockfd,(SA*)&fromaddr,&len);
if (fd == -1)
{
printf("客户端连接出错...\n");
continue;
}
//记录客户端的socket
int i = 0;
for (i = 0;i < size;i++)
{
if (fds[i] == 0)
{
fds[i] = fd;
printf("进入聊天室的fd:%d\n",fd);
//有客户端连接之后,启动线程给此客户服务
pthread_t tid;
pthread_create(&tid,0,service_thread,&fd);
break;
}
if (size == i)
{
//发送给客户端说聊天室满了
char* str = "对不起,聊天室已经满了!";
send(fd,str,strlen(str),0);
close(fd);
}
}
//提示用户上线
i = 0;
char buf2[20] = {0};
recv(fd,buf2,20,0);
sprintf(buf3,"%s进入了聊天室",buf2);
for (i = 0;i < size;i++)
{
if (fds[i] != 0)
{
if(fds[i] == fd)
{
continue;
}
printf("sendto%d\n",fds[i]);
send(fds[i],buf3,strlen(buf3),0);
}
}
memset(buf3,0,100);
}
}
int main(int argc,char * argv[])
{
if(argc != 2)
{
printf(“命令格式错误\n”);
return -1;
}
//设置套接字
sockfd = socket(PF_INET,SOCK_STREAM,0);
if (sockfd == -1){
perror(“创建socket失败”);
exit(-1);
}
struct sockaddr_in addr;
addr.sin_family = PF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(argv[1]);
if (bind(sockfd,(SA*)&addr,sizeof(addr)) == -1){
perror(“绑定失败”);
exit(-1);
}
if (listen(sockfd,10) == -1){
perror(“设置监听失败”);
exit(-1);
}
service();
}
TCP.clint.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 4567
int sockfd;//客户端socket
char name[20] = {0};//客户端姓名
typedef struct sockaddr SA;
//接收服务器转发的信息
void* recv_thread(void* p){
while(1){
char buf[100] = {};
if (recv(sockfd,buf,sizeof(buf),0) <= 0)
{
break;
}
printf("%s\n",buf);
}
}
void start()
{
//给服务器发送信息
pthread_t id;
pthread_create(&id,0,recv_thread,0);
char buf2[100] = {0};
send(sockfd,name,strlen(name),0);
while(1)
{
char buf[100] = {0};
scanf("%s",buf);
if (strcmp(buf,“bye”) == 0)
{
send(sockfd,name,(strlen(name)),0);
break;
}
else if(strcmp(buf,“chat_history”) == 0)
{
send(sockfd,buf,strlen(buf),0);
continue;
}
char msg[131] = {0};
sprintf(msg,"%s:%s",name,buf);
send(sockfd,msg,strlen(msg),0);
}
close(sockfd);
}
//主函数
int main(int argc,char * argv[])
{
//设置套接字
sockfd = socket(PF_INET,SOCK_STREAM,0);
struct sockaddr_in addr;
addr.sin_family = PF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr(argv[1]);
//连接服务器
if (connect(sockfd,(SA*)&addr,sizeof(addr)) == -1)
{
perror(“无法连接到服务器”);
exit(-1);
}
printf(“客户端启动成功\n”);
printf(“请输入您的名字:”);
scanf("%s",name);
start();
return 0;
}
这个聊天室程序还有很多不足,1.发送数据不能识别空格;2.查找聊天室记录功能还需完善,现在只能显示前100个字符。如果还有其他不足的地方希望各位能提出来,以供其他阅读的人参考
希望能对各位有所帮助,同时希望各位大佬们能够完善。