当前位置:文档之家› 选择性重传滑动窗口协议

选择性重传滑动窗口协议

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import yout.*;

public class Frame5 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
Label label1 = new Label();
Label label2 = new Label();
Label label3 = new Label();
Label label4 = new Label();
Label label5 = new Label();
Label label6 = new Label();
Label label7 = new Label();
Label label8 = new Label();
Label label9 = new Label();
Label label10 = new Label();
TextField textField1 = new TextField();
TextField textField2 = new TextField();
TextField textField3 = new TextField();
TextField textField4 = new TextField();
TextField textField5 = new TextField();
TextField textField6 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Button button4 = new Button();
Button button5 = new Button();

String m1,buffer1="",m4,m3,m2,m21;
int c1=0,c12;
long z4;


//Construct the frame
public Frame5() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
label1.setText("发送窗口大小:8");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("选择性重传---滑动窗口协议");
label2.setText("接收窗口大小:4");
label3.setText("发送窗口 ");
label4.setText("接收窗口");
label5.setText("收到应答");
label6.setText("正常发送:");
label7.setText("发送出错");
label8.setText("传送状态:");
label9.setText("发送缓冲区");
label10.setText("接收缓冲区");
textField1.setColumns(8);
textField1.setText("");
textField2.setColumns(8);
textField2.setText("");
textField3.setColumns(8);
textField3.setText("");
textField4.setColumns(4);
textField4.setText("");
textField5.setColumns(6);
textField5.setText("");
textField6.setColumns(18);
textField6.setText("");
button1.setFont(new java.awt.Font("Dialog", 0, 12));
button1.setLabel("发送");
button1.addActionListener(new Frame5_button1_actionAdapter(this));
button2.setFont(new java.awt.Font("Dialog", 0, 12));
button2.setLabel("重发");
button2.addActionListener(new Frame5_button2_actionAdapter(this));
button3.setFont(new java.awt.Font("Dialog", 0, 12));
button3.setLabel("发送");
button3.addActionListener(new Frame5_button3_actionAdapter(this));
button4.setBackground(new Color(159, 208, 76));
button4.setFont(new java.awt.Font("Dialog", 0, 13));
button4.setLabel("接收");
button4.addActionListener(new Frame5_button4_actionAdapter(this));
button5.setBackground(new Color(138, 221,

255));
button5.setFont(new java.awt.Font("Dialog", 0, 13));
button5.setLabel("重置");
button5.addActionListener(new Frame5_button5_actionAdapter(this));
contentPane.add(label1, new XYConstraints(43, 6, -1, -1));
contentPane.add(label2, new XYConstraints(257, 6, -1, -1));
contentPane.add(label6, new XYConstraints(232, 133, -1, -1));
contentPane.add(label7, new XYConstraints(330, 133, -1, -1));
contentPane.add(textField2, new XYConstraints(277, 26, -1, -1));
contentPane.add(label10, new XYConstraints(202, 34, -1, -1));
contentPane.add(textField6, new XYConstraints(7, 228, -1, -1));
contentPane.add(label8, new XYConstraints(16, 200, -1, -1));
contentPane.add(button1, new XYConstraints(228, 153, -1, -1));
contentPane.add(button2, new XYConstraints(226, 187, -1, -1));
contentPane.add(button3, new XYConstraints(324, 153, -1, -1));
contentPane.add(button5, new XYConstraints(225, 226, -1, -1));
contentPane.add(button4, new XYConstraints(323, 225, -1, -1));
contentPane.add(label5, new XYConstraints(7, 125, -1, -1));
contentPane.add(label3, new XYConstraints(9, 82, -1, -1));
contentPane.add(label4, new XYConstraints(217, 78, -1, -1));
contentPane.add(textField4, new XYConstraints(282, 76, -1, -1));
contentPane.add(label9, new XYConstraints(4, 30, -1, -1));
contentPane.add(textField1, new XYConstraints(80, 28, -1, -1));
contentPane.add(textField3, new XYConstraints(81, 75, -1, -1));
contentPane.add(textField5, new XYConstraints(80, 119, -1, -1));
}

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void button5_actionPerformed(ActionEvent e) {
textField1.setText("01234567");
textField2.setText("");
textField3.setText("01234567");
textField4.setText("0123");
textField5.setText("");
textField6.setText("");
buffer1="";
c1=0;
button1.setEnabled(true);
button2.setEnabled(false);
button3.setEnabled(true);
button4.setEnabled(false);
}

void button1_actionPerformed(ActionEvent e) {
m1=textField1.getText();
buffer1+=m1.valueOf(m1.charAt(0));
textField2.setText(buffer1);
textField1.setText(m1.substring(1));
c1++;
c1=c1%8;
if(c1%4==0){
textField6.setText("缓冲区满,开始接收");
button4.setEnabled(true);
button1.setEnabled(false);
}

}

void button4_actionPerformed(ActionEvent e) {
button4.setEnabled(false);
button1.setEnabled(true);
String m41,m42,l1,l2,l3,l4;
m4=textField3.getText();
z4=Integer.parseInt(m4);
if(z4==1234567){
z4=45670123;
m41=String.valueOf(z4);
textField3.setText(m41);
m42=textField3.getText();
}


else{
z4=1234567;
m41=String.valueOf(z4);
textField3.setText("0"+m41);
m42=textField3.getText();
}
l1=m42.valueOf(m42.charAt(0));
l2=m42.valueOf(m42.charAt(1));
l3=m42.valueOf(m42.charAt(2));
l4=m42.valueOf(m42.charAt(3));
textField4.setText(l1+l2+l3+l4);
textField5.setText("ack"+m42.valueOf(m42.charAt(7)));
textField6.setText("开始发送");
}

void button3_actionPerformed(ActionEvent e) {
String m31;
m3=textField1.getText();
m31=m3.valueOf(m3.charAt(0));
textField1.setText(m3.substring(1)+m31);
button2.setEnabled(true);
textField5.setText("nak"+c1);
textField6.setText("第"+c1+"桢出错,以后缓存");
c1++;
c1=c1%8;
if(c1%4==0){
button3.setEnabled(false);

}

}

void button2_actionPerformed(ActionEvent e) {
String m21;
button2.setEnabled(false);
m2=textField1.getText();
m21=textField2.getText();
textField2.setText(m21+m2);
textField3.setText(m2);
textField4.setText(m2);
textField5.setText("ack"+m2);
textField6.setText("重发完毕");
textField1.setText("");

}

}

class Frame5_button5_actionAdapter implements java.awt.event.ActionListener {
Frame5 adaptee;

Frame5_button5_actionAdapter(Frame5 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button5_actionPerformed(e);
}
}

class Frame5_button1_actionAdapter implements java.awt.event.ActionListener {
Frame5 adaptee;

Frame5_button1_actionAdapter(Frame5 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button1_actionPerformed(e);
}
}

class Frame5_button4_actionAdapter implements java.awt.event.ActionListener {
Frame5 adaptee;

Frame5_button4_actionAdapter(Frame5 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button4_actionPerformed(e);
}
}

class Frame5_button3_actionAdapter implements java.awt.event.ActionListener {
Frame5 adaptee;

Frame5_button3_actionAdapter(Frame5 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button3_actionPerformed(e);
}
}

class Frame5_button2_actionAdapter implements java.awt.event.ActionListener {
Frame5 adaptee;

Frame5_button2_actionAdapter(Frame5 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.button2_actionPerformed(e);
}
}

相关主题