Need help coding MorseCodeServer in Java Create Class MorseC

Need help coding MorseCodeServer in Java

Create Class MorseCodeServer which represents a multithreaded server application This class maintains morse code for two clients MorseCodeClient. This first version should just relay the morse code from one client to the other. This server does not need to have a Graphical interface. You will most likely need to import all of the following classes into your server. Create class MorseCodeServerTest, this is a test class for morse code server. It creates an instance of MorseCodeServerTest.

Solution

encodeMorse = new TreeMap<>(); decodeMorse = new TreeMap<>(); // Create encode and decode Maps for(int i = 0; i < characters.length; i++) { encodeMorse.put( characters[i], codes[i]); decodeMorse.put( codes[i], characters[i]); } } // end Client constructor // connect to server and process messages from server public void runClient() { Boolean socketConnected = false; try // Connect to server, get streams, process connection { // If socket creation is successful. On failure abort operation if ( socketConnected = connectToServer() ) { getStreams(); // Get the input and output streams processConnection(); // Process connection } // end if socketConnect } // end try catch ( EOFException eofException ) { displayNotice( \"Client terminated connection\" ); } // end catch catch ( IOException ioException ) { ioException.printStackTrace(); } // end catch finally { // If socket didn\'t connect, ignore socket close method if (socketConnected) closeConnection(); // close connection } // end finally } // end method runClient // Connect to server private boolean connectToServer() throws IOException { displayNotice( \"Welcome \" + name + \"!\" ); // Greet user try { // create Socket to make connection to server client = new Socket( InetAddress.getByName( chatServer ), 61000 ); } catch(IOException e) { displayNotice(\"Server connection could not be established\"); return false; } // display connection information displayNotice( \"Connected to: \" + client.getInetAddress().getHostName() ); return true; } // End connectToServer() method // Get I/O streams private void getStreams() throws IOException { // Set up output stream for objects output = new ObjectOutputStream( client.getOutputStream() ); output.writeObject(name); // Send name of chat user to server output.flush(); // Flush output buffer to send header information // set up input stream for objects input = new ObjectInputStream( client.getInputStream() ); displayNotice( \"Got I/O streams\" ); } // End getStreams() method // Process connection with server private void processConnection() throws IOException { // Enable enterField so client user can send messages setTextFieldEditable( true ); int newLine; // String index of a detected newline character while ( openConnection ) // Process messages sent from server { try // Read message and display it { message = ( String ) input.readObject(); // read new message newLine = message.indexOf(\'\ \'); // Remove name of client from coded message displayMessage( message.substring( 0, newLine) ); // Display message displayMessage( \" \" + convertToText(message.substring(newLine + 1)) ); } // End try catch ( ClassNotFoundException classNotFoundException ) { displayNotice( \"Unknown object type received\" ); } // End catch } // End while openConnection } // End processConnection() method // Close streams and socket private void closeConnection() { displayNotice( \"Closing connection\" ); setTextFieldEditable( false ); // disable enterField try { output.close(); // close output stream input.close(); // close input stream client.close(); // close socket } // end try catch ( IOException ioException ) { ioException.printStackTrace(); } // end catch } // end closeConnection() method // Send message to server private void sendData( String message ) { try // Send object to server { output.writeObject(message); // Send message to server output.flush(); // Flush data to output } // End try catch ( IOException ioException ) { displayNotice( \"Error writing object\" ); } // End catch } // End sendData() method // Manipulate displayArea in the event-dispatch thread private void displayMessage( final String messageToDisplay ) { SwingUtilities.invokeLater( new Runnable() { public void run() // Updates displayArea { displayArea.append( messageToDisplay + \"\ \"); } // End run() method } // End anonymous inner class ); // End SwingUtilities.invokeLater } // End displayMessage() method // Manipulate displayArea in the event-dispatch thread private void displayNotice( final String messageToDisplay ) { SwingUtilities.invokeLater( new Runnable() { public void run() // Updates displayArea { notificationArea.append( messageToDisplay + \"\ \"); } // End run() method } // End anonymous inner class ); // End SwingUtilities.invokeLater } // End displayMessage() method // Manipulate enterField in the event-dispatch thread private void setTextFieldEditable( final boolean editable ) { SwingUtilities.invokeLater( new Runnable() { public void run() // Sets enterField\'s editability { enterField.setEditable( editable ); } // End run() method } // End anonymous inner class ); // End SwingUtilities.invokeLater } // End setTextFieldEditable() method // Encode message into morse code private String convertToMorse( String encodeMessage ) { // Tokenize encodeMessage by whitespace String[] tempWords = encodeMessage.split(\" \"); Character tempChar; // Store each individual token StringBuilder codedString = new StringBuilder(); // Build encoded message for (String token : tempWords) // For each token from encodeMessage { for (int i = 0; i < token.length(); i++) // For each character of token { // Convert character to lowercase tempChar = Character.toLowerCase(token.charAt(i) ); // Convert character to matched morse code value if ( encodeMorse.containsKey(tempChar) ) codedString.append( encodeMorse.get( tempChar ) ); // Is no morse code equivalent, insert as literal character else codedString.append( token.charAt(i)); // If at end of token\'s length, do not add a single \'/\' separator if (i != (token.length() - 1) ) codedString.append(\'/\'); // Separate morse code characters by a / } codedString.append(\"//\"); // Separate morse code words by // } displayNotice( \" Your Encoded Message is:\ \" + codedString.toString() + \"\ \" ); return codedString.toString(); } // end convertToMorse() method // Decode message into a string private String convertToText( String decodeMessage ) { // Tokenize decodeMessage by \"//\" into Morse Code words String[] tempWords = decodeMessage.split(\"//\"); String[] tempString; // Store tokens of individual Morse Code letters StringBuilder decodedString = new StringBuilder(); // Build decodeMessage Boolean capNext = true; // Detect when a letter should be capitalized for ( String token : tempWords) // For each encoded word { tempString = token.split(\"/\"); // Break each encoded word into a single coded character for (String code : tempString) // For each encoded character of a word { // Convert coded character to matched alphabet character if ( decodeMorse.containsKey( code ) ) { if (capNext) // Capitalize next character { decodedString.append( Character.toUpperCase(decodeMorse.get( code )) ); capNext = false; } // End if capNext else decodedString.append( decodeMorse.get( code ) ); if ( code.equals(\".-.-.-\") || code.equals(\"..--..\") || code.equals(\'!\') ) capNext = true; } // End if code exists in decode Map // Is no mapped morse code equivalent, insert as literal character else decodedString.append( code ); } // End for each encoded character of a word decodedString.append(\" \"); // Separate words by a space } // End for each encoded word return decodedString.toString(); } // End convertToText() method } // End class Client
Need help coding MorseCodeServer in Java Create Class MorseCodeServer which represents a multithreaded server application This class maintains morse code for tw
Need help coding MorseCodeServer in Java Create Class MorseCodeServer which represents a multithreaded server application This class maintains morse code for tw

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site