📄 client_put.java
字号:
/**
*协议的Client_Put状态.
*/
import java.io.IOException;
import java.util.Timer;
public class Client_Put extends State {
public static FileIOClass myfile;//将要发送的文件名
public static ComClass Com; //用于传输
Timer timer = new Timer(false);
static String addr, file;
State doIt() {
SimpleIO input = new SimpleIO(System.in);
System.out.println("请输入文件名:\n");
file = input.readString();
myfile = new FileIOClass();
byte filemode = myfile.openFile(file); //打开文件
if(filemode==FileIOClass.open_failed) { //打开不成功则返回Client_Put状态
this.next_state = new Client_Put(); ///???
return this.next_state;
}
System.out.println("请输入服务器IP:\n");///???这儿有判断就好了!
addr = input.readString();
Com = new ComClass(5555);
Com.writeOtherSiteAddress(addr); //记下服务器IP
RWDelayClass RWdelay = new RWDelayClass(file);//发送WRQ的任务线程
RWdelay.start();
byte[] RWack = Com.receivePacket();
while(RWack[1]!=PacketCode.ACK ||RWack[3]!=(byte)0) {
System.out.println("收到了错误的响应WRQ的包,继续在这里阻塞!");
RWack = Com.receivePacket();
}
RWdelay.task.cancel(); //取消任务
timer.purge(); //移除任务
boolean endsLoop = false;
byte num = 1;
while( endsLoop == false) {
byte [] filedata = myfile.readFromFile();
//发送DATA包的任务线程
DataDelayClass Datadelay = new DataDelayClass(filedata,num);
Datadelay.start();
byte[] Dataack = Com.receivePacket() ;
//如果不是DATA包或者对方相应的序号错
while(Dataack[1]!=PacketCode.ACK ||Dataack[3]!=num) {
System.out.println("收到了错误的ACK的包,保持等待!");
Dataack = Com.receivePacket() ;
}
Datadelay.task.cancel();
timer.purge();
num +=1 ; //下一个 block#序号
if(num == 126) { //1 -- 125
num = 1;
}
if(filedata[511]==1) { //如果传文件结束的话,结束整个发送循环
endsLoop = true ;
System.out.println("文件传送结束.") ;
}
}
Com.closeSocket();
this.next_state = new Idle();
return this.next_state;
}
//发送数据包的线程
public class DataDelayClass extends java.lang.Thread {
byte[] filedata ;
byte num ;
DataResendClass task ;
public DataDelayClass(byte[] filedata,byte num) {
this.filedata = filedata ;
this.num = num ;
task = new DataResendClass(filedata, num);
}
public void run() {
timer.schedule(task,0 ,2000);
}
}
//超时重发数据包的线程
public class DataResendClass extends java.util.TimerTask {
byte[] filedata ;
byte num ;
final long time = System.currentTimeMillis();//date.getTime();
public DataResendClass(byte[] filedata,byte num) {
this.filedata = filedata ;
this.num = num ;
}
public void run() {
Client_Put.Com.sendPacketDATA(filedata,(byte)0,num); //发送数据报
if (System.currentTimeMillis() - time >= 10000) {
this.cancel();
timer.purge();
System.out.println("等待超时!");
// System.exit(0);
}
}
}
//发送写请求包的线程
public class RWDelayClass extends java.lang.Thread {
String filename ;
byte num ;
RWResendClass task ;
public RWDelayClass(String filename) {
this.filename = filename ;
task = new RWResendClass(filename);
}
public void run() {
timer.schedule(task,0 ,2000);
}
}
//超时重发写请求包的线程
public class RWResendClass extends java.util.TimerTask {
String filename ;
final long time = System.currentTimeMillis();//date.getTime();
public RWResendClass(String filename ) {
this.filename = filename ;
}
public void run() {
Client_Put.Com.sendPacketRW(filename,(byte)0,PacketCode.WRQ); //发送数据报
if (System.currentTimeMillis() - time >= 10000) {
this.cancel();
timer.purge();
System.out.println("等待超时!");
System.exit(0);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -