Java Reference
In-Depth Information
you to examine a number of files to see which ones are ready for reading or writing. Java
does not support this model. The other model, which works on most platforms and is the
is our plan here; the class
Pipe
encapsulates one thread and the code for copying data in one
direction; two instances are used, one to drive each direction of transfer independently of the
other.
This program allows you to connect to any text-based network service. For example, you can
talk to your system's SMTP (simple mail transport protocol) server, or the
Daytime
server
(port 13) used in several earlier recipes in this chapter:
$
java network.Telnet darian 13
Host darian; port 13
Connected OK
Sat Apr 28 14:07:41 2001
^C
$
The source code is shown in
Example 13-11
.
Example 13-11. Telnet.java
public
public class
Telnet
{
String host
;
int
class
Telnet
int
portNum
;
public
public static
static
void
void
main
(
String
[]
argv
) {
new
new
Telnet
().
talkTo
(
argv
);
}
private
private
void
void
talkTo
(
String av
[]) {
iif
(
av
.
length
>=
1
)
host
=
av
[
0
];
else
else
host
=
"localhost"
;
iif
(
av
.
length
>=
2
)
portNum
=
Integer
.
parseInt
(
av
[
1
]);
else
else
portNum
=
23
;
System
.
out
.
println
(
"Host "
+
host
+
"; port "
+
portNum
);
try
try
{
Socket s
=
new
new
Socket
(
host
,
portNum
);
// Connect the remote to our stdout
new
new
Pipe
(
s
.
getInputStream
(),
System
.
out
).
start
();
// Connect our stdin to the remote