This article mainly introduces two parts of the content:
- Introduction to Using Thrift in C #
- Create a server with Java, create a client in C # to interact with thrift.
The Java service side that is created by using the RPC learning----Thrift Quick Start and Java Simple example, this article.
I. Introduction to Using Thrift in C #
For an introduction to RPC, refer to: RPC Learning----Thrift QuickStart and Java simple example
1. Download Thrift
1) Click to download: thrift-0.9.1.tar.gz (or http://thrift.apache.org/download)
2) Thrift Compiler for Windows (Thrift-0.9.1.exe)
All two are to be downloaded.
2. Introduction of Thrift.dll
Here will download the good. gz file after extracting and then locate the Lib directory
With VS opened, as shown, then right--"Regenerate---" generated thrift.dll
3. Generate CS File
Hello.thrift
Service HelloWorldService { string sayHello (1:string username)}
To generate a CS file using the command:
thrift-0.9. 1. Exe-gen CSharp Hello.thrift
About How to use thrift-0.9.1.exe you can view the command: thrift-0.9.1.exe-help
Copy the generated HelloWorldService.cs file into the project.
C # Client sends a message to a Java-generated server for cross-platform operation
1, start the Java version of the server
2. Use vs to create a new WinForm program
button click event:
Private void button1_click (object sender, EventArgs e) { ifnull ) { new helloworldserviceclient(). Startclient(TextBox1.Text.Trim ()); } }
HelloWorldServiceClient.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingThrift.protocol;usingThrift.transport;usingThrift;namespacethrfitcsharp{class helloworldserviceclient { Public Const stringServerIP ="localhost"; Public Static intServerPort =8090; Public Static intTIMEOUT = the; Public void startclient(String username) {Ttransport transport=NULL; Try{Transport=NewTsocket (ServerIP, ServerPort, TIMEOUT); //agreement to be consistent with the service sideTprotocol protocol =NewTcompactprotocol (transport); Helloworldservice.client Client=Newhelloworldservice.client (protocol); Transport. Open (); String result= Client.sayhello (username); Console.WriteLine ("Thrift Client result =:"+result); } Catch(Exception e) {Console.WriteLine (e.stacktrace); } finally { if(NULL!=transport) { //CloseTransport. Close (); } } } }}
HelloWroldImpl.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethrfitcsharp{classHellowroldimpl: helloworldservice.iface { Public string SayHello(stringusername) {Console.WriteLine ("Hello"+username); return "Hello"+username; } }}
:
Note: The following is an improved version, the main addition of pure C # version of:
Pure C # is about implementing the client and server in C #, and here is the output of the Pure C # Edition:
This article source :https://github.com/amosli/rpc/tree/thriftCsharp
C # Implementation Key reference:http://www.cnblogs.com/hanmos/archive/2011/09/15/2177891.html
RPC Learning--c# Using thrift Introduction, C # client and Java service side interact with each other