The Android client interacts with the server in UDP mode.

Source: Internet
Author: User

Tag: The Android client interacts with the server in UDP mode.

Client code:

/**

* Enable UDP send

*

* @author Gzh

*/

public class Client implements Runnable {

@Override

public void Run () {

try {


Wifigpsinfo = new Wifigpsinfo ();

Wifigpsinfo.setstatuscode ("32");//Sign In


Intent Intent = Getintent (); The intent object used to activate it

String acc = Intent.getstringextra ("ACC");

WIFIGPSINFO.SETACC (ACC);


InetAddress serveraddr = Inetaddress.getbyname (ServerIP);

Datagramsocket socket = new Datagramsocket ();

Byte[] BUF;

if (! "". Equals (Json.tojsonstring (wifigpsinfo))) {

BUF = (json.tojsonstring (wifigpsinfo)). GetBytes ("GBK");

} else {

BUF = ("Default message"). GetBytes ();

}

Datagrampacket packet = new Datagrampacket (buf, Buf.length,

SERVERADDR, ServerPort);

Socket.send (packet);

Socket.close ();

} catch (Exception e) {

}

}

}

/**

* Enable UDP Receive

*

* @author Gzh

*/

Class Serverthread extends Thread {

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 (), "gb2312");


try {

Jsonobject jsonobject = new Jsonobject (result);

String code = jsonobject.getstring ("code");

String msg = jsonobject.getstring ("msg");

Sign-in Status display

Message message = Handler.obtainmessage ();//Use this method to avoid creating new objects and to reduce the overhead of memory

if (code! = null && code.equals ("1005")) {

Message.what = Secuss;

Message.obj = msg;

} else if (code! = null && code.equals ("1006")) {

Message.what = ERROR;

Message.obj = msg;

}

Handler.sendmessage (message);


} catch (Jsonexception e) {

E.printstacktrace ();

}

} catch (SocketException e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}


}

}

There is always a problem with UDP mode, after the client sends the request once, the second send request will always prompt

java.net.BindException:Address already in use

It has not been solved by now. Therefore, we do not use UDP mode to interact with the server.

The Android client interacts with the server in UDP mode.

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.