The example in this article describes how Android implements TCP client reception data. Share to everyone for your reference, specific as follows:
Used with log4net to receive debugging information. Therefore, this client only receives string data through TCP and then displays it on the interface.
Receiving TCP Data
Try {
socket s = new Socket ("192.168.1.5", 8240);
InputStream InputStream = S.getinputstream ();
DataInputStream input = new DataInputStream (inputstream);
Byte[] B = new byte[10000];
while (true)
{
int length = Input.read (b);
String Msg = new String (b, 0, length, "gb2312");
LOG.V ("Data", MSG);
}
catch (Exception ex)
{
ex.printstacktrace ();
}
A thread performs a receive operation
However, if the receiving code puts the UI button handling events directly, the networkonmainthreadexception is raised directly because the socket operation cannot be performed in the main thread. This uses Asynctask to open another thread to perform the socket operation.
Activity button Event
getlogtask task = new Getlogtask ();
Task.execute (null);
Nested class public class
Getlogtask extends asynctask<void,void,string>
{
@Override
in the activity class Protected String Doinbackground (void...param) {
try {
socket s = new socket ("192.168.1.5", 8240);
InputStream InputStream = S.getinputstream ();
DataInputStream input = new DataInputStream (inputstream);
Byte[] B = new byte[10000];
while (true)
{
int length = Input.read (b);
String Msg = new String (b, 0, length, "gb2312");
LOG.V ("Data", MSG);
}
catch (Exception ex)
{
ex.printstacktrace ();
}
Return "";
}
}
Asynctask Communication with Interface threads
1, the interface needs to start and suspend the TCP receive operation.
The interface thread uses Asynctask.cancel () to notify the receiving thread to end the receive operation.
The receiving thread calls iscancelled () in Doinbackground to check whether an end receive request occurs.
2, Asynctask received data, passed to the interface display.
The receiving thread uses handler to pass data to the interface
Using handler, the data is passed as "message" to the interface processing.
Handler includes the ability to process messages and publish messages. Here, the processing message is to display the log text on the interface, and the interface thread to do it. The post message is to use log text as a parameter, call the PostMessage function, and receive the thread to do it.
Processing messages in the main thread
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
Text.settext ( Text.gettext (). ToString () + (String) msg.obj);
Post messages in the receiving thread
msg = new Message ();
Msg.obj = msgstring;
(mainactivity.this). Handler.postmessage ();
The above constitutes a simple, but available TCP way to the log receiver. Get a 360wifi or millet WiFi, and you can use the mobile phone to receive the log from the PC application.
For more information on Android-related content readers can view the site topics: "The Android Communication method Summary", "Android Debugging techniques and common problem solving method summary", "Android Development introduction and Advanced Course", "Android Multimedia operation tips Summary (audio, Video, audio, etc.), "Summary of Android Basic components usage", "Android View Overview", "Android Layout layout skills Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.