Android Development Socket Communication--Mobile server terminal C/S architecture implementation

Source: Internet
Author: User
Tags socket blocking

Every smartphone is a miniature computer, in the school's favorite is to find 10 eight students with a local area network to open a few games of DotA, suddenly think, take a mobile phone c a game to play how, do not have WiFi, do not open data, several mobile phones choose a master machine, other mobile phone connected to its hot spots, Can interact with the game to get up, how happy ~ ~

The actual implementation process found to take the mobile phone itself to do the server is really very few people use, on the net there are few ready-made solutions, after some groping, simple implementation of the mobile phone itself to do server (can also do client) socket communication function, write to please guide you, It is also easy to comb for future reuse.

The biggest problem is probably the server-side socket blocking problem, because we need to have the server side of the socket is in the listening state, so as to ensure that the client's connection request and the client sends the message, and then broadcast to all the client machine. So we can't put ServerSocket in mainactivity because it blocks the app's operation, so you can create a new service, define ServerSocket operations in the service, In mainactivity, define a button that starts the service with a button to open the server side. My service code is as follows:

Import Java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import java.net.ServerSocket; Import Java.net.socket;import java.util.arraylist;import Android.app.service;import Android.content.intent;import Android.os.bundle;import Android.os.ibinder;import Android.os.message;public class Serversocketservice extends Service{private static arraylist<socket> socketlist = new arraylist<socket> ();p rivate ServerSocket ServerSocket = null, @Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic void OnCreate () {super.oncreate ();} @Overridepublic void OnDestroy () {Super.ondestroy ();} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {try{new Thread (new Serverthread ()). Start (); catch (Exception e) {e.printstacktrace ();} return start_sticky;} public class Serverthread implements runnable{private socket socket = Null;public Serverthread () {}public void run () {Try{s Erversocket = new ServerSocket (4567); while (true) {socket = Serversocket.aCcept (); Socketlist.add (socket); new Thread (New Handleinputmessagethread (socket)). Start ();}} catch (IOException e) {e.printstacktrace ();}}} public class Handleinputmessagethread implements runnable{private socket socket = Null;public Handleinputmessagethread ( Socket sock) {this.socket = sock;} public void Run () {while (true) {InputStream InputStream = Null;try{inputstream = Socket.getinputstream (); byte data[] = new Byte[1024*4];int i = 0;while ((i = inputstream.read (data))!=-1) {string buffer = Null;buffer = new String (data, "GBK"); Buffe R = Buffer.trim () + "\ n"; OutputStream outputstream = Null;for (Socket sock:socketlist) {outputstream = Sock.getoutputstrea M (); Outputstream.write (Buffer.getbytes ("GBK")); Outputstream.flush (); Break;}} catch (IOException e) {e.printstacktrace ();}}}}
the implementation of the client is relatively simple, the client code to connect the service side function and send message data and other functions can be, here no longer posted.

Hope to be helpful to those who need it ~ The road is long, fly slowly

Android Development Socket Communication--Mobile server terminal C/S architecture implementation

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.