I use message queue in C and sending information about all files (in current folder) from the client to server (MSGMAX defined as 4096). Client code:
int mq_id;
struct { long type; char text[MSGMAX]; } mymsg;
mq_id = msgget(12, IPC_CREAT | 0666);
FILE* p;
char str[MSGMAX];
p = popen("ls -l", "r");
while (fgets(str, MSGMAX, p)){ strcpy (mymsg.text, str); }
pclose(p);
mymsg.type = 1;
msgsnd(mq_id, &mymsg, sizeof(mymsg), IPC_NOWAIT);
}
In this part mymsg.text
returns correct multline string (result of ls -l
). But when I tried to print this string in server part, it retured only last line of whole string:
int mq_id;
struct { long type; char text[MSGMAX]; } mymsg;
mq_id = msgget(12, 0);
if (msgrcv(mq_id, &mymsg, sizeof(mymsg), 0, 0) < 0)
perror("\nMsg error!");
else
printf("%s", mymsg.text);
How could I send mymsg.text
completely, with all lines?
With while (fgets(str, MSGMAX, p)){ strcpy (mymsg.text, str); }
while (fgets(str, MSGMAX, p)){ strcpy (mymsg.text, str); }
, you will override the contents of mymsg.txt
with every new line read by fgets
. I'd suggest to write something like...
strcpy(mymsg.text,"");
while (fgets(str, MSGMAX, p)){ strcat (mymsg.text, str); }
or you send the buffer to the server within the loop, ie sending every line read separately.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.