In: Computer Science
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;
}
}
//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;
}
}