1、RPC通信模型
2、调用截图
服务器端
PC客户端:
Android客户端:
3、remotetea
jrpcgen.jar:生成Java源文件
oncrpc.jar:框架通信调用
portmap.jar:Windows调用
4、生成Java源文件
test.x
const MAXNAMELEN = 2048; typedef string test_string<MAXNAMELEN>; program TEST_RPC_FUNCTION_NUMBER { version TEST_RPC_FUNCTION_VERSION { mcps_string TEST_TEST(string) = 1; mcps_string TEST_DO_PROCESS(string) = 2; } = 1; } = 0x20000001;
命令:java -jar jrpcgen test.x
生成的文件:
test.java
test_string.java
testClient.java(文件中的mcps_string改为test_string)
testServerStub.java(文件中的mcps_string改为test_string)
5、Server端:
引用:oncrpc.jar和4个Java文件
import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.acplt.oncrpc.OncRpcException; public class Main extends testServerStub { public Main(InetAddress bindAddr, int port) throws OncRpcException, IOException { super(bindAddr, port); // TODO Auto-generated constructor stub } public test_string TEST_TEST_1(String arg1){ System.out.println("This is test function" + arg1); return null; } public test_string TEST_DO_PROCESS_1(String arg1){ System.out.println("Got msg from client " + arg1); return null; } /** * @param args */ public static void main(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 = new Main(address, 2023); System.out.println("Is server null? " + (server == null ? true : false)); server.run(); }catch(Exception e){ System.out.println(e.getMessage()); } } }
6、Client端
import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.acplt.oncrpc.OncRpcException; import org.acplt.oncrpc.OncRpcProtocols; public class Main extends testClient { public Main(InetAddress host, int port, int protocol) throws OncRpcException, IOException { super(host, port, protocol); // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.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 = new Main(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端:
package com.fish.compass.util; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.acplt.oncrpc.OncRpcException; import org.acplt.oncrpc.OncRpcProtocols; public class Main extends testClient { public Main(InetAddress host, int port, int protocol) throws OncRpcException, IOException { super(host, port, protocol); } /** * @param args */ public static String run() { StringBuilder rtn = new StringBuilder(); 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 = new Main(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()); } return rtn.toString(); } }
Activity
package com.fish.compass; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import com.fish.compass.util.Main; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.content.Context; import android.graphics.Color; import android.media.RingtoneManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView m_TextView; @Override protected void onCreate(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(new OnClickListener() { @Override public void onClick(View v) { Thread t = new Thread(new Runnable(){ @Override public void run() { getMsg(); } }); t.start(); } }); } Handler m_Handler = new Handler(){ @Override public void handleMessage(Message msg){ super.handleMessage(msg); if(msg.what == 1){ m_TextView.setText(msg.obj.toString()); } } }; public void getMsg(){ StringBuilder sb = new StringBuilder(); try{ String msg = Main.run(); sb.append(msg); }catch(Exception ex){ sb.append(ex.getMessage()); } m_Handler.obtainMessage(1, sb.toString()).sendToTarget(); } }
源码下载:百度网盘
参考:
http://blog.csdn.net/jackliang55/article/details/7580563