Java Reference
In-Depth Information
Escape character is '^]'.
220 pan.alephnull.com dictd 1.12.0/rf on Linux 3.0.0-14-server
<auth.mime> <499772.29595.1364340382@pan.alephnull.com>
DEFINE eng-lat gold
150 1 definitions retrieved
151 "gold" eng-lat "English-Latin Freedict dictionary"
gold [gould]
aurarius; aureus; chryseus
aurum; chrysos
.
250 ok [d/m/c = 1/0/10; 0.000r 0.000u 0.000s]
DEFINE eng-lat computer
552 no match [d/m/c = 0/0/9; 0.000r 0.000u 0.000s]
quit
221 bye [d/m/c = 0/0/0; 42.000r 0.000u 0.000s]
You can see that control response lines begin with a three-digit code. The actual defi‐
nition is plain text, terminated with a period on a line by itself. If the dictionary doesn't
contain the word you asked for, it returns 552 no match. Of course, you could also find
this out, and a lot more, by reading the RFC.
It's not hard to implement this protocol in Java. First, open a socket to a dict server—
_dict.org__ is a good one—on port 2628:
Socket
socket
=
new
Socket
(
"dict.org"
,
2628
);
Once again you'll want to set a timeout in case the server hangs while you're connected
to it:
socket
.
setSoTimeout
(
15000
);
In the dict protocol, the client speaks first, so ask for the output stream using
getOut
putStream()
:
OutputStream
out
=
socket
.
getOutputStream
();
The
getOutputStream()
method returns a raw
OutputStream
for writing data from
your application to the other end of the socket. You usually chain this stream to a more
convenient class like
DataOutputStream
or
OutputStreamWriter
before using it. For
performance reasons, it's a good idea to buffer it as well. Because the dict protocol is
text based, more specifically UTF-8 based, it's convenient to wrap this in a
Writer
:
Writer
writer
=
new
OutputStreamWriter
(
out
,
"UTF-8"
);
Now write the command over the socket:
writer
.
write
(
"DEFINE eng-lat gold\r\n"
);
Finally, flush the output so you'll be sure the command is sent over the network:
writer
.
flush
();