Setting TCP parameters for Socket?

M

markspace

Sahm's post reminded me about this:

Investigating Socket::isReachable() a while back, I discovered that the
Socket constructor actually establishes a TCP connection. This means
you can't set TCP parameters for the inital connection. For example,
SO_TIMEOUT.

Socket sock = new Socket( hostname, port );
sock.setSoTimeout( 6000 ); // too late!

There's no way that I can see to change the time out of the initial
connection, which occurs in the first line above, in the constructor.
You can set the time out for subsequent reads, but not the first connect.

Does anyone know of a way to control various TCP parameters, esp. the
time out, for the Socket constructor?
 
M

markspace

To partially answer my own question here, it appears that the initial
connection in the Socket constructor is sensitive to Thread.interrupt().
This strikes me as a somewhat hokey solution however. I'm still
hoping for something better.



private static void test2( String hostname, int port ) {
Thread t = new Thread( new ConnectTask( hostname, port ) );
try {
Thread.sleep( 1000 );
} catch(InterruptedException ex) {}
t.interrupt();
while( t.isAlive() ) {
try {
t.join();
} catch (InterruptedException ex ) {}
}
System.out.println("Thread finished. " + t );
}



private static class ConnectTask implements Runnable {
private final String hostname;
private final int port;

public ConnectTask(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}

@Override
public void run() {
try {
Socket sock = new Socket( hostname, port );
System.out.println("created: "+sock);
} catch (IOException ex) {
System.err.println(ex);
throw new RuntimeException(ex);
}
}

}
 
M

markspace

private static void test2( String hostname, int port ) {
Thread t = new Thread( new ConnectTask( hostname, port ) );

t.start(); // *cough*
try {
Thread.sleep( 1000 );


Nope, like a dofus, I forgot to start the thread. Do that, and the
connect doesn't interrupt. No worky.
 
S

Steven Simpson

Socket sock = new Socket( hostname, port );
sock.setSoTimeout( 6000 ); // too late!

There's no way that I can see to change the time out of the initial
connection, which occurs in the first line above, in the constructor.
You can set the time out for subsequent reads, but not the first connect.

Is anything stopping you from created an unconnected Socket, then
calling connect() on it, with the timeout specified as an argument?

You could also try setSoTimeout() between the constructor and connect(),
but it appears to be for reads, not necessarily connects. Of course,
that might be the only way for other socket options.
 
D

Daniele Futtorovic

Sahm's post reminded me about this:

Investigating Socket::isReachable() a while back, I discovered that the
Socket constructor actually establishes a TCP connection. This means
you can't set TCP parameters for the inital connection. For example,
SO_TIMEOUT.

Socket sock = new Socket( hostname, port );
sock.setSoTimeout( 6000 ); // too late!

There's no way that I can see to change the time out of the initial
connection, which occurs in the first line above, in the constructor.
You can set the time out for subsequent reads, but not the first connect.

Does anyone know of a way to control various TCP parameters, esp. the
time out, for the Socket constructor?

<http://download.oracle.com/javase/6/docs/api/java/net/Socket.html>

Socket(InetAddress address, int port)
Creates a stream socket *and connects it* to the specified port number
at the specified IP address.

Socket()
Creates an *unconnected socket*, with the system-default type of
SocketImpl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top