Results 1 to 4 of 4
- 04-20-2011, 06:46 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
CLIENTJava 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"); } } }
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()+" ,"); } } }
- 04-21-2011, 12:51 AM #2
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
None of these posts seem useful at all..
- 04-21-2011, 03:06 AM #3
These are spam posts. Please report all spam threads and spamming users at the "Report Spam and Abuse Here" thread under "Forum Lobby".
- 04-29-2011, 07:07 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
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
CLIENTJava 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"); } } }
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
-
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
Radix Sort Problem !!!!
By javanew in forum Advanced JavaReplies: 2Last Post: 09-21-2010, 04:33 AM -
Sorting Multiple Parallel Arrays
By Pyrexkidd in forum New To JavaReplies: 7Last Post: 05-12-2010, 06:34 AM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM


LinkBack URL
About LinkBacks

Bookmarks