资源简介
在linux系统用C语言编写一个多用户的聊天室管理系统。主要功能:
1.能做到3个以上用户之间的聊天;
2.系统要有用户管理功能;
3.每个用户能管理自己的权限,比如不接受信息,撤销已发的信息等;
4.聊天信息的保存,比如保存三天内的信息,或其他规定;

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define KEYPATH “/doc/process/ipc/message_queue/chat“
#define NAMESIZE 16
#define MSGSIZE 128
#define LEN sizeof(struct msgbuf) - 4
#define SERTYPE 1
#define NORMAL 0
#define REGISTER 1
#define UNREGISTER -1
struct msgbuf
{
long mtype;
int subtype;
int id;
int status;
char nickname[NAMESIZE];
char mtext[MSGSIZE];
}buf;
int main(int argc char **argv)
{
int msgidpidid;
key_t key;
int i;
char ch;
opterr = 0;
buf.status = 1;
while ( (ch = getopt(argc argv “h“)) != -1 )
{
switch (ch)
{
case ‘h‘:
buf.status = 0;
break;
}
}
if ( optind >= argc )
{
fprintf(stdout “Usage: %s [-h]\n“ argv[0]);
exit(1);
}
key = ftok(KEYPATH ‘c‘);
msgid = msgget(key 0666);
if ( msgid == -1 )
{
printf(“Can‘t connect to the server maybe it is down!\n“);
//perror(“msgget“);
exit(-1);
}
// register to the server
id = getpid();
buf.mtype = SERTYPE;
buf.subtype = REGISTER;
buf.id = id;
strcpy(buf.nickname argv[optind]);
memset(buf.mtext 0 MSGSIZE);
if ( msgsnd(msgid &buf LEN 0) == -1 )
{
perror(“Failed to register“);
exit(-1);
}
if ( (pid = fork()) == -1 )
{
perror(“fork“);
exit(-1);
}
if ( pid > 0 ) // parent process
{
while ( 1 )
{
usleep(100000);
printf(“Please input a string : “);
fgets(buf.mtext MSGSIZE stdin);
buf.mtype = SERTYPE;
if ( strcmp(buf.mtext “quit\n“) == 0 )
{
kill(pid SIGQUIT);
buf.subtype = UNREGISTER;
if ( msgsnd(msgid &buf LEN 0) == -1 )
{
perror(“can‘t send message...“);
}
exit(0);
}
if ( buf.status == 0 )
{
printf(“You can‘t send message in hidden mode\n“);
continue;
}
buf.subtype = NORMAL;
if ( msgsnd(msgid &buf LEN 0) == -1 )
{
perror(“can‘t send message...“);
}
}
}
else // child process
{
while ( 1 )
{
if ( msgrcv(msgid &buf LEN id 0) == -1 )
{
//perror(“can‘t receive message exit client“);
kill(getppid() SIGINT);
sleep(1);
exit(-1);
}
if ( buf.subtype == NORMAL )
{
printf(“\n[%s] %s\n“ buf.nickname buf.mtext);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-01-07 09:18 C语言编写简单聊天室\
文件 3167 2016-12-27 13:14 C语言编写简单聊天室\client .txt
文件 3167 2016-12-27 13:14 C语言编写简单聊天室\client.c
文件 4727 2016-12-27 13:15 C语言编写简单聊天室\server .txt
文件 4727 2016-12-27 13:15 C语言编写简单聊天室\server.c
文件 8486 2017-01-07 09:15 C语言编写简单聊天室\实验结果.png
- 上一篇:activemq-cpp开发手册.pdf
- 下一篇:astrology32占星源码
相关资源
- C语言开发实战宝典
- C++中头文件与源文件的作用详解
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
- 个人日常财务管理系统(C语言)
- c语言电子商务系统
- 小甲鱼C语言课件 源代码
- 将图片转换为C语言数组的程序
- C语言实现的一个内存泄漏检测程序
- DES加密算法C语言实现
- LINUX下命令行界面的C语言细胞游戏
- 用单片机控制蜂鸣器播放旋律程序(
- 学校超市选址问题(数据结构C语言版
- 电子时钟 有C语言程序,PROTEUS仿真图
- 尚观培训linux许巍老师关于c语言的课
- 算符优先语法分析器(C语言编写)
评论
共有 条评论