Java Reference
In-Depth Information
System
.
err
.
println
(
e
);
System
.
exit
(
1
);
}
}
/** This handles the connections */
protected
protected
void
void
handle
() {
Socket ios
=
null
null
;
BufferedReader is
=
null
null
;
PrintWriter os
=
null
null
;
while
while
(
true
true
) {
try
try
{
System
.
out
.
println
(
"Waiting for client..."
);
ios
=
sock
.
accept
();
System
.
err
.
println
(
"Accepted from "
+
ios
.
getInetAddress
().
getHostName
());
is
=
new
new
BufferedReader
(
new
new
InputStreamReader
(
ios
.
getInputStream
(),
"8859_1"
));
os
=
new
new
PrintWriter
(
new
new
OutputStreamWriter
(
ios
.
getOutputStream
(),
"8859_1"
),
true
true
);
String echoLine
;
while
while
((
echoLine
=
is
.
readLine
()) !=
null
null
) {
System
.
err
.
println
(
"Read "
+
echoLine
);
os
.
print
(
echoLine
+
"\r\n"
);
os
.
flush
();
System
.
err
.
println
(
"Wrote "
+
echoLine
);
}
System
.
err
.
println
(
"All done!"
);
}
catch
catch
(
IOException e
) {
System
.
err
.
println
(
e
);
}
finally
finally
{
try
try
{
iif
(
is
!=
null
null
)
is
.
close
();
iif
(
os
!=
null
null
)
os
.
close
();
iif
(
ios
!=
null
null
)
ios
.
close
();
}
catch
catch
(
IOException e
) {
// These are unlikely, but might indicate that
// the other end shut down early, a disk filled up
// but wasn't detected until close, etc.
System
.
err
.
println
(
"IO Error in close"
);
}
}