-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
88e096e
commit 518db1f
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
examples/src/main/java/org/bitcoinj/examples/PortOpen.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.bitcoinj.examples; | ||
|
||
import org.bitcoinj.core.NetworkParameters; | ||
import org.bitcoinj.net.discovery.PeerDiscoveryException; | ||
import org.bitcoinj.net.discovery.SeedPeers; | ||
import org.bitcoinj.params.MainNetParams; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.Socket; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Determine which of the seed nodes have open ports on mainnet | ||
*/ | ||
public class PortOpen { | ||
private static boolean available(String address, int port) { | ||
try (Socket ignored = new Socket(address, port)) { | ||
return false; | ||
} catch (IOException ignored) { | ||
return true; | ||
} | ||
} | ||
|
||
// https://www.geekality.net/2013/04/30/java-simple-check-to-see-if-a-server-is-listening-on-a-port/ | ||
public static boolean serverListening(String host, int port) { | ||
Socket s = null; | ||
try { | ||
s = new Socket(host, port); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} finally { | ||
if (s != null) | ||
try { | ||
s.close(); | ||
} catch (Exception ignored) { | ||
// ignore this exception | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
NetworkParameters params = MainNetParams.get(); | ||
|
||
SeedPeers seedPeers = new SeedPeers(params); | ||
try { | ||
int index = 0; | ||
int notAvailable = 0; | ||
for (InetSocketAddress address : seedPeers.getPeers(0, 10, TimeUnit.SECONDS)) { | ||
boolean available = serverListening(address.getAddress().getHostAddress(), params.getPort()); | ||
System.out.println(address.getAddress() + " is " + (available ? "available" : "not available")); | ||
index++; | ||
notAvailable += available ? 0 : 1; | ||
} | ||
System.out.println((notAvailable * 100 / index) + "% are not available"); | ||
} catch (PeerDiscoveryException x) { | ||
System.out.println("Error: " + x.getMessage()); | ||
} | ||
} | ||
} |