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…

sure!

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

public void draw(){
  background(230);
  
}

public void scan(){
  println("scanning");
  for(int i=23;i<255;i++){
    println("scanning IP: " + IP + i);
    GetRequest get = new GetRequest("http://" + IP + i + "/status");
    get.send();
    if(get.getContent() != null){
      println("Reponse Content: " + get.getContent());
      println("Reponse Content-Length Header: " + get.getHeader("Content-Length"));
    }
    delay(1000);
  }
  
  /*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());
  scan();
} //_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(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  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.setText("SCAN");
  scanbutton.setTextBold();
  scanbutton.addEventHandler(this, "button1_click1");
  stopbutton = new GButton(this, 119, 77, 80, 30);
  stopbutton.setText("STOP");
  stopbutton.setTextBold();
  stopbutton.addEventHandler(this, "button2_click1");
  resetbutton = new GButton(this, 220, 77, 80, 30);
  resetbutton.setText("RESET");
  resetbutton.setTextBold();
  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.setOpaque(true);
  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 192.168.192.23 finds what it is looking for and the next (.24) does not find a respons.

192.168.192.32
192.168.192.
scanbutton - GButton >> GEvent.CLICKED @ 8503
scanning
scanning IP: 192.168.192.23
Reponse Content: [3:903:1023]
Reponse Content-Length Header: 12
scanning IP: 192.168.192.24
org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.192.24 refused
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)
	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:573)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
	at http.requests.GetRequest.send(Unknown Source)
	at WFDFinder.scan(WFDFinder.java:52)
	at WFDFinder.button1_click1(WFDFinder.java:89)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1411)
	at processing.core.PApplet.handleMethods(PApplet.java:1613)
	at processing.core.PApplet.handleMouseEvent(PApplet.java:2677)
	at processing.core.PApplet.dequeueEvents(PApplet.java:2599)
	at processing.core.PApplet.handleDraw(PApplet.java:2440)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
	... 27 more
scanning IP: 192.168.192.25

This is the link to thread() processing reference:
https://processing.org/reference/thread_.html

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.