zoukankan      html  css  js  c++  java
  • Linux发送与接收信息

    receive:

    View Code
    #include <sys/types.h>
    #include 
    <sys/ipc.h>
    #include 
    <sys/msg.h>
    #include 
    <stdio.h>
    #include 
    <stdlib.h>
    #include 
    <unistd.h>
    #include 
    <string.h>

    #define BUFFER_SIZE  512

    struct message
    {
        
    long msg_type;
        
    char msg_text[BUFFER_SIZE];
    };

    int main()
    {
        
    int qid;
        key_t key;
        
    struct message msg;
        
        
    if((key=ftok(".",'a'))==-1)
        {
            perror(
    "ftok");
            exit(
    1);
        }

        
    if((qid=msgget(key,IPC_CREAT|0666))==-1)
        {
            perror(
    "msgget");
            exit(
    1);
        }

        printf(
    "Open queue %d",qid);

        
    do
        {
            memset(msg.msg_text,
    0,BUFFER_SIZE);
            
    if(msgrcv(qid,(void*)&msg,BUFFER_SIZE,0,0)<0)
            {
                perror(
    "msgrcv");
                exit(
    1);
            }

            printf(
    "The message from process %d : %s",(int)msg.msg_type,msg.msg_text);
            
        }
    while(strncmp(msg.msg_text,"quit",4));

        
    if((msgctl(qid,IPC_RMID,NULL))<0)
        {
            perror(
    "msgctl");
            exit(
    1);
        }
        
        exit(
    0);
        
    }

    send:

     

    View Code
    #include <sys/types.h>
    #include 
    <sys/ipc.h>
    #include 
    <sys/msg.h>
    #include 
    <stdio.h>
    #include 
    <stdlib.h>
    #include 
    <unistd.h>
    #include 
    <string.h>

    #define BUFFER_SIZE 512


    struct message{
        
    long msg_type;
        
    char msg_text[BUFFER_SIZE];
    };

    int main()
    {
        
    int qid;
        key_t key;
        
    struct message msg;
        
        
    if((key=ftok(".",'a'))==-1)
        {
            perror(
    "ftok");
            exit(
    1);
        }

        
    if((qid=msgget(key,IPC_CREAT|0666))==-1)
        {
            perror(
    "msgget");
            exit(
    1);
        }

        printf(
    "Open queue %d\n",qid);
        
        
    while(1)
        {
            printf(
    "Enter some message to the queue:");
            
    if((fgets(msg.msg_text,BUFFER_SIZE,stdin))==NULL)
            {
                puts(
    "No message");
                exit(
    1);
            }
            msg.msg_type
    =getpid();
            
            
    if((msgsnd(qid,&msg,strlen(msg.msg_text),0))<0)
            {
                perror(
    "message posted");
                exit(
    1);
            }

            
    if(strncmp(msg.msg_text,"quit",4)==0)
            {
                
    break;
            }

        }
        exit(
    0);
    }


    gcc 两者

     #运行接收

    ./msgcrv 

    #运行发送 

    ./msgsnd

    #open two console to see the effect

  • 相关阅读:
    [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7)
    [冲昏头脑]IDEA中的maven项目中学习log4j的日志操作
    [烧脑时刻]EL表达式1分钟完事
    Sublime2 破解教程
    全脑瘫IT时代(八)
    全脑瘫IT时代(九)
    迁移完成
    USB Debug Cable (一)
    一个不是很常见的.Net Interop问题
    全脑瘫IT时代(十二)
  • 原文地址:https://www.cnblogs.com/no7dw/p/2049495.html
Copyright © 2011-2022 走看看