模拟停止等待协议内容解说
模拟停止等待协议内容解说
packageweb;
importjava.awt.Color;
importjava.awt.FileDialog;
importjava.awt.TextArea;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
//importjavax.swing.JList;
importjavax.swing.JPanel;
importjavax.swing.JLabel;
importsun.security.krb5.internal.s;
importjava.awt.List;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.Reader;
importjava.net.*;
importjava.util.Random;
publicclassStop_and_WaitextendsJFrame{
privateJPaneljPanel;
privateTextAreasentTextArea;
privateJButtonchooseFileButton,sentButton,cleanButton;
privateJLabelsentFileLabel,sentMessageLabel,sentStatusLabel,
receiveStatusLabel,receiveFileLabel;
privateListsentStatusList,sentAllFileList,receiveStatusList,
receiveFileList;
privatebooleanisTout=false,isACK=true,sentNum=true,
receiveNum=false,changeTemp=true,lastIsACK=true;
privateintreSentNum=0,lineNum=-1;
publicStop_and_Wait(){
init();
listener();
}
publicvoidinit(){
jPanel=newJPanel();
jPanel.setBackground(Color.PINK);
this.setContentPane(jPanel);
sentFileLabel=newJLabel("SentFile:");
sentFileLabel.setBounds(20,20,100,30);
jPanel.add(sentFileLabel);
sentMessageLabel=newJLabel("AllSentFileMessage:");
sentMessageLabel.setBounds(20,260,150,30);
jPanel.add(sentMessageLabel);
sentTextArea=newTextArea();
sentTextArea.setBounds(20,50,300,200);
jPanel.add(sentTextArea);
sentAllFileList=newList();
sentAllFileList.setBounds(20,290,300,200);
jPanel.add(sentAllFileList);
sentStatusList=newList();
sentStatusList.setBounds(360,50,300,200);
jPanel.add(sentStatusList);
receiveFileList=newList();
receiveFileList.setBounds(690,50,180,440);
jPanel.add(receiveFileList);
sentStatusLabel=newJLabel("SentStatusMessage:");
sentStatusLabel.setBounds(360,20,150,30);
jPanel.add(sentStatusLabel);
receiveFileLabel=newJLabel("ReceiveFile:");
receiveFileLabel.setBounds(690,20,100,30);
jPanel.add(receiveFileLabel);
receiveStatusLabel=newJLabel("ReceiveStatusMessage:");
receiveStatusLabel.setBounds(360,260,150,30);
jPanel.add(receiveStatusLabel);
receiveStatusList=newList();
receiveStatusList.setBounds(360,290,300,200);
jPanel.add(receiveStatusList);
chooseFileButton=newJButton("ChooseFile");
chooseFileButton.setBounds(100,500,110,40);
jPanel.add(chooseFileButton);
chooseFileButton.setToolTipText("选择要发送的文件");
sentButton=newJButton("SentData");
sentButton.setBounds(400,500,110,40);
jPanel.add(sentButton);
sentButton.setToolTipText("发送文件");
cleanButton=newJButton("Clean");
cleanButton.setBounds(700,500,110,40);
jPanel.add(cleanButton);
cleanButton.setToolTipText("清除信息");
setTitle("StopandWaitProtocolSimulator");
setBounds(80,60,900,600);
this.setLayout(null);
this.setBackground(newColor(2,3,5));
this.setResizable(false);
setVisible(true);
}
publicbooleanisTout(){
Randomrandom=newRandom();
//System.out.println(random.nextInt(10));
if(random.nextInt(10)>8){
returntrue;
}else{
returnfalse;
}
//importjava.lang.Math;
//System.out.println(Math.random()*10);
}
publicbooleanisCRC_Right(){
Randomrandom=newRandom();
//System.out.println(random.nextInt(10));
if(random.nextInt(10)>2){
//isACK=true;
returntrue;
}else{
//isACK=false;
returnfalse;
}
}
publicvoidsetSentNum(){
//if(sentNum=false)
//sentNum=true;
//else
//sentNum=false;
sentNum=!sentNum;
}
publicvoidsetReceiveNum(){
//if(receiveNum=false)
//receiveNum=true;
//else
//receiveNum=false;
receiveNum=!receiveNum;
}
publicvoidlistener(){
//addWindowListener
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
//System.exit(intstatus);零参数正常终止JVM;非零参数非正常终止(提前)JVM
}
});
//
chooseFileButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
FileDialogfileDialog=newFileDialog(newJFrame(),"请选择传输文件");
fileDialog.setVisible(true);
Filefile=newFile(fileDialog.getDirectory()
+fileDialog.getFile());
/*
*解析:BufferedReader有readLine()方法;男人就是人(子类对象当着父类对象用)
importjava.awt.Color;
importjava.awt.FileDialog;
importjava.awt.TextArea;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JButton;
importjavax.swing.JFrame;
//importjavax.swing.JList;
importjavax.swing.JPanel;
importjavax.swing.JLabel;
importsun.security.krb5.internal.s;
importjava.awt.List;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.Reader;
importjava.net.*;
importjava.util.Random;
publicclassStop_and_WaitextendsJFrame{
privateJPaneljPanel;
privateTextAreasentTextArea;
privateJButtonchooseFileButton,sentButton,cleanButton;
privateJLabelsentFileLabel,sentMessageLabel,sentStatusLabel,
receiveStatusLabel,receiveFileLabel;
privateListsentStatusList,sentAllFileList,receiveStatusList,
receiveFileList;
privatebooleanisTout=false,isACK=true,sentNum=true,
receiveNum=false,changeTemp=true,lastIsACK=true;
privateintreSentNum=0,lineNum=-1;
publicStop_and_Wait(){
init();
listener();
}
publicvoidinit(){
jPanel=newJPanel();
jPanel.setBackground(Color.PINK);
this.setContentPane(jPanel);
sentFileLabel=newJLabel("SentFile:");
sentFileLabel.setBounds(20,20,100,30);
jPanel.add(sentFileLabel);
sentMessageLabel=newJLabel("AllSentFileMessage:");
sentMessageLabel.setBounds(20,260,150,30);
jPanel.add(sentMessageLabel);
sentTextArea=newTextArea();
sentTextArea.setBounds(20,50,300,200);
jPanel.add(sentTextArea);
sentAllFileList=newList();
sentAllFileList.setBounds(20,290,300,200);
jPanel.add(sentAllFileList);
sentStatusList=newList();
sentStatusList.setBounds(360,50,300,200);
jPanel.add(sentStatusList);
receiveFileList=newList();
receiveFileList.setBounds(690,50,180,440);
jPanel.add(receiveFileList);
sentStatusLabel=newJLabel("SentStatusMessage:");
sentStatusLabel.setBounds(360,20,150,30);
jPanel.add(sentStatusLabel);
receiveFileLabel=newJLabel("ReceiveFile:");
receiveFileLabel.setBounds(690,20,100,30);
jPanel.add(receiveFileLabel);
receiveStatusLabel=newJLabel("ReceiveStatusMessage:");
receiveStatusLabel.setBounds(360,260,150,30);
jPanel.add(receiveStatusLabel);
receiveStatusList=newList();
receiveStatusList.setBounds(360,290,300,200);
jPanel.add(receiveStatusList);
chooseFileButton=newJButton("ChooseFile");
chooseFileButton.setBounds(100,500,110,40);
jPanel.add(chooseFileButton);
chooseFileButton.setToolTipText("选择要发送的文件");
sentButton=newJButton("SentData");
sentButton.setBounds(400,500,110,40);
jPanel.add(sentButton);
sentButton.setToolTipText("发送文件");
cleanButton=newJButton("Clean");
cleanButton.setBounds(700,500,110,40);
jPanel.add(cleanButton);
cleanButton.setToolTipText("清除信息");
setTitle("StopandWaitProtocolSimulator");
setBounds(80,60,900,600);
this.setLayout(null);
this.setBackground(newColor(2,3,5));
this.setResizable(false);
setVisible(true);
}
publicbooleanisTout(){
Randomrandom=newRandom();
//System.out.println(random.nextInt(10));
if(random.nextInt(10)>8){
returntrue;
}else{
returnfalse;
}
//importjava.lang.Math;
//System.out.println(Math.random()*10);
}
publicbooleanisCRC_Right(){
Randomrandom=newRandom();
//System.out.println(random.nextInt(10));
if(random.nextInt(10)>2){
//isACK=true;
returntrue;
}else{
//isACK=false;
returnfalse;
}
}
publicvoidsetSentNum(){
//if(sentNum=false)
//sentNum=true;
//else
//sentNum=false;
sentNum=!sentNum;
}
publicvoidsetReceiveNum(){
//if(receiveNum=false)
//receiveNum=true;
//else
//receiveNum=false;
receiveNum=!receiveNum;
}
publicvoidlistener(){
//addWindowListener
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
//System.exit(intstatus);零参数正常终止JVM;非零参数非正常终止(提前)JVM
}
});
//
chooseFileButton.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
FileDialogfileDialog=newFileDialog(newJFrame(),"请选择传输文件");
fileDialog.setVisible(true);
Filefile=newFile(fileDialog.getDirectory()
+fileDialog.getFile());
/*
*解析:BufferedReader有readLine()方法;男人就是人(子类对象当着父类对象用)