Created
February 27, 2019 14:00
-
-
Save rana01645/6595d0e0c6bb6cfe2b7acd9e9c4c6a4c to your computer and use it in GitHub Desktop.
Java pinging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.trickbd.broadbandhack.App; | |
import android.os.AsyncTask; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.net.SocketAddress; | |
import java.net.UnknownHostException; | |
public class Ping extends AsyncTask<String, Void, Boolean> { | |
public Ping(Pingresponse pingresponse){ | |
this.pingresponse = pingresponse; | |
} | |
private Pingresponse pingresponse; | |
protected Boolean doInBackground(String... urls) { | |
try { | |
L.d(urls[0]+" "+urls[1]); | |
// InetAddress address = InetAddress.getByName(urls[0]); | |
// | |
// return address.isReachable(15000); | |
SocketAddress sockaddr = new InetSocketAddress(urls[0], Integer.parseInt(urls[1])); | |
// Create an unbound socket | |
Socket sock = new Socket(); | |
// This method will block no more than timeoutMs. | |
// If the timeout occurs, SocketTimeoutException is thrown. | |
int timeoutMs = 15000; // 2 seconds | |
sock.connect(sockaddr, timeoutMs); | |
return true; | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
return false; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
protected void onPostExecute(Boolean feed) { | |
if (pingresponse!=null) { | |
pingresponse.pingresponse(feed); | |
} | |
// TODO: check this.exception | |
// TODO: do something with the feed | |
} | |
public interface Pingresponse { | |
void pingresponse(Boolean resoonse); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment