Java Reference
In-Depth Information
buffer
=
new
new
byte
byte
[
PACKET_SIZE
];
outp
=
new
new
DatagramPacket
(
buffer
,
PACKET_SIZE
,
servAddr
,
TFTP_PORT
);
inp
=
new
new
DatagramPacket
(
buffer
,
PACKET_SIZE
);
}
/* Build a TFTP Read Request packet. This is messy because the
* fields have variable length. Numbers must be in
* network order, too; fortunately Java just seems
* naturally smart enough :-) to use network byte order.
*/
void
void
readFile
(
String path
)
throws
throws
IOException
{
buffer
[
0
] =
0
;
buffer
[
OFFSET_REQUEST
] =
OP_RRQ
;
// read request
int
int
p
=
2
;
// number of chars into buffer
// Convert filename String to bytes in buffer , using "p" as an
// offset indicator to get all the bits of this request
// in exactly the right spot.
byte
byte
[]
bTemp
=
path
.
getBytes
();
// i.e., ASCII
System
.
arraycopy
(
bTemp
,
0
,
buffer
,
p
,
path
.
length
());
p
+=
path
.
length
();
buffer
[
p
++] =
0
;
// null byte terminates string
// Similarly, convert MODE ("stream" or "octet") to bytes in buffer
bTemp
=
MODE
.
getBytes
();
// i.e., ASCII
System
.
arraycopy
(
bTemp
,
0
,
buffer
,
p
,
MODE
.
length
());
p
+=
MODE
.
length
();
buffer
[
p
++] =
0
;
// null terminate
/* Send Read Request to tftp server */
outp
.
setLength
(
p
);
sock
.
send
(
outp
);
/* Loop reading data packets from the server until a short
* packet arrives; this indicates the end of the file.
*/
int
int
len
=
0
;
ddo
{
sock
.
receive
(
inp
);
iif
(
debug
)
System
.
err
.
println
(
"Packet # "
+
Byte
.
toString
(
buffer
[
OFFSET_PACKETNUM
])+
"RESPONSE CODE "
+
Byte
.
toString
(
buffer
[
OFFSET_REQUEST
]));
iif
(
buffer
[
OFFSET_REQUEST
] ==
OP_ERROR
) {
System
.
err
.
println
(
"rcat ERROR: "
+
new
new
String
(
buffer
,
4
,
inp
.
getLength
()-
4
));