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.