How to set timeout on clientsocket?
How do i set a timeout (if i want for example 100 ms before timeout ?)
Socket Client;
try{
Client =new Socket("127.0.0.1", 4001);
}
catch (IOException e){
System.out.println(e);
}
Thanks in advance!
How do i set a timeout (if i want for example 100 ms before timeout ?)
Socket Client;
try{
Client =new Socket("127.0.0.1", 4001);
}
catch (IOException e){
System.out.println(e);
}
Thanks in advance!
> I have tried with that before, i don't get it.
>
What do you not get?
Sample code is not required for a simple method invocation surely. It would be far more productive if you posted the code that you have tried and explained why it is not working in the way that you expected.
Here is the code:
It's still 1000 ms second timeout. Client connects before, i guess that is the problem. But if i set Client = null; before it gives nullpointerexception.
Socket Client;
try {
Client = new Socket("127.0.0.1", 4001);
Client.setSoTimeout(10);
}
catch (IOException e) {
System.out.println(e);
}
Thanks!
> I just try to see if the port is open or listening.
>
> Thanks
Then you are doing the wrong thing.
This code
Socket s = new Socket("localhost",80);
Tells you if you have a service (http) listening on port 80 of your machine. If it gets past that line without error it's connected and there is something listening on the other end.
> Yes, but it takes 1 second before it gets that it
> don't answer.
>
You can't do anything about that.
> I currently working on a portscanner, if there is
> 5000+ ports you can't have a 1000 ms intervall before
> interrupt.
>
Multiple threads.
Use a 100 threads checking different ranges. That will speed things up.
import java.net.*;
public class PortScanner implements Runnable{
private int startingport;
private String host;
private static final int PORTS_TO_SCAN = 50;
public PortScanner(int startingport, String host){
this.startingport = startingport;
this.host = host;
}
public void run(){
System.out.println("Scanning port range "+startingport+" to "+(startingport+PORTS_TO_SCAN)+" on "+host);
for(int i=startingport;i<startingport+PORTS_TO_SCAN;i++){
try{
Socket s= new Socket(host,i);
System.out.println("PORT "+i+" is open!");
}catch(Exception e){
// port was not open... ignore... somewhere before you should check if the host is reachable at all...
}
}
}
public static void main(String args[]){
for(int i=1;i<5002;i=i+PORTS_TO_SCAN){
Thread t = new Thread(new PortScanner(i,"localhost"));
t.start();
}
}
}
>
In other words you want a connect timeout, not a read timeout?
Socket client;
long timeout = 1000; // 1 second
try {
client = new Socket();
client.connect(new InetSocketAddress("127.0.0.1", 4001),timeout);
// handle success case ...
client.close();
}
catch (IOException e) {
System.out.println(e);
}
Yes, thanks!
But i get error message "cannot find symbol" on
client.connect(new InetSocketAddress("127.0.0.1", 4001),timeout);
Here is full code:
import java.io.*;
import java.net.*;
public class portscan {
public static void main(String args[]) {
Socket client;
long timeout = 1000; // 1 second
try {
client = new Socket();
client.connect(new InetSocketAddress("127.0.0.1", 4001),timeout);
// handle success case ...
client.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}