Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9251 > unrolled thread

Setting TCP parameters for Socket?

Started bymarkspace <-@.>
First post2011-10-27 09:48 -0700
Last post2011-10-27 15:40 -0700
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Setting TCP parameters for Socket? markspace <-@.> - 2011-10-27 09:48 -0700
    Re: Setting TCP parameters for Socket? markspace <-@.> - 2011-10-27 10:10 -0700
      Re: Setting TCP parameters for Socket? markspace <-@.> - 2011-10-27 11:16 -0700
    Re: Setting TCP parameters for Socket? Steven Simpson <ss@domain.invalid> - 2011-10-27 20:41 +0100
    Re: Setting TCP parameters for Socket? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-10-27 23:35 +0200
      Re: Setting TCP parameters for Socket? markspace <-@.> - 2011-10-27 15:40 -0700

#9251 — Setting TCP parameters for Socket?

Frommarkspace <-@.>
Date2011-10-27 09:48 -0700
SubjectSetting TCP parameters for Socket?
Message-ID<j8c20r$cmp$1@dont-email.me>
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?

[toc] | [next] | [standalone]


#9252

Frommarkspace <-@.>
Date2011-10-27 10:10 -0700
Message-ID<j8c3ad$m2s$1@dont-email.me>
In reply to#9251
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);
          }
       }

    }

[toc] | [prev] | [next] | [standalone]


#9256

Frommarkspace <-@.>
Date2011-10-27 11:16 -0700
Message-ID<j8c76b$ht9$1@dont-email.me>
In reply to#9252
On 10/27/2011 10:10 AM, markspace wrote:

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


[toc] | [prev] | [next] | [standalone]


#9258

FromSteven Simpson <ss@domain.invalid>
Date2011-10-27 20:41 +0100
Message-ID<jnoon8-bm2.ln1@news.simpsonst.f2s.com>
In reply to#9251
On 27/10/11 17:48, markspace wrote:
>   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.

-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [next] | [standalone]


#9259

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2011-10-27 23:35 +0200
Message-ID<j8ciqu$cqm$1@dont-email.me>
In reply to#9251
On 27/10/2011 18:48, markspace allegedly wrote:
> 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.

-- 
DF.
Determinism trumps correctness.

[toc] | [prev] | [next] | [standalone]


#9261

Frommarkspace <-@.>
Date2011-10-27 15:40 -0700
Message-ID<j8cmk7$57e$1@dont-email.me>
In reply to#9259
On 10/27/2011 2:35 PM, Daniele Futtorovic wrote:

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


Doh.  Thanks guys.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web