On the acquisition and usage of PROTOBUF see this article: http://www.111cn.net/sj/android/71936.htm
I'll just post my example directly below,
First, the service side:
Key Code Main.java
The code is as follows |
Copy Code |
Package com.jerome.test;
public class Main {
public static void Main (string[] args) { New Thread (New Dealthread ()). Start (); } } Dealthread.java Package com.jerome.test;
Import java.io.IOException; Import Java.io.InputStream; Import Java.net.ServerSocket; Import Java.net.Socket;
Import pbmsg. Userproto.user;
public class Dealthread implements Runnable {
ServerSocket ssocket = null;
@Override public void Run () { try { Ssocket = new ServerSocket (12345); while (true) { Socket socket = ssocket.accept (); InputStream InputStream = Socket.getinputstream ();
byte len[] = new byte[1024]; int count = Inputstream.read (len); byte[] temp = new Byte[count]; for (int i = 0; i < count; i++) { Temp[i] = Len[i]; }
User user = User.parsefrom (temp); System.out.println (User.getid ()); System.out.println (User.getusername ()); System.out.println (User.getpassword ());
User Uproto = User.newbuilder (). SetID (88888888) . SetPassword ("654321"). Setusername ("Zwq"). Build (); Uproto.writeto (Socket.getoutputstream ()); } catch (IOException e) { E.printstacktrace (); }
}
} |
second, the client
The code is as follows |
Copy Code |
Package com.jerome.test;
Import java.io.IOException; Import Java.io.InputStream; Import Java.net.Socket; Import java.net.UnknownHostException;
Import pbmsg. Userproto.user; Import android.app.Activity; Import Android.os.Bundle; Import Android.util.Log; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.TextView; public class Mainactivity extends activity {
button button = null; TextView text = null; Socket socket = NULL;
@Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);
Button = (button) Findviewbyid (R.id.button); Text = (TextView) Findviewbyid (R.id.text); Button.setonclicklistener (New Onclicklistener () {
@Override public void OnClick (View v) { Network requests must be completed in a thread;
Send Thread New Thread () { public void Run () { SendText (); } }.start ();
Accept Threads New Thread () { public void Run () { GetText (); } }.start (); }
}); }
private void SendText () { try { Socket = new Socket ("192.168.7.119", 12345); User Uproto = User.newbuilder (). SetID (147258369) . SetPassword ("123456"). Setusername ("Jerome"). Build (); Uproto.writeto (Socket.getoutputstream ());
You can also accept the stream here, or you can start a new thread to accept InputStream InputStream = Socket.getinputstream (); byte len[] = new byte[1024]; int count = Inputstream.read (len); byte[] temp = new Byte[count]; for (int i = 0; i < count; i++) { Temp[i] = Len[i]; // }
User user = User.parsefrom (temp); LOG.I ("Zwq", "Id:" + user.getid () + "___" + user.getusername () + "___" + User.getpassword ()); catch (Unknownhostexception e) { E.printstacktrace (); catch (IOException e) { E.printstacktrace (); finally { } }
private void GetText () { while (true) { try { if (null!= socket) { InputStream InputStream = Socket.getinputstream (); if (null!= inputstream) { byte len[] = new byte[1024]; int count = Inputstream.read (len); byte[] temp = new Byte[count]; for (int i = 0; i < count; i++) { Temp[i] = Len[i]; }
User user = User.parsefrom (temp); LOG.I ("Zwq", "Id:" + user.getid () + "___" + user.getusername () + "___" + User.getpassword ()); } } catch (Exception e) { E.printstacktrace (); } } } } |