Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Socket threads java

  • 31-03-2010 4:24pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 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