Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Java Socket Connection Time out

843790Feb 19 2010 — edited Feb 19 2010
Goal*

I want to check host/port availability of computers in our network (~100 computers) .. Just check if host is alive and if it has port 445 opened. I want to do this somehow in parallel, so the code execution is as quick as possible.

My implementation*

I do this by creating socket connection to the host. Something like this:
        Socket s = null;

        try {

            s = new Socket();
            
            InetSocketAddress socketAddress = new InetSocketAddress(ipAddress, 445);

            s.connect(socketAddress, 3000);
            
            if (s.isConnected()) {

                // do something

            } 

        } catch (ConnectException ex) {

            //exception thrown

        } catch (IOException ex1) {

            //exception thrown

        } finally {

            try {
                // close socket
                s.close();
            } catch (IOException ex) {
                Logger.getLogger(ComputerModel.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
This code is executed in a loop, where a separate thread is created for every iteration = ~100 separate threads.

Problem:*

1.
It seems to me, that first ~10-15 connections are created ok, but then my code reaches 'IOException' catch block for every subsequent itteration and ex.getMessage() returns "connection time out" (I try to use different timeout values in the 2nd parameter. Values from 3000 up to 20000).

Is it valid to create 100 separate threads in a loop? Can't I make my NIC to bussy with this ??? What is the maximum ammount of threads, I can create? I did not use threads before, so I have no idea, if max values are near to 10, 100, 1000, or 10000 ..

2.
After my code is executed, I enter 'netstat -an' command in my command line and I get bunch of connections, which report status of SYN_SENT for several connections.

TCP 10.20.11.140:4557 10.30.11.119:445 SYN_SENT
TCP 10.20.11.140:4558 10.30.11.176:445 SYN_SENT
TCP 10.20.11.140:4559 10.30.11.100:445 SYN_SENT
TCP 10.20.11.140:4560 10.30.11.142:445 SYN_SENT
TCP 10.20.11.140:4561 10.30.11.171:445 SYN_SENT
TCP 10.20.11.140:4562 10.30.11.143:445 SYN_SENT
TCP 10.20.11.140:4563 10.30.12.12:445 SYN_SENT
TCP 10.20.11.140:4564 10.30.12.11:445 SYN_SENT
TCP 10.20.11.140:4565 10.30.12.21:445 SYN_SENT
TCP 10.20.11.140:4566 10.30.11.150:445 SYN_SENT

I also feel, that my computer network response (e.g. firefox browsing) is a bit slower ..

How shall I handle this problem properly?

Thx in advance,

Juraj

Comments

David Grieve-Oracle
http://docs.oracle.com/javafx/2/api/javafx/scene/control/Labeled.html#setContentDisplay(javafx.scene.control.ContentDisplay)
edward17
Cryptic API reference not helpful.
957628
radioButton.setContentDisplay(ContentDisplay.LEFT);
jsmith
ContentDisplay seems like a good solution - it just doesn't work for RadioButtons.
Most of the radio buttons with different content display settings in the example below end up being laid out identically.
package javafxsamples;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RadioContentDisplayFailure extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws Exception {
    VBox radios = new VBox(15);
    for (ContentDisplay display: ContentDisplay.values()) {
      RadioButton radio = new RadioButton("radio-text: " + display);
      radio.setStyle("-fx-background-color: coral; -fx-border-color: red;");
      radio.setContentDisplay(display);
      radios.getChildren().add(radio);
    }
    radios.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    stage.setScene(new Scene(radios));
    
    stage.show();
  }
}
edward17
The FXML version of this solution is ignored and the code version (as indicated) results in an error:

WARNING: com.sun.javafx.css.StyleHelper calculateValue caught: java.lang.IllegalArgumentException: Parsed value is not an Effect

Edited by: edward17 on Aug 23, 2012 12:51 PM
jsmith
The FXML version of this solution is ignored
Nobody provided as FXML solution for this, I guess you wrote one and it worked as my code sample did (i.e. the ContentDisplay setting for a radio button does nothing), which is what I would have expected given the code sample output.
the code version (as indicated) results in an error . . . .css.StyleHelper . . . Parsed value is not an Effect
The code sample I provided does not have any reported error (it just shows that ContentDisplay does not really affect the layout of a RadioButton).

I believe the stylehelper error you reported is related to some other css you use in your application which you have not published here and is unrelated to radio button layout.
David Grieve-Oracle
Yeah. I should know better. setContentDisplay has to do with the graphic, not the radio button itself. Here's a hack.
     @Override
    public void start(Stage primaryStage) {

        RadioButton rb = new RadioButton("RadioButton");        
        StackPane root = new StackPane();
        root.getChildren().add(rb);
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
       
        Node radio = rb.lookup(".radio");
        Node text = rb.lookup(".text");
        double rx = radio.getBoundsInParent().getMinX();
        double tx = text.getBoundsInParent().getMaxX();
        radio.setLayoutX(tx);
        text.setLayoutX(rx);
}
shakir.gusaroff
Here is another example (but not smart):
  
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class RadioBtns extends Application {

    @Override
    public void start(Stage primaryStage) {

        ToggleGroup grp = new ToggleGroup();
        VBox vb = new VBox(5);
        vb.setPrefWidth(50);
        
        RadioButton rbHome = new RadioButton();
        rbHome.setToggleGroup(grp);
        rbHome.setSelected(true);
        Label lHome = new Label("Home ");
        HBox hbHome = new HBox();
        hbHome.setAlignment(Pos.CENTER_RIGHT);
        hbHome.getChildren().addAll(lHome, rbHome);



        RadioButton rbCal = new RadioButton();
        rbCal.setToggleGroup(grp);
        Label lCal = new Label("Calendar ");
        HBox hbCal = new HBox();
        hbCal.setAlignment(Pos.CENTER_RIGHT);
        hbCal.getChildren().addAll(lCal, rbCal);


        RadioButton rbContact = new RadioButton();
        rbContact.setToggleGroup(grp);
        Label lContact = new Label("Contact ");
        HBox hbContact = new HBox();
        hbContact.setAlignment(Pos.CENTER_RIGHT);
        hbContact.getChildren().addAll(lContact, rbContact);
       
        vb.getChildren().addAll(hbHome, hbCal, hbContact);

        Scene scene = new Scene(vb, 300, 250);

        primaryStage.setTitle("Radio buttons");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
jsmith
Thanks for posting the hack David.

I created http://javafx-jira.kenai.com/browse/RT-24461 "Positioning of a radio button dot relative to it's label" to track whether a more elegant solution should be added to the core api or not.
edward17
Thanks Shakir, I agree is kludgy but I used your solution in the interim
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 19 2010
Added on Feb 19 2010
5 comments
522 views