RPC Communication (Windows Edition, Android Edition)

Source: Internet
Author: User

1. RPC Communication model

2. Call

Server-side

PC Client:

Android Client:

3, Remotetea

Jrpcgen.jar: Generating Java source files

Oncrpc.jar: Frame Communication Call

Portmap.jar:Windows Call

4. Generate Java Source files

Test.x

Const Maxnamelen = 2048; typedef string test_string<MAXNAMELEN>;p rogram test_rpc_function_number{    Version test_rpc_function_version    {        = 1;         = 2;     = 1= 0x20000001;

Command: Java-jar Jrpcgen test.x

Generated files:

Test.java

Test_string.java

Testclient.java (mcps_string in file changed to test_string)

Testserverstub.java (mcps_string in file changed to test_string)

5. Server side:

References: Oncrpc.jar and 4 java files

Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.UnknownHostException;Importorg.acplt.oncrpc.OncRpcException; Public classMainextendsTestserverstub { PublicMain (InetAddress bindaddr,intPortthrowsoncrpcexception, IOException {Super(bindaddr, Port); //TODO auto-generated Constructor stub    }         Publictest_string test_test_1 (string arg1) {System.out.println ("This is test function" +arg1); return NULL; }         Publictest_string test_do_process_1 (string arg1) {System.out.println ("Got msg from client" +arg1); return NULL; }    /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        Try{System.out.println ("--server start--"); InetAddress Address=NULL; Try{                //address = Inetaddress.getlocalhost ();Address = inetaddress.getbyaddress (New byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 101});            System.out.println (Address.tostring ()); }Catch(unknownhostexception e) {System.out.println (E.getmessage ()); } Main Server=NewMain (Address, 2023); System.out.println ("Is server null?" + (Server = =NULL?true:false));                                Server.run (); }Catch(Exception e) {System.out.println (E.getmessage ()); }            }}

6. Client Side

Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.UnknownHostException;Importorg.acplt.oncrpc.OncRpcException;ImportOrg.acplt.oncrpc.OncRpcProtocols; Public classMainextendsTestClient { PublicMain (inetaddress host,intPortintprotocol)throwsoncrpcexception, IOException {Super(host, port, protocol); //TODO auto-generated Constructor stub    }    /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("--start client--"); InetAddress Address=NULL; Try{            //address = Inetaddress.getlocalhost ();Address = inetaddress.getbyaddress (New byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 101});        System.out.println (Address.tostring ()); }Catch(unknownhostexception e) {System.out.println (E.getmessage ()); }                Try{//Main client = new Main (address, 2023, oncrpcprotocols.oncrpc_tcp);//client. Test_test_1 ("SSB Test2");//client.close ();Main Client=NewMain (Address, 2023, ONCRPCPROTOCOLS.ONCRPC_TCP); Client. Test_do_process_1 ("SSB Test");                    Client.close (); }Catch(oncrpcexception e) {System.out.println (E.getmessage ()); }Catch(IOException e) {System.out.println (E.getmessage ()); }    }}

7. Android Terminal:

 PackageCom.fish.compass.util;Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.UnknownHostException;Importorg.acplt.oncrpc.OncRpcException;ImportOrg.acplt.oncrpc.OncRpcProtocols; Public classMainextendsTestClient { PublicMain (inetaddress host,intPortintprotocol)throwsoncrpcexception, IOException {Super(host, port, protocol); }    /**     * @paramargs*/     Public StaticString Run () {StringBuilder Rtn=NewStringBuilder (); Rtn.append ("--start client--"); InetAddress Address=NULL; Try{Address= Inetaddress.getbyaddress (New byte[]{(byte) 192, (byte) 168, (byte) 1, (byte) 101});        Rtn.append (Address.tostring ()); }Catch(unknownhostexception e) {rtn.append (E.getmessage ()); }                Try{Main Client=NewMain (Address, 2023, ONCRPCPROTOCOLS.ONCRPC_TCP); Client. Test_do_process_1 ("SSB phone Test");                    Client.close (); }Catch(oncrpcexception e) {rtn.append (E.getmessage ()); }Catch(IOException e) {rtn.append (E.getmessage ()); }                returnrtn.tostring (); }}

Activity

 PackageCom.fish.compass;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.net.URLConnection;ImportCom.fish.compass.util.Main;Importandroid.app.Activity;Importandroid.app.Notification;ImportAndroid.app.NotificationManager;ImportAndroid.content.Context;ImportAndroid.graphics.Color;ImportAndroid.media.RingtoneManager;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {TextView m_textview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button M_okbutton=(Button) Findviewbyid (R.id.m_okbutton); M_textview=(TextView) Findviewbyid (R.id.m_msgtextview); M_okbutton.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Thread T=NewThread (NewRunnable () {@Override Public voidrun () {getmsg ();                }                                    });                            T.start ();    }        }); } Handler M_handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); if(Msg.what = = 1) {M_textview.settext (msg.obj.toString ());        }        }    };  Public voidgetmsg () {StringBuilder sb=NewStringBuilder (); Try{String msg=Main.run ();                                Sb.append (msg); }Catch(Exception ex) {Sb.append (Ex.getmessage ()); } m_handler.obtainmessage (1, Sb.tostring ()). Sendtotarget (); }        }

SOURCE Download: Baidu Network disk

Reference:

http://blog.csdn.net/jackliang55/article/details/7580563

RPC Communication (Windows Edition, Android Edition)

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.