Skip to content

Commit 7eabe56

Browse files
committed
Added timeout to subnetdevices and updated readme
1 parent ff42807 commit 7eabe56

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

app/src/main/java/com/stealthcotper/networktools/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ private void findSubnetDevices() {
221221

222222
final long startTimeMillis = System.currentTimeMillis();
223223

224-
String partialIpAddress = editIpAddress.getText().toString();
225-
226224
SubnetDevices.fromLocalAddress().findDevices(new SubnetDevices.OnSubnetDeviceFound() {
227225
@Override
228226
public void onDeviceFound(Device device) {
@@ -232,6 +230,7 @@ public void onDeviceFound(Device device) {
232230
@Override
233231
public void onFinished(ArrayList<Device> devicesFound) {
234232
float timeTaken = (System.currentTimeMillis() - startTimeMillis)/1000.0f;
233+
appendResultsText("Devices Found: " + devicesFound.size());
235234
appendResultsText("Finished "+timeTaken+" s");
236235
}
237236
});

library/src/main/java/com/stealthcopter/networktools/PortScan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ else if (IPTools.isIpAddressLocalNetwork(ia)){
105105
* @return this object to allow chaining
106106
*/
107107
public PortScan setTimeOutMillis(int timeOutMillis){
108-
if (timeOutMillis<0) throw new IllegalArgumentException("Times cannot be less than 0");
108+
if (timeOutMillis<0) throw new IllegalArgumentException("Timeout cannot be less than 0");
109109
this.timeOutMillis = timeOutMillis;
110110
return this;
111111
}

library/src/main/java/com/stealthcopter/networktools/SubnetDevices.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class SubnetDevices {
2121
private ArrayList<String> addresses;
2222
private ArrayList<Device> devicesFound;
2323
private OnSubnetDeviceFound listener;
24+
private int timeOutMillis = 2500;
2425

2526
// This class is not to be instantiated
2627
private SubnetDevices() {
@@ -92,6 +93,18 @@ public SubnetDevices setNoThreads(int noThreads) throws IllegalAccessException {
9293
return this;
9394
}
9495

96+
/**
97+
* Sets the timeout for each address we try to ping
98+
*
99+
* @return this object to allow chaining
100+
*/
101+
public SubnetDevices setTimeOutMillis(int timeOutMillis){
102+
if (timeOutMillis<0) throw new IllegalArgumentException("Timeout cannot be less than 0");
103+
this.timeOutMillis = timeOutMillis;
104+
return this;
105+
}
106+
107+
95108
public void findDevices(final OnSubnetDeviceFound listener) {
96109

97110
this.listener = listener;
@@ -134,7 +147,7 @@ public class SubnetDeviceFinderRunnable implements Runnable {
134147
public void run() {
135148
try {
136149
InetAddress ia = InetAddress.getByName(address);
137-
PingResult pingResult = Ping.onAddress(ia).doPing();
150+
PingResult pingResult = Ping.onAddress(ia).setTimeOutMillis(timeOutMillis).doPing();
138151
if (pingResult.isReachable) {
139152
Device device = new Device(ia);
140153
device.mac = ARPInfo.getMACFromIPAddress(ia.getHostAddress());

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Requires internet permission (obviously...)
5151

5252
### Port Scanning
5353

54-
A simple java based TCP port scanner, fast and easy to use.
54+
A simple java based TCP port scanner, fast and easy to use. By default it will try and guess the best timeout and threads to use while scanning depending on if the address looks like localhost, local network or remote. You can override these yourself by calling setNoThreads() and setTimeoutMillis()
5555

5656
```java
5757
// Synchronously
@@ -74,7 +74,7 @@ A simple java based TCP port scanner, fast and easy to use.
7474

7575
### Subnet Devices
7676

77-
Finds devices that respond to ping that are on the same subnet as the current device.
77+
Finds devices that respond to ping that are on the same subnet as the current device. You can set the timeout for the ping with setTimeOutMillis() \[default 2500\] and the number of threads with setNoThreads() \[default 255\]
7878

7979
```
8080
// Asynchronously

0 commit comments

Comments
 (0)