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...
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; }...
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.
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
An instructor in a large lecture class found, at the end of the semester, that the...
An instructor in a large lecture class found, at the end of the semester, that the total point distribution in his class was approximately Normal, with a mean of 530 and a standard deviation of 80. About what percent scored between 490 and 590? 30.9% 46.5% 77.3% Paleontology students went on a dig for fossils. The following data set contains the number of fossils found by a sample of 12 students. 10,13,9,50,16,13,22,18,20,14,19,12 Median: _______ fossils In my friend's coin purse,...
Create a class that calls for user to input a string. Pass the string from that...
Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d. java language
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT