1:下载Ice,配置Ice环境
下载地址:https://zeroc.com/downloads/archive/ice/3.4.2
分别下载并安装Ice-3.4.2.msi和Ice-3.4.2-ThirdParty.msi
环境变量配置:
检验:
2:创建服务端
添加ice.dll引用
编写ice文件
列如:实现大写转换
module ServerModule{
interface testINTF
{
void ToUpper(string aaa);
};
};
保存后右键项目属性,选Ice Configuration
勾选确认后生成一个.cs文件
启动服务与接口实现
class Server :ServerModule.testINTFDisp_
{
public static void StartServer()
{
try
{
///文件大小限制(M)
//int MessageSizeMax = 200;
///服务器线程数量
//int ThreadPoolSize = 100;
/// Ice.Communicator对象
Ice.Communicator ic = null;
Ice.InitializationData initializationData = new Ice.InitializationData();
//initializationData.properties= Ice.Util.createProperties();
//initializationData.properties.setProperty("Ice.MessageSizeMax",(MessageSizeMax * 1024).ToString());
//initializationData.properties.setProperty("Ice.ThreadPool.Server.SizeMax","5000");
//initializationData.properties.setProperty("Ice.ThreadPool.Client.SizeMax","50");
//initializationData.properties.setProperty("Ice.ACM.Client","0");
//initializationData.properties.setProperty("Ice.ACM.Server","0");
//initializationData.properties.setProperty("Ice.ThreadPool.Server.Size",ThreadPoolSize.ToString());
//初始化Ice run time
ic = Ice.Util.initialize();
//创建对象适配器,"MyIceAdapter"适配器的名字,iceTransProtocol传输协议和端口
Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("MyIceAdapter","tcp -h 127.0.0.1 -p 11000");
Server servant = new Server();
//Ice.Objectobj = servant as Ice.Object;
//告诉适配器新的servant,"MyIce"是servant的名字
adapter.add(servant, Ice.Util.stringToIdentity("MyIce"));
//**适配器
adapter.activate();
MessageBox.Show("开启成功");
}
catch(Ice.Exception ex)
{
MessageBox.Show("开启失败");
}
}
public override string ToUpper(string aaa, Ice.Current current__)
{
return aaa.ToUpper();
}
}
//Form界面添加button
private void button1_Click(object sender, EventArgs e)
{
Server.StartServer();
}
3.创建客户端
添加ice.dll引用
添加.ice编译后的.cs文件
连接服务
class ConnectServer
{
static testINTFPrx m_prxHelper = null;
public static void Connect()
{
try
{
Ice.InitializationData initializationData = new Ice.InitializationData();
//initializationData.properties= Ice.Util.createProperties();
//initializationData.properties.setProperty("Ice.MessageSizeMax","204800");
//initializationData.properties.setProperty("Ice.ThreadPool.Client.SizeMax","10");
//initializationData.properties.setProperty("Ice.ACM.Client","0");
//initializationData.properties.setProperty("Ice.ACM.Server","0");
//initializationData.properties.setProperty("Ice.Override.ConnectTimeout","2000");
//initializationData.properties.setProperty("Ice.Override.Timeout","30000");//设置ice超时时间为30秒
Ice.Communicator m_communicator = Ice.Util.initialize(initializationData);
//代理
Ice.ObjectPrx op = m_communicator.stringToProxy("MyIce:tcp-h 127.0.0.1 -p 11000");//.ice_twoway().ice_timeout(-1).ice_secure(false));
m_prxHelper = testINTFPrxHelper.checkedCast(op);
MessageBox.Show("连接成功");
}
catch(Ice.Exception ex)
{
MessageBox.Show("连接失败");
}
}
public static string toUpper(string aaa)
{
return m_prxHelper.ToUpper(aaa);
}
}
//Form
private void button1_Click(object sender, EventArgse)
{
ConnectServer.Connect();
}
private void button2_Click(objectsender, EventArgs e)
{
string aaa = textBox1.Text.Trim();
textBox2.Text = ConnectServer.toUpper(aaa);
}
4.测试