Home > Mobile >  How did my android studio UDP information of PC sent to come over
How did my android studio UDP information of PC sent to come over

Time:10-12

Recently with android studio to write a line snarling system, electricity by UDP protocol and a string to the android tablets, tablets after receiving show, but could not receive the data in the debugging zhong an eminent, please advice each master:
Main interface is a text, is used to display the received information,


<? The XML version="1.0" encoding="utf-8"?>
Package="com. Example. Myapplication" & gt;


Android: allowBackup="true"
The android: icon="@ mipmap/ic_launcher"
Android: label="@ string/app_name"
Android: roundIcon="@ mipmap/ic_launcher_round"
Android: supportsRtl="true"
Android: theme="@ style/AppTheme" & gt;








MainActivity file is as follows:
Package com. Example. Myapplication;

The import androidx. Appcompat. App. AppCompatActivity;
The import android. OS. Bundle;
The import java.net.DatagramPacket;
The import java.net.DatagramSocket;
The import android. OS. Handler;
The import android. OS. The Message;
The import android. Widget. TextView;

Public class MainActivity extends AppCompatActivity {
Private final static int RECEIVE_PORT=5200;//receiving port
Private Boolean listenStatus=true;//receiving thread loop identification
Private byte [] receiveInfo;//receive message information
Private ReceiveHandler ReceiveHandler=new ReceiveHandler ();
Private TextView tvMessage;//display the received information
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//into the open when the Activity receives a message thread
New UdpReceiveThread (). The start ();
TvMessage=(TextView) the findViewById (R.i which v1);
}

The class ReceiveHandler extends Handler {
@ Override
Public void handleMessage (Message MSG) {
Super. HandleMessage (MSG);
TvMessage. SetText (" to receive the data "+ receiveInfo. ToString ());
}
}

/* the UDP data receiving thread * */
Public class UdpReceiveThread extends Thread
{
@ Override
Public void the run ()
{
//super. The run ();
Try
{
While (listenStatus)
{
DatagramSocket receiveSocket=new DatagramSocket (5200);
Byte data []=new byte [1024].
DatagramPacket inPacket=new DatagramPacket (data, the data length);
ReceiveSocket. The receive (inPacket);//block type, receive the sender inPacket
//tvMessage setText (" 111 ");
ReceiveInfo=inPacket. GetData ();
ReceiveHandler. SendEmptyMessage (1);
//receiveSocket. Close ();
}
} the catch (Exception e)
{
e.printStackTrace();
}
}
}

}

I'm receiveSocket. The receive (inPacket); Statement preceded by tvMessage. SetText (" 111 "); Can add show '111', and after it can't
Displayed, may be wrong here, I use UDP test tool, the computer data is sent,
  • Related