Results 1 to 4 of 4
  1. #1
    Darksurf is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Parallel Radix Sorting client/server

    I'm having an issue with the way my client and server are communicating. When I use a threaded client to create 10 threads (makes 10 clients) 3 out of 10 come up scrambled, 7 out of 10 come up perfect!

    I need some help. I've been on this for over 2 weeks!

    SERVER

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    public class ParallelRadixSort implements Runnable {
    
        private static Vector connectionSocket = new Vector();
        private static Thread[] threadarray = new Thread[10];
        
        private static int index=0;
        private int i=index;
        
        private static ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
        private static Stack RadixStack = new Stack<Integer>();
        
        public static void main(String argsv[]) throws Exception {
            
            ServerSocket welcomeSocket = new ServerSocket(16788);
            
            int largest = 0;
            
            for(int i=0; i<1000; i++){
                RadixStack.push((int)(Math.random()*1000));
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            for(int i=0; i<10; i++){
               arraystack.add(new Stack<Integer>());
            }
            
            int largestValue = Integer.toString(largest).length()-1;
            stackAssign(largestValue, RadixStack);
            
            while(true){
            
                if(index<10){
                    System.out.println("Server: Listening for client request.");
                    connectionSocket.add(welcomeSocket.accept());
    
                    System.out.println("Server: Connection Established with client(s)");
                    
                    threadarray[index] = (new Thread(new ParallelRadixSort()));
            
                    threadarray[index].start();
                    
                    System.out.println("creating thread "+ index);
                    index++;
                }
                else{
                        
                    for(int j=0; j<10; j++){
                        System.out.println("joining thread "+j);
                        threadarray[j].join();
                    }
                    
                    System.out.println("thread joining complete!");
                    
                    for(int k=0; k<10; k++){
                        System.out.println(" \n Bucket "+k+":");
                        while(!arraystack.get(k).empty()){
                        System.out.print(arraystack.get(k).pop()+", ");
                        }
                        System.out.println();
                    }
                    break;
                }
            }
            
        }
      
        public static void stackAssign(int placeValue, Stack<Integer> RadixStack){
                  
            while(!RadixStack.empty()){        
                int Data = RadixStack.pop();
                int X = (int) (Data/Math.pow(10,placeValue)%10);
                arraystack.get(X).push(Data);
                System.out.println("Bucket "+X+": "+Data);
            }
        }
        
        public void run() {
            //Create an input stream to read from the socket at slot i of the array
    	try{
                Socket S = (Socket) connectionSocket.lastElement();
                connectionSocket.remove(S);
    
                ObjectOutputStream out = new ObjectOutputStream(S.getOutputStream());
                out.writeObject(arraystack.get(i));
                ObjectInputStream in = new ObjectInputStream(S.getInputStream());
                try {
                    
                    Stack<Integer> RadixDATA = (Stack<Integer>) in.readObject();
                    arraystack.add(i, RadixDATA);                
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ParallelRadixSort.class.getName()).log(Level.SEVERE, null, ex);
                }
                out.close();
                in.close();
                S.close();
                System.out.println("Server: Done with client thread "+i);
                }
                
            catch(IOException ioe){
                System.out.println("IO failed");
                }
        }
        
    }
    CLIENT

    Java Code:
    import java.util.*;
    import java.net.*;
    import java.io.*;
    
    public class RadixSortClient implements Runnable {
        
        private static Vector connectionSocket = new Vector(); 
        private static int index=0;
        private int i=index;
        
        public static void main(String argsv[]) throws Exception {
            
            while(index<10){
    
                System.out.println("Client: starting MultiClient thread(s)!");
                (new Thread(new RadixSortClient())).start();
            
                index++;
            }
            
        }
        
        public static Stack<Integer> RadixSort(Stack<Integer> RadixDATA){
            
            ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
            Stack RadixStack = new Stack<Integer>();
            int largest=0;
            int largestValue=0;
            int Data=0;
            int X=0;
            
            while(!RadixDATA.empty()){
                RadixStack.push(RadixDATA.pop());
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            largestValue = Integer.toString(largest).length();
            // create array of stacks
            for (int i=0; i<10; i++){
                arraystack.add(new Stack<Integer>());
            }
            
            for(int placeValue=0; placeValue<largestValue; placeValue++){
                //sort numbers in array of stacks
                while(!RadixStack.empty()){
                    Data=(int)RadixStack.pop();
                    X = (int) (Data/Math.pow(10,placeValue)%10);
                    arraystack.get(X).push(Data);
                }
                //push back to single stack
               for(int k=0; k<10; k++){
                   while(!arraystack.get(k).empty()){
                    RadixStack.push((arraystack.get(k).pop()));
                   }
               }
                
            }
                    
                    return RadixStack;
        }
      
        public void run(){
            try{
                System.out.println("Client: Connecting to server....");
                Socket ConnectionSocket = new Socket("localhost",16788);
                System.out.println("Client "+i+" : Connection Esablished!");
                Stack RadixDATA = new Stack<Integer>();
               
                ObjectInputStream in = new ObjectInputStream(ConnectionSocket.getInputStream());
                
                //Object capture and Radix Sort Code here
                try{
                    RadixDATA = (Stack<Integer>) in.readObject();
                }
                catch(Exception e){
                    System.out.println("Stack capture failed!");
                }
                
                try{
                    RadixDATA = RadixSort(RadixDATA);
                }
                catch(Exception e){
                    System.out.println("RadixSort Failed!");
                }
                
                 //create and output stream to the client socket
                 ObjectOutputStream out = new ObjectOutputStream(ConnectionSocket.getOutputStream()); 
                 out.writeObject(RadixDATA);
                 System.out.println("Client "+i+" is done writing");
                
                in.close();
                out.close();
                ConnectionSocket.close();
                System.out.println("Client: Done with server thread "+i);
                }
                
            catch(IOException ioe){
                System.out.println("IO failed");
                }
        }
        
    }

    Radix Sort Algorithm (just for reference, if anyone wants it)

    Java Code:
    import java.util.*;
    
    public class RadixSortingAlgorithm {
    
        public static void main(String argsv[]){
           
            ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>(); 
            Stack RadixStack = new Stack<Integer>();
            int largest=0;
            int largestValue=0;
            int Data=0;
            int X=0;
            
            for(int i=0; i<1000; i++){
                RadixStack.push((int)(Math.random()*1000));
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            largestValue = Integer.toString(largest).length();
            
            // create array of stacks
            for (int i=0; i<10; i++){
                arraystack.add(new Stack<Integer>());
            }
            
            for(int placeValue=0; placeValue<largestValue; placeValue++){
                //sort numbers in array of stacks
                while(!RadixStack.empty()){
                    Data=(int)RadixStack.pop();
                    X = (int) (Data/Math.pow(10,placeValue)%10);
                    arraystack.get(X).push(Data);
                }
                
                //push back to single stack
                for(int k=0; k<10; k++){
                   while(!arraystack.get(k).empty()){
                    RadixStack.push((arraystack.get(k).pop()));
                   }
                }
               
            }
                
            while(!RadixStack.empty()){
                   System.out.print(RadixStack.pop()+" ,");
                }
            }
        }

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    None of these posts seem useful at all..

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    These are spam posts. Please report all spam threads and spamming users at the "Report Spam and Abuse Here" thread under "Forum Lobby".

  4. #4
    Darksurf is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    I figured it out. The error was on the server side. I was adding to the arraylist of stacks without deleting what was in its place first. I thought It was replacing the stacks in slot i, but in actuality I was adding to slot i and moving the unorganized stacks over. That was why I was still ending up with 3 unorganized stacks.

    Here is the finished project, ENJOY :)

    SERVER

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    public class ParallelRadixSort implements Runnable {
    
        private static Vector connectionSocket = new Vector();
        private static Thread[] threadarray = new Thread[10];
        
        private static int index=0;
        private int i=index;
        
        private static ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
        private static Stack RadixStack = new Stack<Integer>();
        
        public static void main(String argsv[]) throws Exception {
            
            ServerSocket welcomeSocket = new ServerSocket(16788);
            
            int largest = 0;
            
            for(int i=0; i<1000; i++){
                RadixStack.push((int)(Math.random()*1000));
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            for(int i=0; i<10; i++){
               arraystack.add(new Stack<Integer>());
            }
            
            int largestValue = Integer.toString(largest).length()-1;
            stackAssign(largestValue, RadixStack);
            
            while(true){
            
                if(index<10){
                    System.out.println("Server: Listening for client request.");
                    connectionSocket.add(welcomeSocket.accept());
    
                    System.out.println("Server: Connection Established with client(s)");
                    
                    threadarray[index] = (new Thread(new ParallelRadixSort()));
            
                    threadarray[index].start();
                    
                    System.out.println("creating thread "+ index);
                    index++;
                }
                else{
                        
                    for(int j=0; j<10; j++){
                        System.out.println("joining thread "+j);
                        threadarray[j].join();
                    }
                    
                    System.out.println("thread joining complete!");
                    
                    for(int k=0; k<10; k++){
                        System.out.println(" \n Bucket "+k+":");
                        while(!arraystack.get(k).empty()){
                        System.out.print(arraystack.get(k).pop()+", ");
                        }
                        System.out.println();
                    }
                    break;
                }
            }
            
        }
      
        public static void stackAssign(int placeValue, Stack<Integer> RadixStack){
                  
            while(!RadixStack.empty()){        
                int Data = RadixStack.pop();
                int X = (int) (Data/Math.pow(10,placeValue)%10);
                arraystack.get(X).push(Data);
                System.out.println("Bucket "+X+": "+Data);
            }
        }
        
        public void run() {
            //Create an input stream to read from the socket at slot i of the array
    	try{
                Socket S = (Socket) connectionSocket.lastElement();
                connectionSocket.remove(S);
    
                ObjectOutputStream out = new ObjectOutputStream(S.getOutputStream());
                out.writeObject(arraystack.get(i));
                ObjectInputStream in = new ObjectInputStream(S.getInputStream());
                try {
                    
                    Stack<Integer> RadixDATA = (Stack<Integer>) in.readObject();
                    arraystack.remove(i);
                    arraystack.add(i, RadixDATA);                
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ParallelRadixSort.class.getName()).log(Level.SEVERE, null, ex);
                }
                out.close();
                in.close();
                S.close();
                System.out.println("Server: Done with client thread "+i);
                }
                
            catch(IOException ioe){
                System.out.println("IO failed");
                }
        }
        
    }
    CLIENT

    Java Code:
    import java.util.*;
    import java.net.*;
    import java.io.*;
    
    public class RadixSortClient implements Runnable {
        
        private static Vector connectionSocket = new Vector(); 
        private static int index=0;
        private int i=index;
        
        public static void main(String argsv[]) throws Exception {
            
            while(index<10){
    
                System.out.println("Client: starting MultiClient thread(s)!");
                (new Thread(new RadixSortClient())).start();
            
                index++;
            }
            
        }
        
        public static Stack<Integer> RadixSort(Stack<Integer> RadixDATA){
            
            ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>();
            Stack RadixStack = new Stack<Integer>();
            int largest=0;
            int largestValue=0;
            int Data=0;
            int X=0;
            
            while(!RadixDATA.empty()){
                RadixStack.push(RadixDATA.pop());
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            largestValue = Integer.toString(largest).length();
            // create array of stacks
            for (int i=0; i<10; i++){
                arraystack.add(new Stack<Integer>());
            }
            
            for(int placeValue=0; placeValue<largestValue; placeValue++){
                //sort numbers in array of stacks
                while(!RadixStack.empty()){
                    Data=(int)RadixStack.pop();
                    X = (int) (Data/Math.pow(10,placeValue)%10);
                    arraystack.get(X).push(Data);
                }
                //push back to single stack
               for(int k=0; k<10; k++){
                   while(!arraystack.get(k).empty()){
                    RadixStack.push((arraystack.get(k).pop()));
                   }
               }
                
            }
                    
                    return RadixStack;
        }
      
        public void run(){
            try{
                System.out.println("Client: Connecting to server....");
                Socket ConnectionSocket = new Socket("localhost",16788);
                System.out.println("Client "+i+" : Connection Esablished!");
                Stack RadixDATA = new Stack<Integer>();
               
                ObjectInputStream in = new ObjectInputStream(ConnectionSocket.getInputStream());
                
                //Object capture and Radix Sort Code here
                try{
                    RadixDATA = (Stack<Integer>) in.readObject();
                }
                catch(Exception e){
                    System.out.println("Stack capture failed!");
                }
                
                try{
                    RadixDATA = RadixSort(RadixDATA);
                }
                catch(Exception e){
                    System.out.println("RadixSort Failed!");
                }
                
                 //create and output stream to the client socket
                 ObjectOutputStream out = new ObjectOutputStream(ConnectionSocket.getOutputStream()); 
                 out.writeObject(RadixDATA);
                 System.out.println("Client "+i+" is done writing");
                
                in.close();
                out.close();
                ConnectionSocket.close();
                System.out.println("Client: Done with server thread "+i);
                }
                
            catch(IOException ioe){
                System.out.println("IO failed");
                }
        }
        
    }

    Radix Sort Algorithm (just for reference, if anyone wants it)

    Java Code:
    import java.util.*;
    
    public class RadixSortingAlgorithm {
    
        public static void main(String argsv[]){
           
            ArrayList<Stack<Integer>> arraystack = new ArrayList<Stack<Integer>>(); 
            Stack RadixStack = new Stack<Integer>();
            int largest=0;
            int largestValue=0;
            int Data=0;
            int X=0;
            
            for(int i=0; i<1000; i++){
                RadixStack.push((int)(Math.random()*1000));
                if((int)RadixStack.peek()>largest)
                    largest=(int)RadixStack.peek();
            }
            
            largestValue = Integer.toString(largest).length();
            
            // create array of stacks
            for (int i=0; i<10; i++){
                arraystack.add(new Stack<Integer>());
            }
            
            for(int placeValue=0; placeValue<largestValue; placeValue++){
                //sort numbers in array of stacks
                while(!RadixStack.empty()){
                    Data=(int)RadixStack.pop();
                    X = (int) (Data/Math.pow(10,placeValue)%10);
                    arraystack.get(X).push(Data);
                }
                
                //push back to single stack
                for(int k=0; k<10; k++){
                   while(!arraystack.get(k).empty()){
                    RadixStack.push((arraystack.get(k).pop()));
                   }
                }
               
            }
                
            while(!RadixStack.empty()){
                   System.out.print(RadixStack.pop()+" ,");
                }
            }
        }

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2010, 11:31 AM
  2. Radix Sort Problem !!!!
    By javanew in forum Advanced Java
    Replies: 2
    Last Post: 09-21-2010, 04:33 AM
  3. Sorting Multiple Parallel Arrays
    By Pyrexkidd in forum New To Java
    Replies: 7
    Last Post: 05-12-2010, 06:34 AM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •