Note that there are some explanatory texts on larger screens.

plurals
  1. POcan't ping the same IP twice
    primarykey
    data
    text
    <p>I have written this code for pinging class C IP addresses on port 6789, the thread starts when I click on a button called <code>PING</code>. It will retrieve all IP addresses that has the port 6789 open. But what I need is to refresh (re-ping) every, let's say 5 seconds, and add IPs recently joined if exist and omit ones that leave the port. Unfortunately another issue appears. When I started the application the first iteration of the <code>while (true)</code> works perfectly, and it adds any IP that had the port 6789 open to the <code>ArrayList ips_List</code> and then display it on the <code>ListView</code>, and when another device joins the port, my phone will add it to the <code>ips_List</code> also. BUT in the second iteration after the Thread sleeps 5 seconds and then begins to re-ping the IPs from (x.x.x.1 - x.x.x.254) to see if another IP had joined the port when pinging to an IP previously pinged, the Socket will throw IOException (as written in the code).</p> <p>Why is that happening?</p> <pre><code>Thread pingo = new Thread(new Runnable() { public void run() { while (true) { if (readableNetmask.equals("255.255.255.0")) { for (int i = 2; i &lt;= 25; i++) { String ip_address = readableIPAddress; String oct1 = "", oct2 = "", oct3 = "", oct4 = ""; StringTokenizer stok = new StringTokenizer( ip_address, "."); while (stok.hasMoreTokens()) { oct1 = stok.nextToken(); oct2 = stok.nextToken(); oct3 = stok.nextToken(); oct4 = stok.nextToken(); } to_ping_ip = oct1 + "." + oct2 + "." + oct3 + "." + String.valueOf(i); if (pingAddress(to_ping_ip, 6789)) { ips_List.add(to_ping_ip); } } } // delay 10 seconds, then re-ping try { Thread.sleep(5000); } catch (InterruptedException e) { break; } handler.post(new UpdateIPListViewRunnable()); } } }); pingo.start(); </code></pre> <p>PingAddress() function:</p> <pre><code>public boolean pingAddress(String ip, int port) { Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(ip, port), 200); socket.close(); } catch (IOException e) { return false; } return true; } </code></pre> <p>List where addresses appear:</p> <pre><code>static public class UpdateIPListViewRunnable implements Runnable { public void run() { arrayAdapter.clear(); for (int i = 0; i &lt; ips_List.size(); i++) { arrayAdapter.add(ips_List.get(i)); arrayAdapter.notifyDataSetChanged(); } ips_List.clear(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload