-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictClient.java
More file actions
77 lines (71 loc) · 2 KB
/
DictClient.java
File metadata and controls
77 lines (71 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Java_Inter;
/*
*
* 有问题
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.Socket;
public class DictClient {
public static final String SERVER = "dict.org";
public static final int PORT = 2628;
public static final int TIMEOUT = 15000;
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket socket = null;
try {
System.out.println("开始执行");
socket = new Socket(SERVER, PORT);
socket.setSoTimeout(TIMEOUT);
OutputStream out = socket.getOutputStream();
Writer writer = new OutputStreamWriter(out,"UTF-8");
writer = new BufferedWriter(writer);
InputStream in = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String[] arg1 = {"hello"};
for(String word : arg1){
define(word,writer,reader);
}
writer.write("quit\r\n");
System.out.println("结果"+writer);
writer.flush();
} catch (Exception e) {
// TODO: handle exception
}
finally{
//释放资源
if(socket != null){
try{
socket.close();
}
catch(IOException e){
}
}
}
}
static void define(String word,Writer writer,BufferedReader reader) throws IOException,UnsupportedEncodingException{
System.out.println("翻译");
writer.write("DEFINE eng-lat" + word + "\r\n");
writer.flush();
for(String line = reader.readLine();line != null;line = reader.readLine()){
if(line.startsWith("250 ")){//OK
//System.out.println("找到了");
return;
}
else if(line.startsWith("552 ")){//无匹配
System.out.println("No definition found for"+line);
return;
}
else if(line.matches("\\d\\d\\d .*"))continue;
else if(line.trim().equals(".")) continue;
else System.out.println(line);
}
}
}