msgrcv()函数在客户端进程中没有返回

时间:2015-04-02 23:34:40

标签: message-queue

我的程序基于进程间通信。客户端发送服务器读取的文件名并将其内容返回给客户端。 msgsnd()函数似乎正在工作,但在调用msgrcv()来读取文件内容时,没有返回客户端函数。

这是我的客户端程序:

#include<sys/msg.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<fcntl.h>

#define MSGSIZE 10000

typedef struct
{
    long mtype;
    char msgtext[MSGSIZE];
} msg_buf;

int main()
{
    int msgflag=IPC_CREAT | 0666;
    key_t k1, k2;
    msg_buf buf;

    k1 = 1234;
    k2 = 5678;

    int msgqid1, msgqid2;

    if(msgqid1=msgget(k1,msgflag)<0)
    {
        printf("\nClient online : the output message queue isn't available. Restart !\n");
        return 0;
    }
    else
    {
        printf("\nClient online : the output message queue is available.\n Enter the file name : ");
        scanf("%s",buf.msgtext);
    }

    buf.mtype = 1;

    if(msgqid2=msgget(k2,msgflag)<0)
        printf("\nClient: Input message queue unavailable. Restart !\n");
    else
        printf("\nClient : Input message queue available \n");

    int buf_len = strlen(buf.msgtext)+1;

    printf("Client: Sending file name : %s \n",buf.msgtext);

    if(msgsnd(msgqid1,&buf,buf_len,IPC_NOWAIT)<0)
    {
        printf("Client : Error sending file name\n");
        return 0;
    }

    printf("hello\n");

    if((msgrcv(msgqid2,&buf,MSGSIZE,2,0))<0)
    {
        printf("Client : Error Recieving file contents from server\n");return 0;
    }
    else
    {
        printf("File Contents from the server as follows :\n ");
        fputs(buf.msgtext,stdout);
        printf("\n\n");
    }
}

这是我的服务器程序:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

#define MSGSIZE 10000

typedef struct msg_buf
{
    long mtype;
    char msgtext[MSGSIZE];
} msg_buf;

int main()
{
    msg_buf buf;
    key_t k1, k2;
    int filesize, fileread, msgqid1, msgqid2;

    k1 = 1234;
    k2 = 5678;

    int msgflag=IPC_CREAT | 0666;

    if((msgqid1=msgget(k1,msgflag))<0)
    {
        printf("Server Online :Input message queue not available. Restart !\n");
        return 0;
    }

    printf("Server Online :Input message queue available.\n");

    if((msgrcv(msgqid1,&buf,MSGSIZE,1,0))<0)
    {
        printf("Server : Error receiving file name");
        return 0;
    }

    printf("Server : File name %s received. \n",buf.msgtext);

    if((msgqid2=msgget(k2,msgflag))<0)
    {
        printf("Server : Output message queue not available. Restart !\n");
        return 0;
    }
    else
        printf("Server Online : Output message queue available.\n");

    fileread = open(buf.msgtext,O_RDONLY);

    if(fileread<0)
    {
        printf("Server : File not found,%d \n",fileread);
        return 0;
    }

    buf.mtype = 2;printf("Server :  file found\n");
    filesize = lseek(fileread,0,2);
    lseek(fileread,0,0);

    int n = read(fileread,buf.msgtext,filesize);
    int buf_len = strlen(buf.msgtext)+1;

    fputs(buf.msgtext,stdout);

    if(msgsnd(msgqid2,&buf,buf_len,IPC_NOWAIT)>=0)
        printf("Server : contents of the file sent to the client\n");
    else
    {
        printf("Server : Error on sending file contents \n");
        return 0;
    }
}

0 个答案:

没有答案