/*
TCP MultiThread Chat Server
Author :Andrew Huang
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#define MAX_USER 60
static pthread_mutex_t chat_lock;
static int user_list[MAX_USER]; //以client_fd 为下标,如果对应socket在线,值为1,下线为0
static void * thread_fn(void *arg)
{
int client_fd = (int)arg;
int len,fd;
char buf[1024];
fprintf(stdout,"thread %d begin\n",client_fd);
while(1)
{
len = recv(client_fd,buf,sizeof(buf),0);
if(len <= 0)
break;
fprintf(stdout,"recv client:%s\n",buf);
//对所有在线的socket进行轮播
pthread_mutex_lock(&chat_lock);
for(fd=3; fd< MAX_USER;fd++)
{
if((user_list[fd] == 0) || (fd == client_fd))
continue;
if(send(fd,buf,len,0)<0)
continue;
}
pthread_mutex_unlock(&chat_lock);
}
pthread_mutex_lock(&chat_lock);
user_list[client_fd] = 0;
pthread_mutex_unlock(&chat_lock);
close(client_fd);
fprintf(stdout,"CLOSE socket %d\n",client_fd);
}
int main(int argc ,char * argv[])
{
unsigned short port = 9000;
int listen_fd ;
int client_fd;
pthread_t pth;
char * welcome_msg = "Welcome to Chat Server ,ver 0.1";
char * exit_msg = "user to max!";
struct sockaddr_in addr; //ipv4的地址结构
struct sockaddr_in client_addr ;
socklen_t addr_len;
int len ;
if(argc >1)
{
port = (unsigned short)atoi(argv[1]);
}
//第一步:创建一个IPV4,TCP,可以运行所有协议的SOCKET
listen_fd = socket(PF_INET,SOCK_STREAM,0);
if(listen_fd == -1)
{
perror("socket");
return -1;
}
{
int val = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof (val));
}
//第二步:用bind来指定端口
memset(&addr,0,sizeof(addr));
addr.sin_port = htons(port); //转成网络序的端口
addr.sin_family = AF_INET; //ipv4的地址类型
//addr.sin_addr.s_addr = inet_addr("0.0.0.0"); //bind 的sin_addr表示在哪一个网卡上有效,
//绑定在0.0.0.0上表示这个SOCKET在所有网卡侦听
addr.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY = 0 = inet_addr("0.0.0.0")
if(bind(listen_fd,(const struct sockaddr *)&addr,sizeof(addr)) == -1)
{
perror("bind");
close(listen_fd);
return -2;
}
//第三步: 通知内核里的TCP/IP协议栈,让这个SOCKET开始侦听网络上的请求
// listen只是通知一下,通知成功立即执行后面代码
if(listen(listen_fd,20) == -1)
{
perror("listen");
close(listen_fd);
return -3;
}
fprintf(stdout,"TCP Chat Server listen on %d\n",port);
//初始化锁
pthread_mutex_init(&chat_lock,NULL);
memset(user_list,0,sizeof(user_list));
while(1)
{
memset(&client_addr,0,sizeof(client_addr));
client_addr.sin_family = AF_INET;
addr_len = sizeof(client_addr); //必须赋值!
//第四步:服务器在侦听客户端请求
printf("accept client ...\n");
client_fd = accept(listen_fd,(struct sockaddr *)&client_addr,&addr_len);
if(client_fd == -1)
{
perror("accept");
continue;
}
fprintf(stdout,"client socket %d,addr %s,port %d\n",client_fd,
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
if(client_fd > MAX_USER)
{
send(client_fd,exit_msg,strlen(exit_msg),0);
close(client_fd);
continue;
}
if(send(client_fd,welcome_msg,strlen(welcome_msg),0)<=0)
{
perror("send");
}
//把上线用户加入在线用户表
pthread_mutex_lock(&chat_lock);
printf("user %d online\n",client_fd);
user_list[client_fd] = 1;
pthread_mutex_unlock(&chat_lock);
pthread_create(&pth,NULL,thread_fn,(void*)client_fd);
}
printf("exit");
close(listen_fd);
}
|