Http.requests.* handle connection refused

I am writing a little program to search all ip adresses in my network for an appropriate respons.
But the ip addresses that do not have any server or service running give an error and even worse, it takes very long until the program continues to the next ip.
The error is not my problem, since it is only displayed in the console, but i want to limit the time it can take to wait for a respons. The servers i want to find easily respond within 500ms. So i set the program to give it 1000ms for each ip.

If anyone can help me… please speak up :smile:

Hi mano1979, have you tryed to use thread() function to parallelize the requests?

No. Mainly because i don’t know what it means i guess.
Can you show me a short example on how to use it?

Maybe post your code…


// Need G4P library
import g4p_controls.*;
import http.requests.*;
String ipAddr;
String IP;
public void setup(){
  size(480, 320, JAVA2D);
  ipAddr = KetaiNet.getIP();  
  String[] list = split(ipAddr, '.');
  IP = (list[0] + "." + list[1] + "." + list[2] + ".");

public void draw(){

public void scan(){
  for(int i=23;i<255;i++){
    println("scanning IP: " + IP + i);
    GetRequest get = new GetRequest("http://" + IP + i + "/status");
    if(get.getContent() != null){
      println("Reponse Content: " + get.getContent());
      println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
  /*textArray = txa.getTextAsArray();
    myText = join(textArray, "\n");
    println("XXX" + myText + "XXX");*/
// Use this method to add additional statements
// to customise the GUI controls
public void customGUI(){


and here the code from the G4P gui

/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:
 * Do not rename this tab!
 * =========================================================

public void button1_click1(GButton source, GEvent event) { //_CODE_:scanbutton:657135:
  println("scanbutton - GButton >> GEvent." + event + " @ " + millis());
} //_CODE_:scanbutton:657135:

public void button2_click1(GButton source, GEvent event) { //_CODE_:stopbutton:640171:
  println("stopbutton - GButton >> GEvent." + event + " @ " + millis());
} //_CODE_:stopbutton:640171:

public void button3_click1(GButton source, GEvent event) { //_CODE_:resetbutton:602799:
  println("resetbutton - GButton >> GEvent." + event + " @ " + millis());
} //_CODE_:resetbutton:602799:

public void textarea1_change1(GTextArea source, GEvent event) { //_CODE_:resulttext:484876:
  println("textarea1 - GTextArea >> GEvent." + event + " @ " + millis());
} //_CODE_:resulttext:484876:

// Create all the GUI controls. 
// autogenerated do not edit
public void createGUI(){
  surface.setTitle("WiFiDomo finder");
  imgButton1 = new GImageButton(this, 26, 3, 432, 60, new String[] { "WFD-Finder-logo-img.png", "WFD-Finder-logo-img.png", "WFD-Finder-logo-img.png" } );
  scanbutton = new GButton(this, 19, 77, 80, 30);
  scanbutton.addEventHandler(this, "button1_click1");
  stopbutton = new GButton(this, 119, 77, 80, 30);
  stopbutton.addEventHandler(this, "button2_click1");
  resetbutton = new GButton(this, 220, 77, 80, 30);
  resetbutton.addEventHandler(this, "button3_click1");
  resulttext = new GTextArea(this, 17, 121, 444, 182, G4P.SCROLLBARS_VERTICAL_ONLY | G4P.SCROLLBARS_AUTOHIDE);
  resulttext.setText("PRESS THE 'SCAN' BUTTON\r\nSCAN results will display here...\r\n Be patient! This can take up to 8 minutes");
  resulttext.addEventHandler(this, "textarea1_change1");

// Variable declarations 
// autogenerated do not edit
GImageButton imgButton1; 
GButton scanbutton; 
GButton stopbutton; 
GButton resetbutton; 
GTextArea resulttext; 

and here is the console output. The first address finds what it is looking for and the next (.24) does not find a respons.
scanbutton - GButton >> GEvent.CLICKED @ 8503
scanning IP:
Reponse Content: [3:903:1023]
Reponse Content-Length Header: 12
scanning IP:
org.apache.http.conn.HttpHostConnectException: Connection to refused
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(
	at org.apache.http.impl.client.DefaultRequestDirector.execute(
	at org.apache.http.impl.client.AbstractHttpClient.execute(
	at org.apache.http.impl.client.AbstractHttpClient.execute(
	at org.apache.http.impl.client.AbstractHttpClient.execute(
	at http.requests.GetRequest.send(Unknown Source)
	at WFDFinder.scan(
	at WFDFinder.button1_click1(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at g4p_controls.GAbstractControl.fireEvent(Unknown Source)
	at g4p_controls.GButton.mouseEvent(Unknown Source)
	at g4p_controls.GWindowImpl.mouseEvent(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at processing.core.PApplet$RegisteredMethods.handle(
	at processing.core.PApplet.handleMethods(
	at processing.core.PApplet.handleMouseEvent(
	at processing.core.PApplet.dequeueEvents(
	at processing.core.PApplet.handleDraw(
	at processing.awt.PSurfaceAWT$12.callDraw(
	at processing.core.PSurfaceNone$
Caused by: Operation timed out (Connection timed out)
	at Method)
	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(
	... 27 more
scanning IP:

This is the link to thread() processing reference:

try to parallelize scanning inside a for statement

when i have some time i help you…

Thank you, i will read it. If i can’t make sense of it, i’ll post here again (also if i was succesfull :wink:)

I had a look at the example, but if i understand right, I will have multiple threads running at the same time, all sending a get request to different ip addresses. won’t this give issues? either with the pc or the network?

I will offcourse try it. just have to figure out how to implement it. but i think i can do it.

i’ll keep you posted!

@Dennis I have found a sollution, but it means i have to make adjustments to the .net library.
However, when i make the changes and try to test my app, the changes do not make a difference. Do i need to recompile the library before i can use it? I tried restarting processing after the changes but still no difference.

I’m having trouble to rebuild the library so before i put more efford into it, i’d like to know if i have to rebuild it at all.

Sorry , but I’m not very expert in the library recompiling…

Ok. Thanks for replying anyway.