package web; import java.awt.Color; import java.awt.FileDialog; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; // import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JLabel; import sun.security.krb5.internal.s; import java.awt.List; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net. * ; import java.util.Random; public class Stop_and_Wait extends JFrame ... { private JPanel jPanel; private TextArea sentTextArea; private JButton chooseFileButton, sentButton, cleanButton; private JLabel sentFileLabel, sentMessageLabel, sentStatusLabel, receiveStatusLabel, receiveFileLabel; private List sentStatusList, sentAllFileList, receiveStatusList, receiveFileList; private boolean isTout = false, isACK = true, sentNum = true, receiveNum = false, changeTemp = true, lastIsACK = true; private int reSentNum = 0, lineNum = -1; public Stop_and_Wait() ...{ init(); listener(); } public void init() ...{ jPanel = new JPanel(); jPanel.setBackground(Color.PINK); this.setContentPane(jPanel); sentFileLabel = new JLabel("SentFile:"); sentFileLabel.setBounds(20, 20, 100, 30); jPanel.add(sentFileLabel); sentMessageLabel = new JLabel("AllSentFileMessage:"); sentMessageLabel.setBounds(20, 260, 150, 30); jPanel.add(sentMessageLabel); sentTextArea = new TextArea(); sentTextArea.setBounds(20, 50, 300, 200); jPanel.add(sentTextArea); sentAllFileList = new List(); sentAllFileList.setBounds(20, 290, 300, 200); jPanel.add(sentAllFileList); sentStatusList = new List(); sentStatusList.setBounds(360, 50, 300, 200); jPanel.add(sentStatusList); receiveFileList = new List(); receiveFileList.setBounds(690, 50, 180, 440); jPanel.add(receiveFileList); sentStatusLabel = new JLabel("SentStatusMessage:"); sentStatusLabel.setBounds(360, 20, 150, 30); jPanel.add(sentStatusLabel); receiveFileLabel = new JLabel("ReceiveFile:"); receiveFileLabel.setBounds(690, 20, 100, 30); jPanel.add(receiveFileLabel); receiveStatusLabel = new JLabel("ReceiveStatusMessage:"); receiveStatusLabel.setBounds(360, 260, 150, 30); jPanel.add(receiveStatusLabel); receiveStatusList = new List(); receiveStatusList.setBounds(360, 290, 300, 200); jPanel.add(receiveStatusList); chooseFileButton = new JButton("ChooseFile"); chooseFileButton.setBounds(100, 500, 110, 40); jPanel.add(chooseFileButton); chooseFileButton.setToolTipText("选择要发送的文件"); sentButton = new JButton("SentData"); sentButton.setBounds(400, 500, 110, 40); jPanel.add(sentButton); sentButton.setToolTipText("发送文件"); cleanButton = new JButton("Clean"); cleanButton.setBounds(700, 500, 110, 40); jPanel.add(cleanButton); cleanButton.setToolTipText("清除信息"); setTitle("Stop and Wait Protocol Simulator"); setBounds(80, 60, 900, 600); this.setLayout(null); this.setBackground(new Color(2, 3, 5)); this.setResizable(false); setVisible(true); } public boolean isTout() ...{ Random random = new Random(); // System.out.println(random.nextInt(10)); if (random.nextInt(10) > 8) ...{ return true; } else ...{ return false; } // import java.lang.Math; // System.out.println(Math.random()*10); } public boolean isCRC_Right() ...{ Random random = new Random(); // System.out.println(random.nextInt(10)); if (random.nextInt(10) > 2) ...{ // isACK=true; return true; } else ...{ // isACK=false; return false; } } public void setSentNum() ...{ // if(sentNum=false) // sentNum=true; // else // sentNum=false; sentNum = !sentNum; } public void setReceiveNum() ...{ // if(receiveNum=false) // receiveNum=true; // else // receiveNum=false; receiveNum = !receiveNum; } public void listener() ...{ // addWindowListener addWindowListener(new WindowAdapter() ...{ public void windowClosing(WindowEvent e) ...{ System.exit(0); // System.exit(int status); 零参数正常终止JVM ; 非零参数 非正常终止(提前)JVM } }); // chooseFileButton.addActionListener(new ActionListener() ...{ public void actionPerformed(ActionEvent e) ...{ FileDialog fileDialog = new FileDialog(new JFrame(), "请选择传输文件"); fileDialog.setVisible(true); File file = new File(fileDialog.getDirectory() + fileDialog.getFile()); /**//* * 解析: BufferedReader 有 readLine()方法; 男人就是人 (子类对象 当着 父类对象用) * BufferedReader 类的构造方法 BufferedReader(Reader in) 需要一个Reader 对象 * Reader 实现子类有 BufferedReader, FilterReader, InputStreamReader, * 即可以通过 FilterReader,InputStreamReader,来构造 BufferedReader对象; * * FileReader(File file) && InputStreamReader(InputStream in) * InputStream 实现子类 FileInputStream FileInputStream(File file) * File(String pathname) new FileReader(file) && new * InputStreamReader(new FileInputStream(file) * * */ try ...{ BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new FileInputStream(file))); // BufferedReader bufferedReader =new BufferedReader(new // FileReader(file)); sentTextArea.setText(null); String string; while ((string = bufferedReader.readLine()) != null) ...{ // sentTextArea.setText(sentTextArea.getText()+" "+string); sentTextArea.append(string + " "); // receiveList.add(string); // showReceive(); } bufferedReader.close(); } catch (FileNotFoundException e1) ...{ e1.printStackTrace(); } catch (IOException e2) ...{ e2.printStackTrace(); } } }); sentButton.addActionListener(new ActionListener() ...{ public void actionPerformed(ActionEvent e) ...{ // Random random=new Random(); // System.out.println(random.nextInt(10)); while (true) ...{ if (changeTemp == true) ...{ sent(); } else ...{ receive(); } } } }); sentTextArea.addKeyListener(new KeyAdapter() ...{ public void keyPressed(KeyEvent e) ...{ if (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_ENTER) ...{ while (true) ...{ if (changeTemp == true) ...{ sent(); } else ...{ receive(); } } } } }); cleanButton.addActionListener(new ActionListener() ...{ public void actionPerformed(ActionEvent e) ...{ // new Thread().currentThread().stop(); // sentTextArea.setText(null); reSentInit(); } }); } public void reSentInit() ...{ new Stop_and_Wait(); this.dispose(); // lineNum=0; // reSentNum=0; // isTout=false; // isACK=true; // sentNum=true; // receiveNum=false; // changeTemp=true; // lastIsACK=true; // // sentAllFileList.clear(); // receiveFileList.clear(); // receiveStatusList.clear(); // sentStatusList.clear(); // System.gc(); // System.gc(); }; public void sent() ...{ changeTemp = !changeTemp; if (isTout == false) // 如果未超时 ...{ if (isACK == true) // ACK ...{ lastIsACK = true; int tip = lineNum + 1; sentStatusList.add(tip + "ACK,发送当前帧", 0); // 发送当前帧 showSentFile(); setSentNum(); // 设置发送序号 } else if (isACK == false)// NCK ...{ lastIsACK = false; sentStatusList.add(lineNum + "NCK,重发当前帧", 0); // 重发当前帧 showResentFile(); setSentNum(); // 设置发送序号(即使senNum回到前一个状态) reSentNum++; } } else if (isTout == true)// 超时 ...{ if (reSentNum <= 20)// 未超过发送次数 ...{ if (lastIsACK == true) ...{ // 发送下一帧 sentStatusList.add(lineNum + "超时,未超过发送次数,lastACK,发送下一帧", 0); showSentFile(); reSentNum++; setSentNum(); // 设置发送序号 } else if (lastIsACK == false) ...{ // 重发当前帧 sentStatusList.add(lineNum + "超时,未超过发送次数,lastNCK,重发当前帧", 0); showResentFile(); reSentNum++; } // setSentNum(); //设置发送序号(即使senNum回到前一个状态) } else if (reSentNum > 20)// 超过发送次数 ...{ sentStatusList.add(lineNum + "超时,超过发送次数,停止发送", 0); // 停止发送 // System.exit(0); // Thread.yield(); /**//* * try { Thread.sleep(10); } catch (InterruptedException e) { * e.printStackTrace(); } */ new Thread().currentThread().stop(); } } } public void receive() ...{ changeTemp = !changeTemp; isACK = isCRC_Right();// 判断帧是否正确 boolean sign = sentNum; if (isACK == true)// 当前帧正确 ...{ if (receiveNum == sign)// 发送序号正确 ...{ receiveStatusList.add(lineNum + "当前帧正确,发送序号正确,接受当前帧", 0); // 接受当前帧 showReceiveFile(); isACK = true; setReceiveNum(); } else if (receiveNum != sign)// 发送序号错误(重复帧) ...{ receiveStatusList.add(lineNum + "当前帧正确,发送序号错误(重复帧),丢弃当前帧,请求发送下一正帧", 0); // 丢弃当前帧 isACK = true; setReceiveNum(); } } else if (isACK == false)// 当前帧错误 ...{ receiveStatusList.add(lineNum + "当前帧错误,丢弃当前帧,请求重发", 0); // 丢弃当前帧 isACK = false; setReceiveNum(); } isTout = isTout(); // 判断是否超时 } public void showSentFile() ...{ lineNum++; String string = sentTextArea.getText(); // string=string.substring((lineNum-1)*30, lineNum*30); string = string.substring(lineNum * 1, (lineNum + 1) * 1); sentAllFileList.add(string); // lineNum++; } public void showReceiveFile() ...{ // lineNum--; String string2 = sentTextArea.getText(); // string=string.substring((lineNum-2)*30, (lineNum-1)*30); // string2=string2.substring((lineNum-1)*1,lineNum*1); string2 = string2.substring(lineNum * 1, (lineNum + 1) * 1); receiveFileList.add(string2); } public void showResentFile() ...{ // lineNum--; String string3 = sentTextArea.getText(); // string=string.substring((lineNum-1)*30, lineNum*30); string3 = string3.substring(lineNum * 1, (lineNum + 1) * 1); sentAllFileList.add(string3); // lineNum++; } public static void main(String[] args) ...{ Stop_and_Wait stop_and_Wait = new Stop_and_Wait(); }}