Java Reference
In-Depth Information
to improve performance and to obtain access to
BufferedReader
's
String
readLine()
method,whichconvenientlyletsyoureadastringofcharacterstermin-
ated by any one of a line feed (
'\n'
), a carriage return (
'\r'
), or a carriage return
followed immediately by a linefeed.
Thethirdlineusesthe
PrintWriter(OutputStream out)
constructortocre-
atea
PrintWriter
instanceforwritingastringofcharacterstotheoutputstream,and
convertingthesecharacterstoastreamofbytesviaaninternallycreatedoutputstream
writer instance set to the default character encoding.
When you call this constructor, it doesn't automatically flush bytes to the output
streamwhenyouinvokea
println()
method.Toensurethatbytesareoutput,you'll
needtoinvokethe
flush()
methodafter
println()
.However,youcanensurethat
flushingtakesplacebyusingthe
PrintWriter(OutputStream out, boolean
autoFlush)
constructor and passing
true
to
autoFlush
.
ServerSocket
isusedtocreatetheserverendofaTCPconnection.Aserversock-
etwaitsforrequeststocomeinoverthenetwork.Itperformssomeoperationbasedon
that request, and then possibly returns a result to the requester.
While the server socket is processing a request, additional requests might arrive.
These requests are stored in a queue for subsequent processing.
ServerSocket
declares four constructors:
•
ServerSocket()
createsanunboundserversocket.Youcanbindthissocket
toaspecificsocketaddress(towhichclientsocketscommunicate)byinvoking
either of
ServerSocket
'stwo
bind()
methods.
Binding
makes the server
socket address available to a client socket so that a client process can com-
municatewiththeserverprocessviatheclientsocket.Thisconstructorthrows
IOException
when an I/O error occurs while attempting to open the socket.
•
ServerSocket(int port)
createsaserversocketboundtothespecified
port
value and an IP address associated with one of the host's NICs. When
youpass
0
to
port
,anarbitraryportnumberischosen.Theportnumbercan
beretrievedbycalling
int getLocalPort()
.Themaximumqueuelength
for incoming
connection indications
(connection requests from clients) is set
to50.Ifaconnectionindicationarriveswhenthequeueisfull,theconnection
is refused. This constructor throws
IOException
when an I/O error occurs
while attempting to open the socket, and
IllegalArgumentException
when
port
'svalueliesoutsidethespecifiedrangeofvalidportvalues,which
is between 0 and 65535, inclusive.