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!

[484 byte] By [hannesruns@hotmail.coma] at [2007-10-2 15:47:32]
# 1
RTFM http://java.sun.com/j2se/1.5.0/docs/api/java/net/Socket.html#setSoTimeout(int)
tjacobs01a at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 2
I have tried with that before, i don't get it.Can you post some samplecode?Thanks
hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 4

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!

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 5
setSOTimeout comes into effect when you attempt to read from the socket. Are you attempting to read from this socket somewhere?
hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 6
I just try to see if the port is open or listening.Thanks
hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 7

> 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.

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 8
Yes, but it takes 1 second before it gets that it don't answer.I currently working on a portscanner, if there is 5000+ ports you can't have a 1000 ms intervall before interrupt.Thanks, sorry for crossposting
hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 9

> 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.

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 10
How?Got link?
hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 11

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();

}

}

}

>

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 12

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);

}

ejpa at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 13

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);

}

}

}

hannesruns@hotmail.coma at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 14
I'm frequently amazed at the lack of common sense shown by OPs in these forums.Cannot find what symbol?and once you have found the answer, is it so difficult to look up the Javadoc yourself? There is an error in the code I posted but it's trivial to
ejpa at 2007-7-13 15:47:45 > top of Java-index,Java Essentials,New To Java...
# 15
Thanks! I've fixed it now
hannesruns@hotmail.coma at 2007-7-20 22:43:53 > top of Java-index,Java Essentials,New To Java...