服务器程序用socket听5010端口,接收一个RPC调用叫"Ping",你需要实现callPing()
using System;
using System.Collections.Generic;
using System.Text;
using XmlRpcLib;
namespace XMLRPCServer1
{
class Program
{
public static XmlRpcServer Server;
static void Main(string[] args)
{
Server = new XmlRpcServer("127.0.0.1", 5010);
Server.Start();
Server.RegisterMethod("Ping", new dCallMethod(callPing));
}
static GbxCall callPing(GbxCall in_call, XmlRpcServer_Client in_client)
{
// process request ...
//Console.WriteLine("> client #" + in_client.Id + " says: "
// + in_call.Params[0].ToString() + ":"
// + in_call.Params[1].ToString());
// process response ...
GbxCall response = new GbxCall(new object[] { true });
response.Handle = in_call.Handle;
return response;
}
}
}
客户端直接发送"Ping"和想带的参数就行了,需要多个RPC调用的时候在server上多加RegisterMethod
using System;
using System.Collections.Generic;
using System.Text;
using TMXmlRpcLib;
namespace XMLRPCClient
{
class Program
{
static void Main(string[] args)
{
XmlRpcClient Client = new XmlRpcClient("127.0.0.1", 6310);
if (Client.Connect() != 0)
Console.WriteLine("Failed to connect to server."); // xou throw exception.
// send a test call to our server ...
GbxCall Result = Client.Request("Test", new object[] { "hello, if you see this message, the example worked!" });
// if the call was successfull ...
if (!Result.Error)
{
Console.WriteLine("Ok!");
}
// wait for the user to cancel ...
Console.ReadLine();
}
}
}
其中用到的TMXmlRpcLib就是C#的XMLRPC的包了。