?? dictionaryclient.java
字號:
import java.net.*;
import java.io.*;
import java.util.*;
public class DictionaryClient
{
private static String serverName="localhost";
private static int port=4500;
public static void main(String[] args)
{
String meaning=null;
try
{
if(args.length==0)
throw new Exception("No parameters, use default setting");
else if(args.length!=2)
throw new Exception("Please input 2 parameters!");
else
{
serverName=args[0];
port=Integer.parseInt(args[1]);
}
}
catch (Exception e)
{
String message= e.getMessage();
System.out.println(message);
}
System.out.println("Strat using DictionaryClient");
Scanner keyboard=new Scanner(System.in);
boolean exit=false;
while(!exit)
{
System.out.println("Please input a word");
System.out.println("If you want to exit, input the word: exit!");
String inputword=keyboard.nextLine();
if(inputword.equalsIgnoreCase("exit"))
{
exit=true;
}
else
{
meaning=DictionarySearch(inputword,meaning);
if(meaning.equals("no"))
{
System.out.println("The dictionary does not have this word!");
}
else
{
System.out.println(inputword+" means "+meaning);
System.out.println();
}
}
}
}
public static String DictionarySearch(String keyword, String meaning)
{
String result="no";
try
{
Socket client=new Socket(serverName,port);
DataInputStream cinputstream = new DataInputStream(client.getInputStream());
DataOutputStream coutputstream = new DataOutputStream(client.getOutputStream());
coutputstream.writeUTF(keyword);
String explanation = new String (cinputstream.readUTF());
result = explanation;
cinputstream.close();
coutputstream.close();
client.close();
}
catch(UnknownHostException uhe)
{
System.out.println("Unknown host: " + serverName);
uhe.printStackTrace();
}
catch(IOException ioe)
{
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
return result;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -