Recently, in the development of Android encountered a problem, that is, when the server and the client to communicate with the UDP protocol, how to determine the beginning of the two sides to establish a connection.
"Connection establishment" is easy to implement, the server is command, return a similar ACK information, the client uses the Datagramsocket.receive () method to receive information, you can know that both parties have completed an interaction, communication success. and to determine "connection failure", that is, the client in datagramsocket.receive () This step will cause blocking.
Think of two solutions, one is to design a timer, datagramsocket.receive () in a certain period of time without receiving a message that the communication failed; One is the client actively to scan the server IP and port, confirm that the server is present and started.
Later find a bit of information, found Datagramsocket existence method Setsotimeout (), the method has helped us set a timer, over the specified timeout value, will throw socketexception.
The following is a partial code for the client
private void SendMessage (String str) {try {datagramsocket socket =new datagramsocket (); InetAddress serveraddress = Inetaddress.getbyname (settings.ipnum); byte data[] = Str.getbytes (); Datagrampacket outpacket = new Datagrampacket (data, Data.length, serveraddress, settings.socketnum); Socket.send (Outpacket); System.out.println (New String (Outpacket.getdata ())); byte[] buffer = "ACK". GetBytes (); Datagrampacket inpacket = new Datagrampacket (buffer, buffer.length); try {//Set timeout time, 3 seconds socket.setsotimeout (3000); Socket.receive (Inpacket); System.out.println (New String (Inpacket.getdata ())); } catch (Exception e) {toast.maketext (connectactivity.this, "Connection Failed", Toast.length_short). Show (); Throw e; } finally{Socket.close (); } Intent Intent = new Intent (connectactiVity.this,screenactivity.class); ConnectActivity.this.startActivity (Intent); ConnectActivity.this.finish (); Toast.maketext (Connectactivity.this, "Connection succeeded", Toast.length_short). Show (); } catch (Exception e) {e.printstacktrace (); } }
Note:
public void setsotimeout (int timeout) throws SocketException
Enables/disables so_timeout with the specified time-out value, in milliseconds. When this option is set to a non-zero time-out value, the call to receive () for this datagramsocket will only block this length. If the timeout value is exceeded, java.net.SocketTimeoutException is raised, although Datagramsocket is still valid. option must be enabled before entering the blocking operation to take effect. The timeout value must be a number of > 0. A timeout value of 0 is interpreted as an infinity timeout value.
Parameters:
Timeout-Specifies the timeout value in milliseconds.
Thrown:
SocketException-If there is an error in the underlying protocol, such as a UDP error.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The success or failure judgment of Android UDP communication