C++ 使用MS RPC实现客户端服务器双向消息收发及1780报错排查
RPC双向消息收发实现调试
开发背景
正在编写RPC服务器应用与RPC客户端应用,实现双向消息收发能力。已参考基础RPC开发教程完成客户端向服务器发送消息的基础流程,现修改代码以支持服务器向客户端回传响应。
IDL接口定义
import "oaidl.idl"; import "ocidl.idl"; [ // A unique identifier that distinguishes this // interface from other interfaces. uuid(00000001-EAF3-4A7A-A0F2-BCE4C30DA77E), // This is version 1.0 of this interface. version(1.0), // This interface will use an implicit binding // handle named hExample1Binding. implicit_handle(handle_t hExample1Binding) ] interface Example1 // The interface is named Example1 { // A function that takes a zero-terminated string. void Output( [in, string] const char* szInput, [out, string, size_is(500)] const char* szOutput); }
服务端实现代码
// PlainRPCServer.cpp : Defines the entry point for the console application. // #include "stdafx.h" // File ContextExampleServer.cpp #include <string> #include <iostream> #include "Example1_h.h" using namespace std; // Server function. void Output( /* [string][in] */ const unsigned char *szInput, /* [size_is][string][out] */ const unsigned char *szOutput) { std::cout << "from client " << szInput << std::endl; szOutput = "Reply from the server"; return; } // Naive security callback. RPC_STATUS CALLBACK SecurityCallback(RPC_IF_HANDLE /*hInterface*/, void* /*pBindingHandle*/) { return RPC_S_OK; // Always allow anyone. } int main() { RPC_STATUS status; // Uses the protocol combined with the endpoint for receiving // remote procedure calls. status = RpcServerUseProtseqEpA( (RPC_CSTR)("ncacn_ip_tcp"), // Use TCP/IP protocol. RPC_C_PROTSEQ_MAX_REQS_DEFAULT, // Backlog queue length for TCP/IP. (RPC_CSTR)("4747"), // TCP/IP port to use. NULL); // No security. if (status) exit(status); // Registers the Example1 interface. status = RpcServerRegisterIf2( Example1_v1_0_s_ifspec, // Interface to register. NULL, // Use the MIDL generated entry-point vector. NULL, // Use the MIDL generated entry-point vector. RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH, // Forces use of security callback. RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Use default number of concurrent calls. (unsigned)-1, // Infinite max size of incoming data blocks. SecurityCallback); // Naive security callback. if (status) exit(status); // Start to listen for remote procedure // calls for all registered interfaces. // This call will not return until // RpcMgmtStopServerListening is called. status = RpcServerListen( 1, // Recommended minimum number of threads. RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Recommended maximum number of threads. FALSE); // Start listening now. if (status) exit(status); } // Memory allocation function for RPC. // The runtime uses these two functions for allocating/deallocating // enough memory to pass the string to the server. void* __RPC_USER midl_user_allocate(size_t size) { return malloc(size); } // Memory deallocation function for RPC. void __RPC_USER midl_user_free(void* p) { free(p); }
当前服务端可正常运行,但客户端无法工作,无法确认服务端逻辑是否正确。
客户端实现代码
// PlainRPCClient.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include "Example1_h.h" using namespace std; int main() { RPC_STATUS status; RPC_CSTR szStringBinding = NULL; // Creates a string binding handle. // This function is nothing more than a printf. // Connection is not done here. status = RpcStringBindingComposeA( NULL, // UUID to bind to. (RPC_CSTR)("ncacn_ip_tcp"),// Use TCP/IP protocol. (RPC_CSTR)("localhost"), // TCP/IP network address to use. (RPC_CSTR)("4747"), // TCP/IP port to use. NULL, // Protocol dependent network options to use. &szStringBinding); // String binding output. if (status) exit(status); // Validates the format of the string binding handle and converts // it to a binding handle. // Connection is not done here either. status = RpcBindingFromStringBindingA( szStringBinding, // The string binding to validate. &hExample1Binding); // Put the result in the implicit binding // handle defined in the IDL file. if (status) exit(status); RpcTryExcept { // Calls the RPC function. The hExample1Binding binding handle // is used implicitly. // Connection is done here. const unsigned char* serverMsg = NULL; Output((const unsigned char *)"msg from the client", serverMsg); } RpcExcept(1) { std::cerr << "Runtime reported exception " << RpcExceptionCode() << std::endl; } RpcEndExcept // Free the memory allocated by a string. status = RpcStringFreeA( &szStringBinding); // String to be freed. if (status) exit(status); // Releases binding handle resources and disconnects from the server. status = RpcBindingFree( &hExample1Binding); // Frees the implicit binding handle defined in the IDL file. if (status) exit(status); } // Memory allocation function for RPC. // The runtime uses these two functions for allocating/deallocating // enough memory to pass the string to the client. void* __RPC_USER midl_user_allocate(size_t size) { return malloc(size); } // Memory deallocation function for RPC. void __RPC_USER midl_user_free(void* p) { free(p); }
故障现象
客户端运行后进程终止,控制台输出 Runtime reported exception 1780,开发环境为VS2015。
待解决问题
- 客户端调用远程
Output方法时的故障定位:按照微软官方文档说明,调用RPC方法时,客户端应用需将输出参数(代码中为serverMsg)设置为NULL,但当前触发1780错误,无法定位问题根因。 - 服务端回传消息的正确实现方式:在MSDN中未找到对应参考示例,目前仅在服务端代码中做了
szOutput = "Reply from the server"的赋值操作,不确定逻辑是否正确。 - 客户端接收回传消息的处理流程:需要明确如何将接收到的回传消息赋值到本地变量,实现消息的读取与使用。
内容的提问来源于stack exchange,提问作者Md Sajid
相关产品推荐
相关产品推荐

