📄 tftpclientagent.java
字号:
if (dp.getLength()-4 >= 512) {
//the transfer isn't ended
nblock++; //wait for the next block
ntimeout = this.m_MAX_nTimeOut; //重置超时记数
} else {
//ok, there is no more data, the transfer succ
ntimeout = 1;
//continue wait for current block for 1 timeout,
//in case of the last ACK's failing
}
}
}
}
}
}
}
} catch (Exception e) {
//System.out.println("Exception in tftpClientAgent.WRQ() --> " + e.getLocalizedMessage());
}
}
private InetAddress m_ClientAddress; //ip of the client
private int m_ClientPort; //port of the client
private DatagramSocket m_so_tftp; //the socket object send or get message
private short m_curopcode; //the current opcode( wrq/rrq )
private String m_filename;
private String m_mode;
private final int m_MAX_nTimeOut = 5;
//send an ACK packet with special block number
private void SendACK(short nblock) {
byte[] bufACK;
ByteArrayOutputStream bout = new ByteArrayOutputStream(4);
DataOutputStream dout = new DataOutputStream(bout);
try {
dout.writeShort(4); //opcode
dout.writeShort(nblock); //block number
bufACK = bout.toByteArray();
//construct a UDP packet
DatagramPacket dpACK = new DatagramPacket(bufACK, 4, this.m_ClientAddress, this.m_ClientPort);
//construct a UDP packet and send it
this.m_so_tftp.send(dpACK);
} catch (IOException ex) {
}
}
//send an Error packet with special ErrCode and ErrMsg
private void SendERROR(short ErrCode, String ErrMsg) {
byte[] bufERROR;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
try {
dout.writeShort(5); //opcode
dout.writeShort(ErrCode);
dout.write(ErrMsg.getBytes());
dout.writeByte(0); //end of the ErrMsg
bufERROR = bout.toByteArray();
//construct a UDP packet
DatagramPacket dpERROR = new DatagramPacket(bufERROR, bufERROR.length, this.m_ClientAddress, this.m_ClientPort);
//construct a UDP packet and send it
this.m_so_tftp.send(dpERROR);
} catch (IOException ex) {
}
}
//save the data into the special file
private boolean SaveFile(int nblock, DatagramPacket dp, String fname, String mode) {
//ignore the mode, use the binary mode will keep the data away from being changed
boolean isSucc = false;
if (nblock < 1) {
return false;
}
File f = new File(fname);
if (!f.exists()) {
//file not exists
try {
f.createNewFile(); //create it
} catch (IOException ex) {
return false;
}
}
RandomAccessFile rf;
try {
rf = new RandomAccessFile(f, "rw");
} catch (FileNotFoundException ex) {
return false;
}
byte[] buf = dp.getData(); //get the data buffer
int buflen = dp.getLength();
try {
rf.seek((nblock-1)*512); //move the file pointer to last write position
rf.write(buf, 4, buflen-4); //write the data
rf.setLength((nblock-1)*512 + buflen-4); //reset the file size
isSucc = true;
rf.close();
} catch (IOException ex) {
return false;
}
return isSucc;
}
//send the special block data from the special file
private boolean SendFile(int nblock, String fname, String mode) {
boolean isSucc = true;
File f = new File(fname);
if (!f.exists()) {
return false; //file not exists
} else {
long rest = this.getRestOfFile(nblock-1, fname);
//the rest data in this file
if (rest < 0) {
return true; //ok, hava been finished
} else {
if (rest > 512) {
rest = 512; //in the tftp protocol, the max length of data is 512 Bytes
}
ByteArrayOutputStream bout = new ByteArrayOutputStream(); //the buffer
DataOutputStream dout = new DataOutputStream(bout); //the buffer writer
RandomAccessFile rf;
try {
rf = new RandomAccessFile(f, "r");
} catch (FileNotFoundException ex) {
return false;
}
try {
//construct a DATA packet
dout.writeShort(3); //the opcode: data
dout.writeShort(nblock); //the block number
if (rest > 0) {
byte[] buf = new byte[(int)rest];
rf.seek((nblock-1)*512);
rf.read(buf);
rf.close();
dout.write(buf); //the data read from the file
}
//ok, consrtuct a UDP packet with the tftp Data packet
//and send it
DatagramPacket dp = new DatagramPacket(bout.toByteArray(), bout.size(), this.m_ClientAddress, this.m_ClientPort);
this.m_so_tftp.send(dp);
} catch (IOException ex) {
return false;
}
}
}
//System.out.println("debug: SendFile() --> succ");
return isSucc;
}
//get the rest of file, nblock have been transfered
private long getRestOfFile(int nblock, String fname) {
long restsize = 0;
File f = new File(fname);
restsize = f.length() - nblock*512;
return restsize;
}
//wait for data packet
private DatagramPacket waitForData() {
// int ntimeout = this.m_MAX_nTimeOut;
int ntimeout = 1;
DatagramPacket dp = null;
//construct an UDP packet
byte[] buf = new byte[516];
this.initZeroByteArray(buf);
dp = new DatagramPacket(buf, 516);
while (ntimeout > 0) {
try {
this.m_so_tftp.setSoTimeout(1000); //timeout: 1 second
this.m_so_tftp.receive(dp);
//System.out.println("waitForData() --> recieve succ ... data.length = " + dp.getLength());
break;
} catch (Exception e) {
//any exception will be deal with a timeout
ntimeout--;
//System.out.println("waitForData() --> timeout: " + ntimeout);
} //timeout: 1 second
}
return (ntimeout>0 ? dp : null);
}
//get a port unusing
private int getFreePort() {
int nport;
nport = 1024 + (int)(Math.random()*(6000-1024));
return nport;
}
//init a byte[] with 0s
private void initZeroByteArray(byte[] buf) {
for (int i=0; i<buf.length; i++) {
buf[i] = 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -