Thursday, April 24, 2014

udp does not start in the thread, experts come to see


This is the thread
class mythread implements Runnable {
mythread () {run ();}
private DatagramSocket client;
private DatagramPacket sendPacket;
public void run () {
try {
client = new DatagramSocket ();
String sendStr = "notebookPC";
byte [] sendBuf;
sendBuf = sendStr.getBytes ();
InetAddress addr = InetAddress.getByName ("255.255.255.255");
int port = 6860;
sendPacket
= New DatagramPacket (sendBuf, sendBuf.length, addr, port);

client.send (sendPacket);


client.close ();
} Catch (Exception e)
{
e.printStackTrace ();
}

}
}
Then I wrote a button
Thread my1 = new Thread (new mythread ());
my1.start ();
However useless, please take a look there to write wrong?

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
1, confirm the settings over the listener a
2, there is what you run to confirm whether the method is performed. According to your code, run method indeed executed, but executed twice. my1.start () when executed once, the constructor has executed once. So you write calls run in the constructor method is simply superfluous. Under the listener you set the button's case, run method will certainly be executed, as the UDP protocol to send your message has not been successful is another matter.
Reply:
This code is in the 2.3 system that can be sent, but in the 4.0 Android system is the transmission fails, 255.255.255.255 at 4.0 which is also broadcast address bar,
Reply:
Themselves, thank you

No comments:

Post a Comment