用socket实现一个多人聊天室的思路很简单,即在服务器端定义一个fd的int型数组,用来存储已经连接的客户端的socket连接套接字fd(因为发送和接收数据都只需要借助连接套接字fd),当服务器接收到来自某一客户端的数据时,直接转发给其他所有连接着的客户端,即完成了多人聊天室。
服务器端:多线程进行数据的转发
客户端:创建一个线程专门用来接收数据,主线程进行输入数据等操作
下面直接贴代码:
server.c
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<pthread.h>
#include<stdlib.h>
#define PORT 1234
#define BACKLOG 1
#define Max 5
#define MAXSIZE 1024
/*定义全局变量*/
int fdt[Max]={0};
char mes[1024];
/**/
void *pthread_service(void* sfd)
{
int fd=*(int *)sfd;
while(1)
{
int numbytes;
int i;
numbytes=recv(fd,mes,MAXSIZE,0);
if(numbytes<=0){
for(i=0;i<Max;i++){
if(fd==fdt[i]){
fdt[i]=0;
}
}
printf("numbytes=%d\n",numbytes);
printf("exit! fd=%d\n",fd);
break;
}
printf("receive message from %d,size=%d\n",fd,numbytes);
SendToClient(fd,mes,numbytes);
bzero(mes,MAXSIZE);
}
close(fd);
}
/**/
int SendToClient(int fd,char* buf,int Size)
{
int i;
for(i=0;i<Max;i++){
printf("fdt[%d]=%d\n",i,fdt[i]);
if((fdt[i]!=0)&&(fdt[i]!=fd)){
send(fdt[i],buf,Size,0);
printf("send message to %d\n",fdt[i]);
}
}
return 0;
}
int main()
{
int listenfd, connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
sin_size=sizeof(struct sockaddr_in);
int number=0;
int fd;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Creating socket failed.");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
perror("Bind error.");
exit(1);
}
if(listen(listenfd,BACKLOG) == -1){
perror("listen() error\n");
exit(1);
}
printf("Waiting for client....\n");
while(1)
{
if ((fd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
perror("accept() error\n");
exit(1);
}
if(number>=Max){
printf("no more client is allowed\n");
close(fd);
}
int i;
for(i=0;i<Max;i++){
if(fdt[i]==0){
fdt[i]=fd;
break;
}
}
pthread_t tid;
pthread_create(&tid,NULL,(void*)pthread_service,&fd);
number=number+1;
}
close(listenfd);
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<string.h>
#define PORT 1234
#define MAXDATASIZE 100
char sendbuf[1024];
char recvbuf[1024];
char name[100];
int fd;
void pthread_recv(void* ptr)
{
while(1)
{
if ((recv(fd,recvbuf,MAXDATASIZE,0)) == -1){
printf("recv() error\n");
exit(1);
}
printf("%s",recvbuf);
memset(recvbuf,0,sizeof(recvbuf));
}
}
int main(int argc, char *argv[])
{
int numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in server;
if (argc !=2) { printf("Usage: %s <IP Address>\n",argv[0]);
exit(1);
}
if ((he=gethostbyname(argv[1]))==NULL){
printf("gethostbyname() error\n");
exit(1);
}
if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){
printf("socket() error\n");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr = *((struct in_addr *)he->h_addr);
if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){
printf("connect() error\n");
exit(1);
}
printf("connect success\n");
char str[]="已进入聊天室\n";
printf("请输入用户名:");
fgets(name,sizeof(name),stdin);
send(fd,name,(strlen(name)-1),0);
send(fd,str,(strlen(str)),0);
pthread_t tid;
pthread_create(&tid,NULL,pthread_recv,NULL);
while(1)
{
memset(sendbuf,0,sizeof(sendbuf));
fgets(sendbuf,sizeof(sendbuf),stdin);
if(strcmp(sendbuf,"exit")==0){
memset(sendbuf,0,sizeof(sendbuf));
printf("您已退出群聊\n");
send(fd,sendbuf,(strlen(sendbuf)),0);
break;
}
send(fd,name,(strlen(name)-1),0);
send(fd,":",1,0);
send(fd,sendbuf,(strlen(sendbuf)),0);
}
// sprintf("")
// if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){
// printf("recv() error\n");
// exit(1);
// }
close(fd);
}
下面是程序运行截图: