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 > ...
随机推荐
- 【前端】String.prototype.match() 用法详解
var str="1 plus 2 equal 3" // 正则表达式 console.log(str.match(/\d+/g)); // ["1", &qu ...
- MacBook 显示隐藏文件夹命令
1. 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 第一步:命令行执行上述命令 第二步:将Finder重新打开 第三步 ...
- [多校联考2 T3] 排列 (DP)
DP Description 对于一个排列,考虑相邻的两个元素,如果后面一个比前面一个大,表示这个位置是上升的,用 I 表示,反之这个位置是下降的,用 D表示.如排列 3,1,2,7,4,6,5 可以 ...
- linux环境下安装mysql数据库遇到的问题
总结一句话: 安装完mysql数据库记得授权远程登录. 坑的现象: ERROR 1045 (28000): Access denied for user 'guoxp'@'localhost' ( ...
- iOS开发数据库篇—SQLite简单介绍
iOS开发数据库篇—SQLite简单介绍 一.离线缓存 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等. 说明:离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式 (1 ...
- linux SVNUP显示无法连接主机
今天开发环境中突然无法连接主机了,我就想到 1.更改svn 链接地址,发现不可取,工程中的每个目录下面都有个.svn文件,修改起来麻烦: 2.建立新文件夹,重新checkout,发现还是无法链接 最后 ...
- tcp/ip详解-ip头部选项字段
IP头部的选项字段 作用:用于网络调试和测试 IP首部的可变部分就是一个可选字段.选项字段用来支持排错.测量以及安全等措施,内容很丰富.此字段的长度可变,从1个字节到40个字节不等,取决于所选择的项目 ...
- python成长之路【第七篇】:面向对象
概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向对象三大特性 面向 ...
- C# 使用HttpWebRequest通过PHP接口 上传文件
1:上传文件实例 public void UploadXMLLog(string xmlpath) { NameValueCollection nvc = ne ...
- 输入一个整型数组,数据元素有正数也有负数,求元素组合成连续子数组之和最大的子数组,要求时间复杂度为O(n)。
如果不考虑时间复杂度,我们可以枚举出所有子数组并求出他们的和.不过非常遗憾的是,由于长度为n的数组有O(n2)个子数组(即:n + n-1 + ... + 1=n(n+1)/2):而且求一个长度为n的 ...