How to implement TCP client receiving data in Android _android

Source: Internet
Author: User

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.

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.