Commit 9a50cba3 authored by 马伊齐's avatar 马伊齐

初版本地消息发送接收Demo

parent d9505246
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "../lib/sock_msg.h"
int main(int argc, char *argv[])
{
char buffer[BUFFER_SIZE];
if (init_msg_queue() == -1)
{
exit(EXIT_FAILURE);
}
while (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
if (msg_local_send(MSG_CAN_HEARTBEAT, 0, (void *)buffer, strlen(buffer)) == -1)
{
destroy_msg_queue();
exit(EXIT_FAILURE);
}
// 清空buffer
memset(buffer, 0, sizeof(buffer));
}
// 清理消息队列
if (destroy_msg_queue() == -1)
{
exit(EXIT_FAILURE);
}
return 0;
}
\ No newline at end of file
#include <sys/ipc.h>
#include <stdint.h>
#include <stdio.h>
#include <error.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <string.h>
#include "../lib/sock_msg.h"
int main(int argc, char *argv[])
{
msg_data_t msg;
// 初始化消息队列
if (init_msg_queue() == -1)
{
exit(EXIT_FAILURE);
}
while (1)
{
if (msg_local_recv(&msg) > 0)
{
printf("Received code: %d\n", msg.code);
printf("Received type: %d\n", msg.type);
printf("Received len: %d\n", msg.len);
printf("Received pad: %s\n", (char *)(msg.pad));
// 清空pad
memset(msg.pad, 0, msg.len);
}
else
{
destroy_msg_queue();
exit(EXIT_FAILURE);
}
}
// 清理消息队列
if (destroy_msg_queue() == -1)
{
exit(EXIT_FAILURE);
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment