The Android Development Series socket programming

Source: Internet
Author: User
<span id="Label3"></p><p><p>Last week in the project encountered an interface requirement is the communication series must be socket, so in this blog I would like to talk about my socket programming encountered some of the Problems.</p></p><p><p>In fact, it is very simple to implement a socket in android, we just need to implement a socket object inside the code, and then pass in a ip/port in the object, and set some timeout time. The code is as Follows:</p></p><pre class="brush:java;gutter:true;">public class Socketthread extends Thread {private String IP = "192.168.129.1"; private int port = 12344; Private String TAG = "socket thread"; private int timeout = 60000; private int maxsize = 256; Public Socket client = null; OutputStream out; InputStream in; public Boolean isrun = true; Handler inhandler; Handler outhandler; Context context; Private String Tagtip = "===send==="; Public Socketthread (Handler handlerin, Handler handlerout, context Context) {inhandler = handlerin; Outhandler = handlerout; This.context = context; LOG.I (TAG, "create Thread socket"); } public void conn () {try {client = new Socket (ip, port); Client.setsotimeout (timeout); Client.settcpnodelay (true); in = Client.getinputstream (); out = Client.getoutputstream (); } catch (unknownhostexception e) {log.i (TAG, "connection error unknownhostexception re-fetch"); E.printstacKtrace (); Conn (); } catch (ioexception e) {log.i (TAG, "connection server io error"); E.printstacktrace (); } catch (Exception e) {log.i (TAG, "connection Server error Exception" + e.getmessage ()); E.printstacktrace (); }} @Override public void run () {conn (); String line = ""; While (isrun) {try {if (client! = Null) {log.i (TAG, "2. test data"); byte[] temp = new byte[maxsize]; int size = 0; While (size = In.read (temp)) > 0) {log.i (TAG, "received data"); byte[] res = new byte[size]; System.arraycopy (temp, 0, res, 0, size); for (int i = 0, i < size; i++) {line + = res[i] + ""; } Message msg = Inhandler.obtainmessage (); Msg.obj = line; Inhandler.sendmessage (msg); }} Else {log.i (TAG, "no connection available"); Conn (); }} catch (Exception e) {log.i (TAG, "data receive error" + e.getmessage ()); E.printstacktrace (); }}} public void Send (byte[] mess) {try {if (client! = Null) {out.write ( mess); Out.flush (); LOG.I (tagtip, "send success"); Message msg = Outhandler.obtainmessage (); Msg.obj = mess; Msg.what = 1; Outhandler.sendmessage (msg); } else {log.i (TAG, "client does not exist"); Message msg = Outhandler.obtainmessage (); Msg.obj = mess; Msg.what = 0; Outhandler.sendmessage (msg); LOG.I (TAG, "connection does not exist reconnect"); Conn (); }} Catch (Exception E){LOG.I (tagtip, "send error"); E.printstacktrace (); } finally {log.i (tagtip, "send complete"); }} public void Close () {try {if (client! = Null) {in.close (); Out.close (); Client.close (); }} catch (Exception e) {log.i (TAG, "close err"); E.printstacktrace (); } }}</pre><p><p>The above code I simply implemented a TCP protocol-based socket communication, The content of the message is byte[], of course, if you want to use the UDP protocol or send string strings, then the code needs to be Re-modified. Believe that there is a lot of information on the internet, there is no Code.</p></p><p><p>In fact, I would like to say that, in the process of socket communication, if the transmission is byte[], then you need to pay careful attention to the order of byte, and the process of splicing if it involves a certain encryption. Then we also need to be very careful to verify the correctness of the cryptographic algorithm.</p></p><p><p>Once I asked the department why do I use socket communications? His answer is simple, high security, no other reason!</p></p><p><p>The Android Development Series socket programming</p></p></span>

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.