Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Socket threads java

  • 31-03-2010 05:24PM
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭


    Hello, I'm written a simple( bad :-) ) program which distributedly finds prime numbers, however the way I wrote it is not concurrent, I made a few efforts at socket threads so that the sockets would send information concurrently but each have given annoyng errors. At the moment I query my database, find the information and number of nodes that I wish to use for distributing the calculations. Then an array of sockets is created and the information sent to each node. How would I go about editing this to make it concurrent?
    Thanks.
     public void connectToNodes() throws SQLException, IOException {
    
    
            Statement statement = connection.createStatement();
            ResultSet r = statement.executeQuery("SELECT * FROM trainsystem.operatorlist3");
            
            r.next();
    
            if (r != null) {
                r.beforeFirst();
                r.last();
                size = r.getRow();
    
                System.out.println("size of resultset: " + size);
    
                SocketArray = new Socket[size];
                PrintWriterArray = new PrintWriter[size];
                BufferedReaderArray = new BufferedReader[size];
                Operators = new String[size];
                ServerAvailable = new boolean[size];
            }
    
    
            int i = 0;
            Statement OperatorStatement = connection.createStatement();
            ResultSet rs = OperatorStatement.executeQuery("SELECT * FROM trainsystem.operatorlist3");
            while (rs.next()) {
    
    
    
    
                try {
    
    
                    Operators[i] = (rs.getString("Operator"));
    
                    System.out.println(rs.getString("Operator"));
                    k = rs.getInt("Port");
                    System.out.println(k + "port number here");
                    System.out.println(rs.getString("Port" + ""));
    
                    SocketArray[i] = new Socket("" + rs.getString("IP") + "", k);
                    PrintWriterArray[i] = new PrintWriter(SocketArray[i].getOutputStream(), true);
                    BufferedReaderArray[i] = new BufferedReader(new InputStreamReader(SocketArray[i].getInputStream()));
                    ServerAvailable[i] = true;
                    System.out.println("Operators[" + i + "]= " + Operators[i] + " @ " + SocketArray[i].getPort());
    
                } catch (IOException e) {
                    ServerAvailable[i] = false;
                    System.err.println("Possibly don't know about host5:");
                    System.out.println("Not working: " + Operators[i]);
                }
                i++;
            }
    
    
             int y = 0;
            for (int f = 0; f < size; f++) {
    
                if (ServerAvailable[f]) {
                    y++;
                }
                System.out.println(y + "this is how many are available");
    
    
            }
             maxNum = Integer.parseInt(two);
             minNum = Integer.parseInt(three);
             System.out.println("This is the current max and min"+ maxNum + " "+minNum);
            int range = maxNum - minNum;
            int packet = range / y;
            System.out.println(packet + " is the packet size");
    
            int[] anArray;
            int[] anArray2;
            anArray = new int[y];      // allocates memory
            anArray2 = new int[y];      // allocates memory
            int tempMax = 0;
             int tempMax2 = 0;
              String lol="";
           
    
            for (h = 0; h < y; h++) {
                tempMax = minNum;
                minNum = minNum + packet;
               // tempMax = minNum;
                tempMax2 =minNum;
                anArray[h] = tempMax;
                anArray2[h] = tempMax2;
                minNum++;
    
           
    
                if (ServerAvailable[h]) {
                    System.out.println("Server Available: " + ServerAvailable[h]);
                    PrintWriterArray[h].println("test" + " " + anArray2[h] + " " + anArray[h]);
                    responseLine = BufferedReaderArray[h].readLine();
                 
                    System.out.println(responseLine);
                   
    
                      lol+=responseLine;
                     
                   
    
    
                
            }
                
               
              
            }
       
    
                
        }
    


Advertisement