Tutorial on protobuf-based Android socket communication

Source: Internet
Author: User
Tags gettext socket

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 ();
}
}
}
}

Related Article

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.