Linux多人群聊系统(简单多线程服务器)
一:要求
1.通过一个服务器实现最多5个客户之间的信息群发。
2.服务器显示客户的登录与退出;
3.客户连接后首先发送客户名称,之后发送群聊信息;
4.客户输入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时终止线程;
三:程序
客户端与服务端的头文件:
ifndef _GCS_H
#define _GCS_H
#include <netinet/in.h> #define MSGLEN 1024 #define OK 1
#define EXIT 2
#define MSG 3
#define USER 4 struct CLIENTMSG {
int op;
char user[];
char buf[MSGLEN];
}; struct CLIENTS {
int sockfd;
int port;
char user[];
struct sockaddr_in client;
int stat;
};
struct CLIENTS ent[]; #endif
服务端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <pthread.h>
#include "group_chat_system.h" extern struct CLIENTS ent[]; void* func(void *arg);
void sever_process(int index); int main(int argc, char **argv)
{
int sever_fd,client_fd;
struct sockaddr_in sever,client;
char ip[];
int port,clientlen;
unsigned char i,index;
struct CLIENTMSG clientMSG;
pthread_t tid;
int *arg; for(i=; i<; i++) {
ent[i].stat = ;
} /***************创建服务器sockfd************/
if((sever_fd = socket(AF_INET, SOCK_STREAM, )) == -) {
perror("socket error");
exit(errno);
} /***************输入ip与端口*****************/
printf("please input ip:");
scanf("%s", ip);
printf("\nplease input port:");
scanf("%d", &port);
printf("\n"); /******************bind***********************/
bzero(&sever, sizeof(sever));
sever.sin_family = AF_INET;
sever.sin_port = htons(port);
sever.sin_addr.s_addr= inet_addr(ip);
if(bind(sever_fd, (struct sockaddr*)&sever, sizeof(struct sockaddr)) == -) {
perror("bind error");
exit(errno);
} /******************listen***********************/
if(listen(sever_fd, ) == -) {
perror("listen error");
exit(errno);
} while() {
clientlen = sizeof(client);
if((client_fd = accept(sever_fd, (struct sockaddr*)&client, &clientlen)) == -) {
perror("accept error");
exit(errno);
}
printf("accept ok\n");
index = ;
for(i=; i<; i++) {
if(ent[i].stat ==){
index = i;
break;
}
} if(index <= ) {
printf("client_fd : %d\n", client_fd);
ent[index].sockfd = client_fd;
ent[index].port = port;
ent[index].client = client;
ent[index].stat = ;
arg = malloc(sizeof(int));
*arg = index;
pthread_create(&tid, NULL, func, (void*)arg);
} else {
printf("the client already has 5\n");
bzero(&clientMSG, sizeof(clientMSG));
clientMSG.op = EXIT;
send(client_fd, &clientMSG, sizeof(clientMSG), );
close(client_fd);
} }
close(sever_fd);
return ;
} void *func(void *arg)
{
int *info;
info = (int *)arg;
sever_process(*info);
pthread_exit(NULL);
} void sever_process(int index)
{
struct CLIENTMSG sendMSG;
struct CLIENTMSG recvMSG;
int len,i; /*首先发送邋OK标志*/
sendMSG.op = OK;
send(ent[index].sockfd, &sendMSG, sizeof(sendMSG), ); while() {
bzero(&sendMSG, sizeof(sendMSG));
bzero(&recvMSG, sizeof(recvMSG)); len = recv(ent[index].sockfd, &recvMSG, sizeof(recvMSG), );
if(len == ) {
sendMSG.op = EXIT;
for(i=; i<; i++) {
if(ent[i].stat == )
send(ent[i].sockfd, &sendMSG, sizeof(sendMSG), );
}
}
if(len > ) {
if(recvMSG.op == USER) {
bcopy(recvMSG.user, ent[index].user, strlen(recvMSG.user));
printf("user %s login form ip: %s port: %d\n", ent[index].user, inet_ntoa(ent[index].client.sin_addr), ntohs(ent[index].client.sin_port));
sendMSG.op = USER;
}
if(recvMSG.op == EXIT) {
//printf("recv exit\n");
sendMSG.op = EXIT;
bcopy(ent[index].user, sendMSG.user, strlen(recvMSG.user));
for(i=; i<; i++) {
if(ent[i].stat == )
send(ent[i].sockfd, &sendMSG, sizeof(sendMSG), );
}
break;
}
if(recvMSG.op == MSG) {
//printf("recv msg\n");
//printf("%s: %s", recvMSG.user,recvMSG.buf);
sendMSG.op= MSG;
}
bcopy(recvMSG.user, sendMSG.user, strlen(recvMSG.user));
bcopy(recvMSG.buf, sendMSG.buf, strlen(recvMSG.buf));
for(i=; i<; i++) {
//printf("\n%d: %s\n", sendMSG.op, sendMSG.user);
if(ent[i].stat == ) {
if(strncmp(ent[i].user, sendMSG.user, strlen(sendMSG.user)))
send(ent[i].sockfd, &sendMSG, sizeof(sendMSG), );
} }
}
} }
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <pthread.h>
#include "group_chat_system.h" struct client_arg {
int sockfd;
struct CLIENTMSG clientMSG;
};
struct client_arg *arg; void *func(void* arg);
void client_process(int sockfd, struct CLIENTMSG clientMSG); int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in sever;
char ip[];
unsigned int port,len;
struct CLIENTMSG sendMSG;
pthread_t tid; if((sockfd = socket(AF_INET, SOCK_STREAM, )) == -) {
perror("socket error");
exit();
} printf("please input ip:");
scanf("%s", ip);
printf("\nplease input port:");
scanf("%d", &port);
printf("\n"); bzero(&sever, sizeof(sever));
sever.sin_family = AF_INET;
sever.sin_port = htons(port);
inet_aton(ip, &sever.sin_addr);
if(connect(sockfd, (struct sockaddr*)&sever, sizeof(struct sockaddr)) == -) {
perror("connect error");
exit(errno);
} len = recv(sockfd, &sendMSG, sizeof(sendMSG), );
if(len > ){
if(sendMSG.op == EXIT) {
printf("exceed the numble of users\n");
}
if(sendMSG.op == OK) {
bzero(&sendMSG, sizeof(sendMSG));
printf("please input client name: ");
fgets(sendMSG.user, MSGLEN, stdin);
//scanf("%s", sendMSG.user);
sendMSG.op = USER;
send(sockfd, &sendMSG, sizeof(sendMSG), );
arg = (struct client_arg*)malloc(sizeof(struct client_arg));
arg->sockfd = sockfd;
pthread_create(&tid, NULL, func, (void*)arg); while() {
// bzero(&sendMSG, sizeof(sendMSG));
sendMSG.op = MSG;
//printf("waiting...");
fgets(sendMSG.buf, MSGLEN, stdin);
//scanf("%s", sendMSG.buf);
//printf("%s: %s %d\n", sendMSG.user,sendMSG.buf, sendMSG.op);
if(!strncasecmp(sendMSG.buf, "bye", )) {
sendMSG.op = EXIT;
send(sockfd, &sendMSG, sizeof(sendMSG), );
break;
}
send(sockfd, &sendMSG, sizeof(sendMSG), );
}
}
pthread_cancel(tid);
}
close(sockfd);
return ;
} void *func(void* arg)
{
struct client_arg *info;
info = (struct client_arg*)arg;
client_process(info->sockfd, info->clientMSG);
free(arg);
pthread_exit(NULL);
} void client_process(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.user);
}else if(clientMSG.op == EXIT){
printf("the user %s is logout.\n",clientMSG.user);
}else if(clientMSG.op == MSG){
printf("%s: %s\n",clientMSG.user,clientMSG.buf);
}
}
}
}
Makefile文件:
main:sever.o client.o
gcc sever.o -o sever -lpthread
gcc client.o -o client -lpthread
sever.o:sever.c
gcc -c sever.c group_chat_system.h
gcc -c client.c group_chat_system.h
clean:
rm *.o sever client
四:现象




Linux多人群聊系统(简单多线程服务器)的更多相关文章
- 【LINUX/UNIX网络编程】之简单多线程服务器(多人群聊系统)
RT,Linux下使用c实现的多线程服务器.这个真是简单的不能再简单的了,有写的不好的地方,还希望大神轻拍.(>﹏<) 本学期Linux.unix网络编程的第四个作业. 先上实验要求: [ ...
- 【LINUX/UNIX网络编程】之使用消息队列,信号量和命名管道实现的多进程服务器(多人群聊系统)
RT,使用消息队列,信号量和命名管道实现的多人群聊系统. 本学期Linux.unix网络编程的第三个作业. 先上实验要求: 实验三 多进程服务器 [实验目的] 1.熟练掌握进程的创建与终止方法: 2 ...
- Linux 用libevent实现的简单http服务器
Linux 用libevent实现的简单http服务器 main.c #include <stdio.h> #include <sys/types.h> #include &l ...
- Linux 用epoll实现的简单http服务器
Linux 用epoll实现的简单http服务器 main.c #include <stdio.h> #include <sys/types.h> #include <s ...
- 转:Linux下使用Nginx搭建简单图片服务器
最近经常有人问图片上传怎么做,有哪些方案做比较好,也看到过有关于上传图片的做法,但是都不是最好的,今天再这里简单讲一下Nginx实现上传图片以及图片服务器的大致理念. 如果是个人项目或者企业小项目,仅 ...
- 39条常见的Linux系统简单面试题
39条常见的Linux系统简单面试题 本文主要分享39条常见的Linux系统简单面试题,其中包括如何看当前Linux系统有几颗物理CPU和每颗CPU的核数.如何实时查看网卡流量为多少等等,希望对你有所 ...
- Linux系统搭建Red5服务器
Linux系统搭建Red5服务器 Red5 是 支持Windows,Linux等多平台的RTMP流媒体服务器,Windows下搭建相对容易,图形界面操作比较简单,Linux服务器的环境下没有图形界面, ...
- Linux系统编程@多线程编程(一)
多线程编程 涉及操作系统原理概念 时间片 进程状态 上下文: 对进程来说,就是进程的执行环境,具体就是各个变量和数据,包括所有的寄存器变量.打开的文件.内存信息等. 进程的写时复制:由于一般 fork ...
- linux系统下svn服务器操作命令
linux系统下svn服务器操作命令 .输出指定文件或URL的内容. svncat 目标[@版本]…如果指定了版本,将从指定的版本开始查找. svncat -r PREV filename > ...
随机推荐
- 第三章 对象(JavaScript:语言精粹)
对象是属性的容器,其中每个属性都有名字和值. 3.0. 概览:对象字面量 | 检索 | 更新 | 引用 | 原型 | 反射 | 枚举 | 删除 | 减少全局变量污染 3.1. 对象字面量 ...
- (转)Could not create the view: An unexpected exception was thrown. 电脑突然断电,myeclipse非正常关闭,出现错误
问题:电脑突然断电,myeclipse非正常关闭,“Package Explorer”非正常显示,出现错误“Could not create the view: An unexpected excep ...
- mysql中查询语句中的一个知识点说明
1, 简单说明. select * from tb_name where 1[不为零即可];则会显示所有记录,select * from tb_name where 0;则不显示任何记录 假设数据库中 ...
- Linux文件管理相关命令
Linux文件管理相关命令 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...
- 02-编写第一个C语言程序
本文目录 1.打开Xcode,新建Xcode项目 2.选择最简单的命令行项目 3.输入项目信息 4.选择一个用来存放C程序代码的文件夹 5.运行项目 说明:这个C语言专题,是学习iOS开发的前奏.也为 ...
- C语言语法之关键字
注:该内容整理自以下链接. http://www.cnblogs.com/yezhenhan/archive/2011/10/16/2214420.html 由ANSI标准定义的C语言关键字共32个: ...
- Android 四大组件之三(广播)
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- c#遍历并判断实体或类的成员属性
c#的Attribute有些类似java中的annotation,可以方便地在类成员中做修饰/限制作用. Demo: class ss { public stat BsonDocument Itera ...
- putty自动登录
如果没有公钥/密钥对,就用 PuTTYgen 创建一个,已经有了就可以忽略这一步.一个公钥/密钥对可以用在不同的服务器上,所以也不需要重复创建,关键要有足够强健的密码和安全的存放. 象先前一样输入帐户 ...
- gz
不准备的话,是真的会滚粗的. leetcode 还是重新做起来叭. 那么就开始咯 8.22 leetcode 144 Binary Tree Preorder Traversal 二叉树的前序遍历 ...