ANDROIDTCP Communication, 16 binary data transfer instance

Source: Internet
Author: User

The main function for mobile phone and remote server for 16 of data to send and receive, the Tcpcontrol class to achieve 16 of the data transmission function, wherein the Send function is set to send 4 bytes of data each time, the server sends the data on the phone will be in the form of toast to display.

Code: http://download.csdn.net/detail/miaoyunzexiaobao/8151831

PS: The attachment contains the TCP debugging assistant, set the Debug assistant as a TCP server when used, and tick the send and receive columns 16 binary transceiver Selection

Application:

1. The user in the IP and port edit box corresponding value, click the Set button to complete the server parameter settings.

2. Click the Sendtest button on your phone and the phone will send data to the server.

system Code Execution Flow:

static string variable IP and static int variable port defined in 1.MainActivity

2. Set handler, which is used to display the received data from the server on the phone (handler)

3. In the OnCreate function, get the Btnset and btntest controls, set the listener separately: Btnset used to set the IP and port,btntest to send 16 binary test data to the server "6666"

4. Create a new thread in the OnCreate function that listens for data from the server, sends a message to handler whenever it receives data from the server, and handler is displayed on the phone interface when the message is received. After the thread returns message 0000 to the server, the loop

Tcpcontrol class parsing:

Code

Package Com.example.tcptest;import Java.io.datainputstream;import Java.io.dataoutputstream;import java.net.Socket; public class Tcpcontrol {static Socket s = null;static dataoutputstream dout = null;static DataInputStream din = Null;stat IC String backcmd = null;static public int recvcmd () throws Exception {if (s = = null) s = new Socket (Mainactivity.ip, MainA Ctivity. PORT), if (dout = = null) Dout = new DataOutputStream (S.getoutputstream ()), if (DIN = = null) DIN = new DataInputStream (S.GETINP Utstream ()); Thread.Sleep (0); for (int i = 0; i <; i++) {if (din.available () > 0) {/* client non 16 binary send byte[] buffer = new Byte[d In.available ()]; System.out.println (Din.available ());d in.read (buffer), Backcmd = "MSG from peer:" + new String (buffer); */int backlength = Din.available (); char[] buffer = new Char[backlength];for (int j = 0; j < backlength; ++j) {Buffer[j] = (char) din.readb Yte (); Buffer[j] = (char) (Buffer[j] + ' 0 ');} Backcmd = new String (buffer); return 1;} else {thread.sleep (0); return 0;}};return 0;} static public void Sendcmd (final String cmd) {try {if (s = = null) s = new Socket (Mainactivity.ip, Mainactivity.port); UT = null) dout = new DataOutputStream (S.getoutputstream ()), if (DIN = = null) DIN = new DataInputStream (S.getinputstream ()) ; Thread.Sleep (50, 0); In practice, the post-connection data need to be delayed to ensure that both sides link initialization complete char[] temp = new Char[4];for (int i = 0; i < cmd.length (); ++i) {Temp[i] = (char) (CMD). CharAt (i)-' 0 ');} String strtemp = new String (temp);d out.writebytes (strtemp);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

The Tcpcontrol class contains two functions for sending and receiving

Send:

This function is called when a user sends a message to the server, the function is to first determine whether the socket is empty, if it is empty, a new socket is created based on the IP and port set by the user, and then the socket is used to establish the data receiving stream. Use the Writebytes function of the send stream to send messages to the server after the message that will be sent is CMD to 16 binary

Receive:

First determine whether the socket is empty, if it is empty, according to the user set up the IP and port to create a new socket, and then use the socket to establish a data transmission stream, to determine whether the receiving stream received data, if received will be converted to 16 binary data and return 1, Detection thread in Mainactivity if the return value of the receive function is found to be 1, call handler to display the message. If a message is received, 0 is returned. The thread that mainactivity in the process will always call the receive function, querying the server for messages to be sent to the phone.

ANDROIDTCP Communication, 16 binary data transfer instance

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.