Tuesday, March 1, 2011

How to view/change socket connection timeout on Linux?

When creating a Socket in Java:

new Socket(host, port);

The Socket constructor will try to connect to host:port before returning. On Windows, this fails almost immediately for unreachable hosts but for Linux it can take up to 5 minutes for the Socket to timeout.

I'm aware that if I have control over creating the Sockets, I can do:

Socket s = new Socket();
s.bind(..);
s.connect(.., timeout);

but I'd rather have the OS use a reasonable default value. Is there a way to change this setting on Linux?

Thanks

From stackoverflow
  • It's my understanding that this depends on the system's TCP/IP default timeout (240 seconds by default?)... one option is to try tweaking those, however this could affect any other programs on the same machine which rely on the timeout value. In which case, it might be safer to simply lower the "timeout" value in your Java connect() call, instead.

  • I think you want /proc/sys/net/ipv4/tcp-syn-retries. The default is usually 5 or 6 which comes out to around 3 minutes.

    Note that these are system-wide.

    Kevin : How is the time between each retry set? It seems to increase exponentially with each retry. Where is this set?
    Duck : The intervals do increase, at least up to a point. My memory is failing me here. I don't remember if that is BSD thing or TCP and I am not sure if Linux gives you a way to control it.
    Aditya Sehgal : The intervals are controlled by values called rtoMin,rtoMax and rtoInitial where rto stands for Round Trip Timeout. Basically, it denotes the time it would take for a packet to do a round trip. So, if when TCP sends the first msg, it would wait for rtoInitial time. If it fails to get a response, it will double the rto (and add some jitter value) and then try again. This will continue till maxRetries. The current rto value will never go past rtoMax.
    Duck : @Aditya Thanks for the clarification. I couldn't remember if the TCP algorithms came into play during the connect phase or if the OS was setting arbitrary timers.
    Kevin : Thanks to you both
  • I would advise against changing OS settings as it might affect other applications unexpectedly. The Socket.setSoTimeout() method might help you too.

    kd304 : May I ask the reason for the downvote so I could learn from it?

sliverlight application won't load.

hi. i'm trying to get started with silverlight 2. i have a weird bug. every app i build in vs 2008, when i run it the browser prompts me to install silverlight 2 although it's already installed and i have no problem running other silverlight websites. i'm trying to build a simple "hello world" application. i've tried uninstalling and reinstalling. nothing works, it's very frustrating. please help...

From stackoverflow
  • I've seen this before when the plugin I was running didn't match the SL framework version the app was built against. Is there any chance you've installed SL 3 but then installed the SL 2 plugin? Can you post the tag in your .aspx page that hosts the control?

    Nir : i'm installing the latest version of the plugin, the one i prompts me to install... and yes i'm sure it's silverlight 2
    James Cadd : Thanks for posting - Just a word about stack overflow, wait to hit the green check mark until your problem is fixed. Is this running in IIS or Cassini? If IIS can you post the mime type for .xap?
    Graeme Bradbury : Try removing the MinimumVersion="2.0.30523" attribute.

Monday, February 21, 2011

ServletContext and Session object

Hi,

Is the ServletContext and Session object which we get from request object (HttpServletRequest) behave the same ?

From stackoverflow
  • Sessions are user specific.

    Servlet contexts are essentially global (within the context of that servlet), meaning all users who hit that servlet will share the same servlet context.

  • do some reading

Need help with Apache's mod_rewrite to fetch file based on url

So say I have a url being requested:

sub.domain/file.bin

and I wanted this file to be fetched:

domain/hosted/sub/file.bin

How would I structure the rewrite directives? I've been working on this for days, and it just gives me a headache...

The subdomains are numerous and are added and removed often, so I'm trying to find a solution that works for all of them.

From stackoverflow
  • In sub.domain's httpd config:

    RewriteCond %{HTTP_HOST} ([^\.]+).domain
    RewriteRule ^/file.bin$ http://domain/hosted/%1/file.bin
    
    Cypher : Hrm, getting close. How could I make the "sub" dynamic based on whatever the subdomain is in the initial url?
  • Assuming the home directory for www.domain.com is domain/ and that - given any subdomain sub.domain.com - you want the files for the home directories for that sub-domain to be in domain/hosted/sub/

    Try something like this:

    RewriteEngine on
    
    // If the host is just mydomain.com, do nothing more
    // this is to prevent some recursion problems I've read of...
    RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
    RewriteRule ^.*$ - [L]
    
    // Otherwise strip off everything before mydomain
    // And add it to the start of the request
    RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
    RewriteRule ^.*$ %1%{REQUEST_URI} [QSA]
    
    // Then prefix with 'hosted'
    RewriteRule ^(.*)$ hosted/$1 [QSA,L]
    

    You may also need a wildcard entry in your DNS or something... but I will admit DNS and htaccess mod rewrite are some of my weaker points. See also http://www.webmasterworld.com/forum92/138.htm

  • Try this rule:

    RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
    RewriteCond %1 !=www
    RewriteRule !^hosted/ hosted/%1%{REQUEST_URI} [L]
    

Saving addressbookUI data to an array

I'm trying to save the kABFirstNamePropert, kABLastNameProperty and kABAddressProperty all saved into an array for recall later, I'm just not getting it, can anyone lend me a hand or point me in the right direction? Thanks. I'm a super NOOb on this.

For the naming the strings:

// setting the first name
firstName.text = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

// setting the last name
lastName.text = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

For setting the addressLabel:

NSString *address;
    address = [NSString stringWithFormat:@"%@, %@, %@, %@ %@",
               [theDict objectForKey:(NSString *)kABPersonAddressStreetKey],
               [theDict objectForKey:(NSString *)kABPersonAddressCityKey],
               [theDict objectForKey:(NSString *)kABPersonAddressStateKey],
               [theDict objectForKey:(NSString *)kABPersonAddressZIPKey],
               [theDict objectForKey:(NSString *)kABPersonAddressCountryKey]];

    self.addressLabel.text = address;

Saving the Array: What I have that's not working. ;(

- (IBAction)saveData {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *recipient = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];

    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:firstName];
    [array addObject:lastName];
    [array addObject:addressLabel];

    [array writeToFile:recipient atomically:NO];

}
From stackoverflow
  • This:

    [array addObject:firstName]; 
    

    needed to be:

    [array addObject:firstName.text];
    

    Thanks Everyone.

How to assign text size in sp value using java code..

If I assign an integer value to change a certain text size of a TextView using java code, the value is interpreted as pixel (px), now does anyone know how to assign in sp?

thanks.

-mario_blizz

From stackoverflow
  • You can use a DisplayMetrics object to help convert between pixels and scaled pixels with the scaledDensity attribute.

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    pixelSize = (int)scaledPixelSize * dm.scaledDensity; 
    

Border in jasper report

Hi ,

I am creating a jasper report .In that I want apply border to the each page.Can any one provide me some help on this.Border height should depend on the contents height of that page.

Thanks.

From stackoverflow
  • You can do this by creating a frame or a rectangle in an outer report and then put inside it a supreport. The subreport should be allowed to expand as it wants and the rectangle should be set to Stretch to the band height.