美文网首页
c语言实现dbus客户端和服务端通信

c语言实现dbus客户端和服务端通信

作者: 一路向后 | 来源:发表于2024-04-04 15:06 被阅读0次

1.server.c

#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>

int main()
{
    DBusConnection *connection = NULL;
    DBusError error;

    dbus_error_init(&error);

    connection = dbus_bus_get(DBUS_BUS_SESSION, &error);

    if(dbus_error_is_set(&error))
    {
        printf("连接DBUS失败: %s\n", error.message);

        dbus_error_free(&error);

        return 1;
    }

    printf("等待客户端请求...\n");

    int ret = dbus_bus_request_name(connection, "com.example.TestServer", DBUS_NAME_FLAG_REPLACE_EXISTING, &error);

    if(dbus_error_is_set(&error))
    {
        printf("注册DBUS服务失败: %s\n", error.message);

        dbus_error_free(&error);

        return 1;
    }

    while(1)
    {
        dbus_connection_read_write_dispatch(connection, -1);

        DBusMessage *message = NULL;

        message = dbus_connection_pop_message(connection);

        if(message == NULL)
        {
            sleep(1);

            continue;
        }

        if(dbus_message_is_method_call(message, "com.example.TestInterface", "Hello"))
        {
            DBusMessage *reply;
            const char *reply_str = "Hello, Client!";

            reply = dbus_message_new_method_return(message);

            dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_str, DBUS_TYPE_INVALID);

            dbus_connection_send(connection, reply, NULL);

            dbus_message_unref(reply);
        }

        dbus_message_unref(message);
    }

    return 0;
}

2,client.c

#include <stdio.h>
#include <string.h>
#include <dbus/dbus.h>

int main()
{
    DBusConnection *connection = NULL;
    DBusError error;

    dbus_error_init(&error);

    connection = dbus_bus_get(DBUS_BUS_SESSION, &error);

    if(dbus_error_is_set(&error))
    {
        printf("连接DBUS失败: %s\n", error.message);

        dbus_error_free(&error);

        return 1;
    }

    const char *server_name = "com.example.TestServer";
    DBusMessage *message = NULL;

    message = dbus_message_new_method_call(server_name, "/com/example/TestObject", "com.example.TestInterface", "Hello");

    DBusPendingCall *pending;

    dbus_connection_send_with_reply(connection, message, &pending, -1);

    //dbus_pending_call_set_timeout(pending, 1000);
    dbus_pending_call_block(pending);

    DBusMessage *reply;

    if(dbus_pending_call_get_completed(pending))
    {
        reply = dbus_pending_call_steal_reply(pending);

        char *str_result;

        dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &str_result, DBUS_TYPE_INVALID);

        printf("接收到来自Server的回复:%s\n", str_result);

        dbus_message_unref(reply);
    }

    dbus_pending_call_unref(pending);
    dbus_message_unref(message);

    return 0;
}

3.编译源码

$ gcc -g -o server server.c -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -ldbus-1
$ gcc -g -o client client.c -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -ldbus-1

4.运行及其结果

$ ./server
$ ./client
接收到来自Server的回复:Hello, Client!

相关文章

  • Socket详解《一》

    本文使用最基础的socket API实现客户端和服务端的通信。 socket 是c语言的API,使用过程中使用到大...

  • Android LocalSocket使用

    一、概述 LocalSocket可以在Android上实现跨进程的通信;区分服务端和客户端,服务端需要监听客户端发...

  • 第4章 客户端

    本章了解Redis服务端和客户端的通信协议,以及主流编程语言的Redis客户端使用方法。 1. 客户端通信协议 基...

  • WebSocket协议

    WebSocket让我们可以在客户端和web服务端之间实现实时通信,不需要客户端发起请求,服务端就可以直接向客户端...

  • C#客户端与Java服务端通过socket通信

    工作环境需求:服务端使用C#编写,客户端使用Java编写。以下是一个C#客户端与Java服务端通过socket通信...

  • 2020-02-07-TCP的连接和释放

    这里使用Java语言简单实现了客户端和服务端通信,使用Wireshark抓包工具抓取了过程中的网络包。 客户端IP...

  • https的一点理解

    https的原理和流程,c(客户端)s(服务端) http是不能确保通信是可信的,ssl可以做到通信可信,ssl其...

  • Java学习5-socket通信(2)

    基于TCP协议的socket通信 目标:了解概念,通信模型,实现socket的步骤,服务端和客户端应该做些什么 什...

  • MySQL逻辑架构

    1.最上层是一些客户端和连接服务,包含本地sock通信和大多数基于客户端/服务端工具实现的类似于tcp/ip的通信...

  • Zabbix 3.4监控linux磁盘使用空间

    环境 服务端:Zabbix 3.4.15客户端:Linux 6.8当前zabbix服务端和客户端可以正常通信,想要...

网友评论

      本文标题:c语言实现dbus客户端和服务端通信

      本文链接:https://www.haomeiwen.com/subject/ptmntjtx.html