Java Reference
In-Depth Information
is a little different. It begins with an existing
Socket
object that's connected to a proxy
server. It returns a
Socket
that tunnels through this proxy server to the specified host
and port. The
autoClose
argument determines whether the underlying
proxy
socket
should be closed when this socket is closed. If
autoClose
is
true
, the underlying socket
will be closed; if
false
, it won't be.
The
Socket
that all these methods return will really be a
javax.net.ssl.SSLSocket
, a
subclass of
java.net.Socket
. However, you don't need to know that. Once the secure
socket has been created, you use it just like any other socket, through its
getInput
Stream()
,
getOutputStream()
, and other methods. For example, suppose a server that
accepts orders is listening on port 7000 of
login.ibiblio.org
. Each order is sent as an ASCII
string using a single TCP connection. The server accepts the order and closes the con‐
nection. (I'm leaving out a
lot
of details that would be necessary in a real-world system,
such as the server sending a response code telling the client whether the order was
accepted.) The orders that clients send look like this:
Name: John Smith
Product-ID: 67X-89
Address: 1280 Deniston Blvd, NY NY 10003
Card number: 4000-1234-5678-9017
Expires: 08/05
There's enough information in this message to let someone snooping packets use John
Smith's credit card number for nefarious purposes. Consequently, before sending this
order, you should encrypt it. The simplest way to do that without burdening either the
server or the client with a lot of complicated, error-prone encryption code is to use a
secure socket. The following code sends the order over a secure socket:
SSLSocketFactory
factory
=
(
SSLSocketFactory
)
SSLSocketFactory
.
getDefault
();
Socket
socket
=
factory
.
createSocket
(
"login.ibiblio.org"
,
7000
);
Writer
out
=
new
OutputStreamWriter
(
socket
.
getOutputStream
(),
"US-ASCII"
);
out
.
write
(
"Name: John Smith\r\n"
);
out
.
write
(
"Product-ID: 67X-89\r\n"
);
out
.
write
(
"Address: 1280 Deniston Blvd, NY NY 10003\r\n"
);
out
.
write
(
"Card number: 4000-1234-5678-9017\r\n"
);
out
.
write
(
"Expires: 08/05\r\n"
);
out
.
flush
();
Only the first three statements in the
try
block are noticeably different from what you'd
do with an insecure socket. The rest of the code just uses the normal methods of the
Socket
,
OutputStream
, and
Writer
classes.
Reading input is no harder.
Example 10-1
is a simple program that connects to a secure
HTTP server, sends a simple
GET
request, and prints out the response.