Writing an ice application using C #

Source: Internet
Author: User
Tags constructor empty visual studio
Program ice is a kind of excellent distributed network middleware, which is much better than CORBA and more concise. The advantages I am here also inconvenient to say, there is a special article introduced, nor is today's theme. Interested can check, "Programmer" magazine seems to have a topic.
Here's how to use C # to write an ice based network application.
Environment: Windows Server 2003 Enterprise, Visual Studio.NET 2003 (. NET Framework 1.1)
First go to http://www.zeroc.com/download.html Download Ice installation package, Windows with MSI. For convenience, I downloaded the vs.net2003 special package. As follows:


Note: The Ice-1.5.1-vc71.msi installation package is required. After installation, the bin directory under the installation directory is added to the path path of the environment variable, and then the ice application can be developed in vs.net.

First, we write a slice definition file (equivalent to the IDL file in CORBA). The contents of the file are simple because we want to start with a "Hello World" program.
Named Printer.ice:
Interface Printer
{
void Printstring (string s);
};
compile with the following command: Slice2cs.exe printer.ice (if no command is found to indicate that the environment variable is not set successfully, you can use the full path of the bin directory)
This command generates Printer.cs files in the current directory:


Congratulations, first! Go on, Come on!
I built an empty solution icetest with vs.net, then added an empty project Icetest, joined the Printer.cs file, and finally created the Server.cs file (needless to say you guessed it, it's writing the server). The file directory is as follows:



Add the following code to the Server.cs:

Using System;

Namespace Icetest
{
/**////<summary>
Summary description for Server.
</summary>
public class Server
{
Public Server ()
{
//
Todo:add constructor Logic here
//
}

public static void Main (string[] args)
{
int status = 0;
Ice.communicator IC = null;
Try
{
IC = Ice.Util.initialize (ref args);
Ice.objectadapter Adapter
= Ic.createobjectadapterwithendpoints (
"Simpleprinteradapter", "default-p 10000");
Ice.object obj = new Printeri ();
Adapter.add (
Obj
Ice.Util.stringToIdentity ("Simpleprinter"));
Adapter.activate ();
Ic.waitforshutdown ();
}
catch (Exception e)
{
Console.Error.WriteLine (e);
status = 1;
}
Finally
{
if (IC!= null)
Ic.destroy ();
}
Environment.exit (status);
}
}

public class Printeri: _printerdisp
{
public override void Printstring (string s, ice.current current)
{
Console.WriteLine (s);
}
}
}
The previous code is routine, public class Printeri: _printerdisp's code is what we need (simple!). )
In the same way, we set up the Iceclienttest project, first add the Printer.cs file, and then write the Cient.cs file, the details are as follows:

Using System;

Namespace Iceclienttest
{
/**////<summary>
Summary description for Client.
</summary>
public class Client
{
Public Client ()
{
//
Todo:add constructor Logic here
//
}

public static void Main (string[] args)
{
int status = 0;
Ice.communicator IC = null;
Try
{
IC = Ice.Util.initialize (ref args);
Ice.objectprx obj = Ic.stringtoproxy (
"Simpleprinter:default-p 10000");
PRINTERPRX Printer
= Printerprxhelper.checkedcast (obj);
if (printer = null)
throw new ApplicationException ("Invalid proxy");
Printer.printstring ("Hello world!");
}
catch (Exception e)
{
Console.Error.WriteLine (e);
status = 1;
}
Finally
{
if (IC!= null)
Ic.destroy ();
}
Environment.exit (status);
}
}
}


Similarly, most of them are routine, only printer.printstring ("Hello world!"); A sentence is the key.
Now we can build the solution, and after the success we have the IceTest.exe and IceClientTest.exe files. If not, remember to add a reference to the project and join the Icecs.dll (also under the installed Bin directory).
Should be OK, let's look at the results of the operation:


The running result is very simple, the "Hello World" string that the server output client input. Is it simpler than CORBA?
Finally put out the server-side UML diagram, the client's similar will not be pasted.


Above is my reference "distributed Programming with Ice" (official website can download, is ICE software package Official document) a book to do the exercise, the content is very simple, please advise!




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.