【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

时间:2022-02-20 05:11:50

RT,Linux下使用c实现的多线程服务器。这个真是简单的不能再简单的了,有写的不好的地方,还希望大神轻拍。(>﹏<)

本学期Linux、unix网络编程的第四个作业。

先上实验要求:

【实验目的】

1、熟练掌握线程的创建与终止方法;

2、熟练掌握线程间通信同步方法;

3、应用套接字函数完成多线程服务器,实现服务器与客户端的信息交互。

【实验内容】

通过一个服务器实现最多5个客户之间的信息群发。

服务器显示客户的登录与退出;

客户连接后首先发送客户名称,之后发送群聊信息;

客户输入bye代表退出,在线客户能显示其他客户的登录于退出。

实现提示:

1、服务器端:

主线程:

定义一个全局客户信息表ent,每个元素对应一个客户,存储:socket描述符、客户名、客户IP、客户端口、状态(初值为0)。

主线程循环接收客户连接请求,在ent中查询状态为0的元素,

如果不存在状态为0的元素(即连接数超过最大连接数),向客户发送EXIT标志;

否则,修改客户信息表中该元素的socket描述符、客户IP、客户端口号,状态为1(表示socket可用);

同时创建一个通信线程并将客户索引号index传递给通信线程。

通信线程:

首先向客户端发送OK标志

循环接收客户发来信息,若信息长度为0,表示客户端已关闭,向所有在线客户发送该用户退出;

若信息为用户名,修改全局客户信息表ent中index客户的用户名name,并显示该用户登录;

若信息为退出,修改全局客户信息表ent中index客户状态为0,并显示该用户退出,终止线程;

同时查询全局客户信息表ent,向状态为1的客户发送接收的信息。

2、客户端:

根据用户从终端输入的服务器IP地址及端口号连接到相应的服务器;

连接成功后,接收服务端发来的信息,若为EXIT,则达到最大用户量,退出;

若为OK,可以通讯,首先先发送客户名称;

主进程循环从终端输入信息,并将信息发送给服务器;

当发送给服务器为bye后,程序退出。

同时创建一个线程负责接收服务器发来的信息,并显示,当接收的长度小于等于0时终止线程;

有了上一次多进程服务器的编写经验以后,写起多线程就简单多了。

照例还是绘制一下流程图,以方便我们理清思路。

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

好啦,现在可以开始撸代码了。

先实现一下用于通信的结构体clientmsg.h:(和多进程服务器是一样的)

 //CLIENTMSG between server and client
#ifndef _clientmsg
#define _clientmsg //USER MSG EXIT for OP of CLIENTMSG
#define EXIT -1
#define USER 1
#define MSG 2
#define OK 3 #ifndef CMSGLEN
#define CMSGLEN 100
#endif struct CLIENTMSG{
int OP;
char username[];
char buf[CMSGLEN];
}; #endif

客户端程序看起来比较简单,咱们把它实现了,client.c:

 #include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include "clientmsg.h" struct ARG{
int sockfd;
struct CLIENTMSG clientMsg;
}; void *func(void *arg);
void process_cli(int sockfd,struct CLIENTMSG clientMsg);
int main(){
int sockfd;
char ip[];
int port;
pthread_t tid;
struct sockaddr_in server;
struct CLIENTMSG clientMsgSend;
struct ARG *arg;
/*---------------------socket---------------------*/
if((sockfd = socket(AF_INET,SOCK_STREAM,))== -){
perror("socket error\n");
exit();
} /*---------------------connect--------------------*/
printf("Please input the ip:\n");
scanf("%s",ip);
printf("Please input the port:\n");
scanf("%d",&port);
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
inet_aton(ip,&server.sin_addr);
if(connect(sockfd,(struct sockaddr *)&server,sizeof(server))== -){
perror("connect() error\n");
exit();
}
recv(sockfd,&clientMsgSend,sizeof(clientMsgSend),);
if(clientMsgSend.OP == OK){
//创建一个线程
arg = (struct ARG *)malloc(sizeof(struct ARG));
arg->sockfd = sockfd;
pthread_create(&tid,NULL,func,(void *)arg);
//主线程
printf("Please input the username:\n");
scanf("%s",clientMsgSend.username);
clientMsgSend.OP = USER;
send(sockfd,&clientMsgSend,sizeof(clientMsgSend),);
while(){
clientMsgSend.OP = MSG;
scanf("%s",clientMsgSend.buf);
if(strcmp("bye",clientMsgSend.buf) == ){
clientMsgSend.OP = EXIT;
send(sockfd,&clientMsgSend,sizeof(clientMsgSend),);
break;
}
send(sockfd,&clientMsgSend,sizeof(clientMsgSend),);
}
pthread_cancel(tid);
}
else{
printf("以达到最大连接数!\n");
}
/*------------------------close--------------------------*/
close(sockfd); return ;
} void *func(void *arg){
struct ARG *info;
info = (struct ARG *)arg;
process_cli(info->sockfd,info->clientMsg);
free(arg);
pthread_exit(NULL);
}
void process_cli(int sockfd,struct CLIENTMSG clientMsg){
int len;
while(){
bzero(&clientMsg,sizeof(clientMsg));
len =recv(sockfd,&clientMsg,sizeof(clientMsg),);
if(len > ){
if(clientMsg.OP ==USER){
printf("the user %s is login.\n",clientMsg.username );
}
else if(clientMsg.OP == EXIT){
printf("the user %s is logout.\n",clientMsg.username);
}
else if(clientMsg.OP == MSG){
printf("%s: %s\n",clientMsg.username,clientMsg.buf );
}
}
}
}

然后是服务器端的实现,server.c:

 #include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <pthread.h>
#include "clientmsg.h" struct Entity{
int sockfd;
char username[];
char buf[CMSGLEN];
struct sockaddr_in client;
int stat;
}; void *func(void *arg);
void communicate_process(int index);
struct Entity ent[]; int main(){ struct sockaddr_in server;
struct sockaddr_in client;
int listenfd,connetfd;
char ip[];
int port;
int addrlen;
struct CLIENTMSG clientMsg;
pthread_t tid;
int *arg;
/*---------------------socket-------------------*/
if((listenfd = socket(AF_INET,SOCK_STREAM,))== -){
perror("socket() error\n");
exit();
} /*----------------------IO-----------------------*/
printf("Please input the ip:\n");
scanf("%s",ip);
printf("Please input the port:\n");
scanf("%d",&port); /*---------------------bind----------------------*/
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
if(bind(listenfd,(struct sockaddr *)&server,sizeof(server))== -){
perror("bind() error\n");
exit();
} /*----------------------listen-------------------*/
if (listen(listenfd,)== -){
perror("listen() error\n");
exit();
}
int i;
for(i=;i<;i++){
ent[i].stat = ;
}
while(){
addrlen = sizeof(client);
if((connetfd = accept(listenfd,(struct sockaddr *)&client,&addrlen))== -){
perror("accept() error\n");
exit();
}
int index = ;
for(i=;i<;i++){
if(ent[i].stat == ){
index = i;
break;
}
}
if(index <= ){
printf("connetfd:%d\n",connetfd );
ent[index].client = client;
ent[index].sockfd = connetfd;
ent[index].stat = ;
arg = malloc(sizeof(int));
*arg = index;
pthread_create(&tid,NULL,func,(void *)arg); }
else{
bzero(&clientMsg,sizeof(clientMsg));
clientMsg.OP = EXIT;
send(connetfd,&clientMsg,sizeof(clientMsg),);
close(connetfd);
} } /*----------------------close-------------------*/ close(listenfd); return ;
} /*----------------------------函数实现区----------------------------*/
void *func(void *arg){
int *info ;
info = (int *)arg;
communicate_process( *info);
pthread_exit(NULL);
}
void communicate_process(int index){
struct CLIENTMSG sendMsg;
struct CLIENTMSG recvMsg;
printf("sockfd:%d\n",ent[index].sockfd );
sendMsg.OP = OK;
send(ent[index].sockfd,&sendMsg,sizeof(sendMsg),); while(){
bzero(&sendMsg,sizeof(sendMsg));
bzero(&recvMsg,sizeof(recvMsg));
int len =recv(ent[index].sockfd,&recvMsg,sizeof(recvMsg),);
if(len > ){
if(recvMsg.OP == USER){
printf("user %s login from ip:%s,port:%d\n",recvMsg.username,inet_ntoa(ent[index].client.sin_addr),ntohs(ent[index].client.sin_port) );
bcopy(recvMsg.username,ent[index].username,strlen(recvMsg.username));
sendMsg.OP = USER;
}
else if(recvMsg.OP == EXIT){
printf("user %s is logout\n",recvMsg.username );
sendMsg.OP = EXIT;
ent[index].stat = ;
int i;
for(i=;i<;i++){
if(ent[i].stat == ){ send(ent[i].sockfd,&sendMsg,sizeof(sendMsg),);
}
}
break;
}
else if(recvMsg.OP == MSG){
sendMsg.OP = MSG;
}
bcopy(recvMsg.username,sendMsg.username,strlen(recvMsg.username));
bcopy(recvMsg.buf,sendMsg.buf,strlen(recvMsg.buf));
int i;
for(i=;i<;i++){
if(ent[i].stat == ){
printf("stat 1...\n");
send(ent[i].sockfd,&sendMsg,sizeof(sendMsg),);
}
}
}
else{
continue;
}
}
}

最后是makefile文件:

main:server.o client.o
gcc server.o -oserver -lpthread
gcc client.o -oclient -lpthread
server.o:server.c clientmsg.h
gcc -c server.c
client.o:client.c clientmsg.h
gcc -c client.c

如果程序中引入了#include <pthread.h>,要记得在编译的时候 加上 -lpthread。

下面上一下演示过程:(测试环境,Red Hat Enterprise Linux 6 + centos系Linux,ubuntu下可能会有些问题。)

首先先把服务端启动开来,为了方便测试,这里直接使用的是127.0.0.1的localhost。

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

然后启动两个客户端用来测试,在用户登录的时候客户端会有消息提醒。服务端会有日志打印输出客户端的名字和登录ip、端口。

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

客户可以发送消息了,如图发送与接收均正常。这里为了简单演示只是启动了2个。

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

输入bye以后即可退出聊天并下线。当有客户下线的时候,在线的客户端会收到下线提醒,客户端会有日志打印输出。

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)

【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)