设为首页 加入收藏

TOP

Linux下GNU C实现的简单聊天室程序
2014-11-23 22:13:01 来源: 作者: 【 】 浏览:0
Tags:Linux GNU 实现 简单 聊天室 程序

闲暇之余,利用linux下的多线程和网络知识,写了个简单多人聊天程序,意在说明用GNU C写网络类程序的步骤和需要注意的问题以及linux多线程的使用方法。

编译:
gcc -Wall -o server -lpthread server.c
gcc -Wall -o client -lpthread client.c

服务器端:C代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

//定义同时聊天的人数
#define COUNT 5

//保存socket
int socket_fd[COUNT];

//线程的入口函数
void pthread_function(int client_fd){
char message[1500];
char buf[1024];
int i,recvbytes;
char name[20];
//首次连接时,接受并保存客户端名字
recvbytes = recv(client_fd, name, 20, 0);
name[recvbytes]=:;
name[recvbytes+1]=;
while(1){
if((recvbytes = recv(client_fd, buf, 1024, 0))==-1){
perror("recv error");
exit(1);
}
if(recvbytes==0){
printf("%sbye! ",name);
break;
}
buf[recvbytes]=;
for(i=0;i if(socket_fd[i]==-1){
continue;
}else{
message[0]=;
strcat(message,name);
strcat(message,buf);
if(send(socket_fd[i],message,strlen(message),0)==-1){
perror("send error");
exit(1);
}
}
}
}
//断开时关闭socket,并将描述符值置为-1
close(client_fd);
for(i=0;i if(socket_fd[i]==client_fd){
socket_fd[i]=-1;
}
}
//退出线程
pthread_exit(NULL);
}

int main(){

//初始化socket数组
int i;
for(i=0;i socket_fd[i]=-1;
}
pthread_t id;
int sockfd,client_fd;
socklen_t sin_size;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
//配置信息
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(12345);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
//绑定
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
//监听
if(listen(sockfd,10)==-1){
perror("listen");
exit(1);
}
i=0;
while(1){
sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1){
perror("accept");
exit(1);
}
//找到一个可用的socket位置
while(socket_fd[i]!=-1)
i=(i+1)%COUNT;
//保存socket并启动线程处理
socket_fd[i]=client_fd;
pthread_create(&id,NULL,(void *)pthread_function,(int *)client_fd);
}
}

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Static变量和实例变量的初始化顺.. 下一篇const的几个用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: