Android THREAD/UDP Socket sample

Source: Internet
Author: User

Class Eventreadthread extends Thread {

Boolean socketcreated = false;
Datagramsocket socket;
InetAddress serveraddress;
byte datain[] = new byte[1024];

private void Delay (int ms) {
try {
Thread:
Sleep (MS);
} catch (Exception e) {
E.printstacktrace ();
}
}

public void Run () {
while (Mthreadquitflag = = 0) {

Delay (2000);


if (socketcreated = = False) {
try {
Socket = new Datagramsocket (4599);
ServerAddress = Inetaddress.getbyname ("192.168.0.23");//127.0.0.1 ");
String str = "GMX ls";
byte data[] = Str.getbytes ();
Datagrampacket packet = new Datagrampacket (data, Data.length, serveraddress, 4568);
Socket.send (packet);
Socketcreated = true;
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Read data from UDP socket");
try {
Datagrampacket packetin = new Datagrampacket (DataIn, datain.length,serveraddress, 4568);
Socket.receive (Packetin);
string result = new String (Packetin.getdata (), Packetin.getoffset (), packetin.getlength ());
SYSTEM.OUT.PRINTLN ("GMX client result--->" + result);
} catch (Exception e) {
E.printstacktrace ();
}

SYSTEM.OUT.PRINTLN ("GMX client Quit");
Return
}
}
}

Class Serverthread extends Thread {

private void Delay (int ms) {
try {
Thread:
Sleep (MS);
} catch (Exception e) {
E.printstacktrace ();
}
}
public void Run () {
try {
Datagramsocket socket = new Datagramsocket (4567);
byte data[] = new byte[1024];
Datagrampacket packet = new Datagrampacket (data, data.length);
Socket.receive (packet);
string result = new String (Packet.getdata (), Packet.getoffset (), packet.getlength ());
SYSTEM.OUT.PRINTLN ("GMX Server result--->" + result);

String str = "GMX server echo";
byte dataout[] = Str.getbytes ();
Datagrampacket packetout = new Datagrampacket (dataout, Dataout.length, Packet.getaddress (), Packet.getPort ());
Socket.send (packetout);
SYSTEM.OUT.PRINTLN ("GMX server Quit");

} catch (Exception e) {
E.printstacktrace ();
}
}
}

...

New Serverthread (). Start ();
New Eventreadthread (). Start ();

Android THREAD/UDP Socket sample

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.