Merge "Avoid leaking sockets." into gingerbread
This commit is contained in:
committed by
Android (Google) Code Review
commit
9dbef975f0
@ -72,8 +72,9 @@ public class SntpClient
|
|||||||
* @return true if the transaction was successful.
|
* @return true if the transaction was successful.
|
||||||
*/
|
*/
|
||||||
public boolean requestTime(String host, int timeout) {
|
public boolean requestTime(String host, int timeout) {
|
||||||
|
DatagramSocket socket = null;
|
||||||
try {
|
try {
|
||||||
DatagramSocket socket = new DatagramSocket();
|
socket = new DatagramSocket();
|
||||||
socket.setSoTimeout(timeout);
|
socket.setSoTimeout(timeout);
|
||||||
InetAddress address = InetAddress.getByName(host);
|
InetAddress address = InetAddress.getByName(host);
|
||||||
byte[] buffer = new byte[NTP_PACKET_SIZE];
|
byte[] buffer = new byte[NTP_PACKET_SIZE];
|
||||||
@ -96,7 +97,6 @@ public class SntpClient
|
|||||||
socket.receive(response);
|
socket.receive(response);
|
||||||
long responseTicks = SystemClock.elapsedRealtime();
|
long responseTicks = SystemClock.elapsedRealtime();
|
||||||
long responseTime = requestTime + (responseTicks - requestTicks);
|
long responseTime = requestTime + (responseTicks - requestTicks);
|
||||||
socket.close();
|
|
||||||
|
|
||||||
// extract the results
|
// extract the results
|
||||||
long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET);
|
long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET);
|
||||||
@ -123,6 +123,10 @@ public class SntpClient
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (Config.LOGD) Log.d(TAG, "request time failed: " + e);
|
if (Config.LOGD) Log.d(TAG, "request time failed: " + e);
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (socket != null) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Reference in New Issue
Block a user