zoukankan      html  css  js  c++  java
  • Linux 消息队列 实现的简单聊天室

    http://wenku.baidu.com/view/e75f720e52ea551810a68744.html###

     1 //server.cc
     2 
     3 #include <sys/ipc.h>
     4 #include <string.h>
     5 #include <unistd.h>
     6 #include <sys/types.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include <sys/msg.h>
    10 
    11 struct msgbuf1
    12 {
    13     long mtype;//消息类型
    14     char mtext[100]; //消息正文
    15 };
    16 
    17 int main()
    18 {
    19     FILE *fp; 
    20     key_t key;
    21     pid_t pid;
    22     int msgid;
    23     struct msgbuf1 msg1, msg2;
    24     //消息内容,本地名字, 对方名字
    25     char wbuf[800] = "", my_name[20] = "", others_name[20] = "";
    26     key = ftok(".", 0xFF);
    27     if ((msgid = msgget(key, IPC_CREAT | 0666)) < 0) {
    28         perror("msgget error\n");
    29         exit(0);
    30     }
    31     printf("please input your name: ");
    32     msg1.mtype = 3; //消息类型为3
    33     memset(msg1.mtext, 0, 100);
    34     fgets(wbuf, 100, stdin); //将标准输入到wbuf缓冲区
    35     wbuf[strlen(wbuf) - 1] = 0;
    36     strcpy(my_name, wbuf); //把wbuf中的内容复制到my_name中
    37     strcpy(msg1.mtext, wbuf);
    38     msgsnd(msgid, &msg1, sizeof(msg1.mtext), 0); //把消息添加到消息队列中
    39     msgrcv(msgid, &msg2, 100, 4, 0); //根据消息队列的消息类型接受对应的消息
    40     //即对方姓名
    41     //将收到的消息复制到others_name
    42     strcpy(others_name, msg2.mtext);
    43     fflush(stdout);
    44     //创建一个子进程
    45     if ((pid = fork()) < 0) {
    46         printf("error\n");
    47         exit(0);
    48     }
    49     //子进程
    50     if (pid == 0) {
    51         while (1) {
    52             msg1.mtype = 1;
    53             memset(msg1.mtext, 0, 100); //刷新
    54             printf("%s: ", my_name);
    55             fgets(wbuf, 100, stdin);//输入字符
    56             wbuf[strlen(wbuf) - 1] = 0;
    57             strcpy(msg1.mtext, wbuf);
    58             msgsnd(msgid, &msg1, sizeof(msg1.mtext), 0);
    59         }
    60     }
    61     else {
    62         while (1) {
    63             msgrcv(msgid, &msg2, 100, 2, 0); //接收type为2的消息
    64             if ((fp = fopen("data.txt", "a+")) == NULL) {
    65                 perror("打开失败记录");
    66                 return 0;
    67             }
    68             fprintf(fp, "%s: %s\n", others_name, msg2.mtext);
    69             fclose(fp);
    70             // \r是回到本行的开头
    71             printf("\r");
    72             for (int i = 0; i < strlen(my_name) + 2; i++) printf(" ");
    73             printf("\r%s: %s\n%s: ", others_name, msg2.mtext, my_name);
    74             fflush(stdout);
    75         }
    76     }
    77 }
     1 //client.cc
     2 
     3 #include <sys/ipc.h>
     4 #include <string.h>
     5 #include <unistd.h>
     6 #include <sys/types.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include <sys/msg.h>
    10 
    11 struct msgbuf1
    12 {
    13     long mtype; //消息类型
    14     char mtext[100]; //消息正文
    15 };
    16 
    17 int main()
    18 {
    19     FILE *fp;
    20     key_t key;
    21     pid_t pid;
    22     int msgid;
    23     struct msgbuf1 msg1, msg2;
    24     char wbuf[800] = "", my_name[20] = "", others_name[20] = "";
    25     key = ftok(".", 0xFF);
    26     //打开消息队列
    27     if ((msgid = msgget(key, IPC_CREAT | 0666)) < 0) {
    28         //创建队列
    29         perror("msgget error\n");
    30         exit(0);
    31     }
    32     printf("please input your name: ");
    33     msg1.mtype = 4;
    34     memset(msg1.mtext, 0, 100);
    35     fgets(wbuf, 100, stdin);
    36     wbuf[strlen(wbuf) - 1] = 0;
    37     strcpy(msg1.mtext, wbuf);
    38     strcpy(my_name, wbuf);
    39 
    40     msgsnd(msgid, &msg1, sizeof(msg1.mtext), 0);
    41     msgrcv(msgid, &msg2, 100, 3, 0);
    42 
    43     strcpy(others_name, msg2.mtext);
    44     fflush(stdout);
    45     if ((pid = fork()) < 0) {
    46         printf("error\n");
    47         exit(0);
    48     }
    49     //子进程
    50     if (pid == 0) {
    51         while (1) {
    52             msgrcv(msgid, &msg2, 100, 1, 0);
    53             if ((fp = fopen("data.txt", "a+")) == NULL) {
    54                 perror("打开失败记录");
    55                 return 0;
    56             }
    57             fprintf(fp, "%s: %s\n", others_name, msg2.mtext);
    58             fclose(fp);
    59 
    60             // \r是回到本行的开头
    61             printf("\r%s: %s\n%s: ", others_name, msg2.mtext, my_name);
    62             fflush(stdout);
    63         }
    64     }
    65     else {
    66         while (1) {
    67             msg1.mtype = 2;
    68             memset(msg1.mtext, 0, 100);
    69             printf("%s: ", my_name);
    70             fgets(wbuf, 100, stdin);
    71             wbuf[strlen(wbuf) - 1] = 0;
    72             strcpy(msg1.mtext, wbuf);
    73             msgsnd(msgid, &msg1, sizeof(msg1.mtext), 0);
    74         }
    75     }
    76 }
  • 相关阅读:
    Python 变量回收
    centos服务器内部开启端口
    mysql启动不起来汇总
    torch中DataLoader加载字典
    torch.meshgrid函数
    如何配置好python脚本的命令行参数
    git忽略文件,.gitignore配置
    jupyter如何解析命令行参数argv
    怎么制作虚拟环境的yaml
    如何提高matplotlib在jupyter中作图的分辨率
  • 原文地址:https://www.cnblogs.com/hengli/p/2875830.html
Copyright © 2011-2022 走看看