Java Reference
In-Depth Information
Sending implementation
Receiving implementation
Receiving program
SendQ
RecvQ
Delivered
send(
)
TCP protoco
l
recv(
)
3
2
2
1
6500 bytes
1500 bytes
0 bytes
1
2
3
First write (1000 bytes)
Second write (2000 bytes)
Third write (5000 bytes)
Figure 5.2:
State of the three queues after three writes.
We can think of the sequence of all bytes sent (in one direction) on a TCP connection up
to a particular instant in time as being divided into three FIFO queues:
1.
SendQ
: Bytes buffered in the underlying implementation at the sender that have been
written to the output stream but not yet successfully transmitted to the receiving host.
2.
RecvQ
: Bytes buffered in the underlying implementation at the receiver waiting to be
delivered to the receiving program—that is, read from the input stream.
3.
Delivered
: Bytes already read from the input stream by the receiver.
A call to
out.write()
at the sender appends bytes to
SendQ
. The TCP protocol is responsible
for moving bytes—in order—from
SendQ
to
RecvQ
. It is important to realize that this transfer
cannot be controlled or directly observed by the user program, and that it occurs in chunks
whose sizes are more or less independent of the size of the buffers passed in
write()
s. Bytes
are moved from
RecvQ
to
Delivered
as they are read from the
Socket
's
InputStream
by the
receiving program; the size of the transferred chunks depends on the amount of data in
RecvQ
and the size of the buffer given to
read()
.
Figure 5.2 shows one possible state of the three queues
after
the three
out.write()
sin
the example above, but
before
any
in.reads()
s at the other end. The different shading patterns
denote bytes passed in the three different invocations of
write()
shown above.
Now suppose the receiver calls
read()
with a byte array of size 2000. The
read()
call
will move all of the 1500 bytes present in the waiting-for-delivery (
RecvQ
) queue into the byte
array and return the value 1500. Note that this data includes bytes passed in both the first and
second calls to
write()
. At some time later, after TCP has completed transfer of more data, the
three partitions might be in the state shown in Figure 5.3.
If the receiver now calls
read()
with a buffer of size 4000, that many bytes will be moved
from the waiting-for-delivery (
RecvQ
) queue to the already-delivered (
Delivered
) queue; this
includes the remaining 1500 bytes from the second
write()
, plus the first 2500 bytes from the
third
write()
. The resulting state of the queues is shown in Figure 5.4.
The number of bytes returned by the next call to
read()
depends on the size of the
buffer and the timing of the transfer of data over the network from the send-side socket/TCP
Search WWH ::
Custom Search