资源简介
在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占星源码
相关资源
- erf()函数C语言代码
- C语言课程设计报告-长整数四则运算
- 数据结构课程设计 通讯录的制作
- 作业调度算法c语言版
- 数据结构课程设计——族谱管理系统
- C语言中的无穷积分算法,很好用的,
- 原创modbus 协议c实现 C 语言 实现功能
- 数据结构C语言版_伙伴系统.txt
- 最速下降法C语言代码
- 物资管理系统 C语言
- c语言socket实现的http服务器
- 表达式求值C语言实现《数据结构》课
- 五层电梯算法动态演示-C语言开发
- C语言基础练习题
- C8051F320 c语言 超声波测距程序 实
- 四位七脚数码管C语言
- c语言编写的PQ分解法潮流程序有详细
- 销售管理系统-C语言课程设计
- C语言实现FFT(快速傅里叶变换)
- 矩阵QR分解的C语言
- 贪吃蛇 linux开发 C语言 linux系统编程
- 装载问题有两艘船,载重量分别是c
- 《C语言程序设计》课程设计报告
- 基于tcp的聊天程序c语言的
- 湖南大学C语言题库
- 基于QT的网络聊天室和服务器
- 数据挖掘中的Apriori算法(C语言版)源
- C语言小精灵游戏源代码包含画图库
- c语言实现的电子词典
- 宿舍管理系统C语言源代码与可执行文
评论
共有 条评论