Hi, i'm working on an assignment right now which involves passing a variable series of numbers to the Server to be sorted into the correct order and returned to the client.
So far I have it connecting to the server and asking for my numbers, and I am entering each number and pressing return, and it is accepting each number individually.
One (smaller I think) problem i'm having is with the client code, specifically the while loop.. I've tried to code it so that when I enter a full stop ('.') the program will stop asking me for more numbers and move on to sending them to the server.. but instead it just crashes giving an error.. heres an example
"Enter a number: 1
Enter a number: 3
Enter a number: 2
Enter a number: 1.
java.lang.NumberFormatException: For input string: "1."
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at numberSortprogram.NumSortClient.main(NumSortClient .java:38)'"
If anyone can see the problem here please let me know
here's the client code in full
package numberSortprogram; import java.io.*; import java.net.ServerSocket; public class NumSortClient { public static void main(String[] args) { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); try { System.out.println("Welcome to the Integer Sorting Client.\n" + "What is the name of the server host?"); String hostName = br.readLine(); if (hostName.length() == 0) // if user did not enter a name hostName = "localhost"; // use the default host name System.out.println("What is the port number of the server host?"); String portNum = br.readLine(); if (portNum.length() == 0) portNum = "4444"; // default port number numSortclientHelper helper = new numSortclientHelper(hostName, portNum); System.out.println("Enter your list of numbers <return>"); int [] numbersArray = new int[20]; int i=0; while (i < numbersArray.length) { System.out.print("Enter a number: "); numbersArray[i]=Integer.parseInt(br.readLine()); if (i == '.') { break; } } String stringArray = numbersArray.toString(); // sent the number to the server helper.sendNumber(stringArray); System.out.println("The numbers have been sent to the server for sorting"); // now read the individual integers from the server int sortedArray = helper.receiveNumber(); for(int i1= 0; i1 < sortedArray; i1++){ int splitArray = helper.receiveNumber(); System.out.println(splitArray); } System.out.println("Numbers in ascending order are:-"); // helper.receiveNumber(splitArray); System.out.println("Session Over "); helper.done(); } // end try catch (Exception ex) { ex.printStackTrace( ); } //end catch } //end main } // end numSortclient class
I'll bump the post with my other problem so it's not a great big wall of text. Thanks if you took the time to read this
--- Update ---
Continued: the big problem that I have is figuring out how to send the sorted array back to the client..
So, what im trying to get to happen is-
-Client is asked to enter numbers
-Client enters series of numbers into an array
-Array is converted to a string and sent to the server
-String is then split into an array of strings and sorted using compareTo (don't know if this is the best way?)
-Sorted array is sent back to client and displayed.
fyi this is making use of streamsockets..
All of the rest of my code is below:
Server code
package numberSortprogram; import java.io.IOException; import java.net.*; import java.util.regex.PatternSyntaxException; public class NumSortServer { public static void main(String[] args) { int serverPort = 4444; // default port if (args.length == 1 ) serverPort = Integer.parseInt(args[0]); try { // instantiates a stream socket for accepting connections @SuppressWarnings("resource") ServerSocket myConnectionSocket = new ServerSocket(serverPort); System.out.println("Number sorting server ready."); while (true) { // forever loop // wait to accept a connection System.out.println("Waiting for a connection."); @SuppressWarnings("resource") MyStreamSocketClient myDataSocket = new MyStreamSocketClient(myConnectionSocket.accept( )); System.out.println("Connection Accepted"); // Receive the list of numbers from client String stringArray = myDataSocket.receiveMessage(); // read incoming values try { String[] splitArray = stringArray.split("\\s+"); String tempVar; for (int i = 0; i < splitArray.length; i++) { for(int j = 0; j < splitArray.length; j++) { if(splitArray[i].compareTo(splitArray[j + 1])<0) { tempVar = splitArray [j]; splitArray [j]= splitArray [i]; splitArray [i] = tempVar; } } } for (int i = 0; i < splitArray.length; i++) { System.out.println(splitArray[i].toString()); } } catch (PatternSyntaxException ex) { } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } } }
ClientHelper Code
package numberSortprogram; import java.net.*; import java.io.*; public class numSortclientHelper { private MyStreamSocketClient mySocket; private InetAddress serverHost; private int serverPort; numSortclientHelper(String hostName,String portNum) throws SocketException, UnknownHostException, IOException { this.serverHost = InetAddress.getByName(hostName); this.serverPort = Integer.parseInt(portNum); //Instantiates a stream-mode socket and wait for a connection. this.mySocket = new MyStreamSocketClient(this.serverHost, this.serverPort); System.out.println("Connection request made"); } // end constructor public void sendNumber( String stringArray) throws SocketException, IOException{ mySocket.sendMessage( stringArray); } //end SendNumber public int receiveNumber() throws SocketException, IOException{ String splitArray = mySocket.receiveMessage(); return Integer.parseInt(splitArray, 20); } //end receiveSum public void done( ) throws SocketException, IOException{ mySocket.close( ); } // end done }
StreamSocket Code
package numberSortprogram; import java.net.*; import java.io.*; /** * A wrapper class of Socket which contains * methods for sending and receiving messages */ public class MyStreamSocketClient extends Socket { private Socket socket; private BufferedReader input; private PrintWriter output; MyStreamSocketClient(InetAddress acceptorHost, int acceptorPort ) throws SocketException, IOException{ socket = new Socket(acceptorHost, acceptorPort ); setStreams( ); } MyStreamSocketClient(Socket socket) throws IOException { this.socket = socket; setStreams( ); } private void setStreams( ) throws IOException{ // get an input stream for reading from the data socket InputStream inStream = socket.getInputStream(); input = new BufferedReader(new InputStreamReader(inStream)); OutputStream outStream = socket.getOutputStream(); // create a PrinterWriter object for character-mode output output = new PrintWriter(new OutputStreamWriter(outStream)); } public void sendMessage(String stringArray) throws IOException { output.println(stringArray); //The ensuing flush method call is necessary for the data to // be written to the socket data stream before the // socket is closed. output.flush(); } // end sendMessage public String receiveMessage( ) throws IOException { // read a line from the data stream String message = input.readLine( ); return message; } //end receiveMessage } //end class