Question

In: Computer Science

The DictionaryClient program featured in the class lecture used the constant string SERVER for the hostname....

The DictionaryClient program featured in the class lecture used the constant string SERVER for the hostname. There is also URL object that can be used to get host information. You are to find out how to declare and use this object to replace the use of SERVER constant with the URL object when instantiating the socket. The URL object should be declared and instantiated in main program. If the URL is malformed, the program should gracefully stop. If the URL is not malformed, the program should look up the word in the dictionary. Once working, delete the SERVER constant.

package MySockets;

import java.net.*;
import java.io.*;

public class DictionaryClient {
  
   private static final String SERVER = "dict.org";
   private static final int PORT = 2628;
   private static final int TIMEOUT = 15000;

   public static void main(String[] args) {

       Socket    socket = null;
      
       try {
           socket = new Socket(SERVER, PORT);
           socket.setSoTimeout(TIMEOUT);

           System.out.print("Connected to Host : ");
           System.out.print(socket.getInetAddress() + " Port: ");
           System.out.println(socket.getPort());
           System.out.print("Connected from Host:" );
           System.out.print(socket.getLocalAddress() + " Port: ");
           System.out.println(socket.getLocalPort());
          
           InputStream in = socket.getInputStream();
           BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
          
           OutputStream out = socket.getOutputStream();
           Writer writer = new OutputStreamWriter(out, "UTF-8");
           writer = new BufferedWriter(writer);
          
           define(args[0], writer, reader);

       } catch (IOException ex) {
           System.err.println(ex);
       } finally {
           if(socket != null) {
               try {
                   socket.close();
               } catch(IOException e) {
                   System.out.println(e);
               }
           }              
       }
   }
  
   static void define(String word,
                   Writer writer,
                   BufferedReader reader) throws IOException {
       writer.write("SHOW DB\r\n");
       writer.write("DEFINE fd-eng-lat " + word + "\r\n");
       writer.flush();
      
       for(String line = reader.readLine(); line != null; line = reader.readLine()) {
           System.out.println(line);
           if(line.startsWith("250 ")) {
               writer.write("quit\r\n");
               writer.flush();
           }
       }
       return;
   }
}

Solutions

Expert Solution

//save as DictionaryClient.java file.... thank you

package MySockets;
import java.net.*;
import java.io.*;
public class DictionaryClient {
private static final int TIMEOUT = 15000;
public static void main(String[] args) {
Socket socket = null;
  
try {
       //1 URL object declared and instantiated by providing full host url and port
       URL url=new URL("http://www.dict.org:2628");
socket = new Socket(url.getHost(), url.getPort());
socket.setSoTimeout(TIMEOUT);
System.out.print("Connected to Host : ");
System.out.print(socket.getInetAddress() + " Port: ");
System.out.println(socket.getPort());
System.out.print("Connected from Host:" );
System.out.print(socket.getLocalAddress() + " Port: ");
System.out.println(socket.getLocalPort());
  
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
  
OutputStream out = socket.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer = new BufferedWriter(writer);
  
//provide word to lookup directly by replacing with args[0]
//define("happy", writer, reader);   //look for word 'happy' you can try another word

define(args[0], writer, reader);       // provide word to lookup as a command line argument   

}
catch (MalformedURLException ex) {       //2 if URL is malformed then show error message
System.err.println("Invalid URL:\n" + ex);
} catch (IOException ex) {
System.err.println(ex);
} catch (ArrayIndexOutOfBoundsException ex) {  
       //show error if command line argument is not passed as follows (if using terminal or command prompt to run program)
       //java DictionaryClient wordTofindInDictionary
       //java DictionaryClient happy
System.err.println("---- Word not provided\nPlease provide the word to lookup as command line argument");
}
finally {
if(socket != null) {
try {
socket.close();
} catch(IOException e) {
System.out.println(e);
}
}
}
}
  
static void define(String word,
Writer writer,
BufferedReader reader) throws IOException {
writer.write("SHOW DB\r\n");
writer.write("DEFINE fd-eng-lat " + word + "\r\n");
writer.flush();
  
for(String line = reader.readLine(); line != null; line = reader.readLine()) {
System.out.println(line);
if(line.startsWith("250 ")) {
writer.write("quit\r\n");
writer.flush();
}
}
return;
}
}


Related Solutions

Which of this method of class String is used to obtain a length of String object?...
Which of this method of class String is used to obtain a length of String object? What is the output of the below Java program with WHILE, BREAK and CONTINUE? int cnt=0; while(true) { if(cnt > 4)    break;    if(cnt==0) {     cnt++; continue; }   System.out.print(cnt + ",");   cnt++; } 1,2,3,4 Compiler error 0,1,2,3,4, 1,2,3,4,
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Answer ONLY in C language. Server program: The server program provides a search to check for...
Answer ONLY in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Assembly language - please post with output Question: Using the AddTwo program from class lecture as...
Assembly language - please post with output Question: Using the AddTwo program from class lecture as a reference, write a program that calculates the following expression, using registers: A = (A + B) - (C + D). Assign integer values to the EAX, EBX, ECX, and EDX registers.
Write a simple Java program that does the following: 1) Declare a constant of type String...
Write a simple Java program that does the following: 1) Declare a constant of type String to hold the words "Oakland University". 2) Declare variables of the type stated, and Prompt the user to enter in the following information and store in the variables a. Their current GPA on a 4.0 scale, into a variable of type double b. The number of credits they have so far into a variable of type int c. The amount of tuition they paid...
Redo Program 1 so that you have a template class, that works with a char, string,...
Redo Program 1 so that you have a template class, that works with a char, string, int, or double data type. Program 1 is below: #include <iostream> #include <string> #define CAPACITY 12 using namespace std; class queue { private: string strArray[CAPACITY]; int frontIndex, endIndex; int queueSize; public: queue() { frontIndex = endIndex = -1; queueSize = 0; } void enqueue(string); string dequeue(); int size() { return queueSize; } int front() { return frontIndex; } int end() { return endIndex; }...
Using string functions and all other programming features discussed in the class, write a program that...
Using string functions and all other programming features discussed in the class, write a program that will prompt a user to input their name (first and last). Ex: Please enter your first and last name: John Doe Then, output the string. Next, prompt the user to input their nickname. Ex: Enter your nickname: Rowdy Then modify the name string to consist of the person’s last name comma, nickname (backwards, in all caps, enclosed in double quotes) and first name. Then...
Socket-based Java server program
Write a Socket-based Java server program that responds to client messages as follows: When it receives a message from a client, it simply converts all the letters into ASCII characters and sends back them to the client. Write both client and server programs demonstrating this.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT