首页 > 代码库 > java利用socket通信模拟实现ARQ停止等待协议

java利用socket通信模拟实现ARQ停止等待协议

---恢复内容开始---

  1//服务端,发送方
  1 //客户端,接收方
  2 
  3 import java.awt.*;
  4 import java.io.*;
  5 import java.net.*;
  6 import java.util.*;
  7 import javax.swing.*;
  8 
  9 public class IClient extends JFrame  {
 10     JPanel jp = new JPanel();
 11     JTextArea jta = new JTextArea();
 12     JScrollPane jsp = null;
 13 
 14     public IClient()
 15     {
 16         Toolkit t = Toolkit.getDefaultToolkit();
 17         Dimension Size =t.getScreenSize();
 18         int width =  Size.width; 
 19         int height = Size.height;
 20         setLocationByPlatform(true);
 21         this.setAlwaysOnTop(true);
 22         jsp = new JScrollPane(jta);
 23         this.setTitle("接收方");
 24         this.add(jp);
 25         this.setSize(500, 400);
 26         this.setBounds((width - 500) / 2,
 27                 (height - 400) / 2, 600,500);
 28         this.add(jsp, BorderLayout.CENTER);
 29         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 30         this.setVisible(true);
 31     }
 32     public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
 33             new IClient().Receive();        
 34     }
 35 
 36     public void Receive() throws UnknownHostException, IOException, InterruptedException
 37     {
 38         Socket client = new Socket("localhost",9000);
 39         DataOutputStream dos = new DataOutputStream(client.getOutputStream());
 40         DataInputStream dis =new DataInputStream(client.getInputStream());        
 41         int length = dis.read();                                //读取接收数据帧的长度
 42         //System.out.println(length);                                //输出要接受的数据的长度        
 43         for(int i=0;i<length;i++)
 44         {
 45             char c = dis.readChar();                            //接收数据帧,判断是否正确            
 46             if(c!=‘η‘){                                            //CRC数据帧校验正确
 47                 Random ran=new java.util.Random();
 48                 int re=ran.nextInt(13);
 49                 if(re>=4){                                        //数据帧帧数正确
 50                     if(i<length-1)
 51                     {    
 52                         if (jta.getText() == null || "".equals(jta.getText())) {
 53                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 54                         } else {
 55                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 56                         }
 57                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);    //输出接受到的数据帧
 58                         dos.writeInt(i+2);    
 59                     }
 60                     else
 61                     {
 62                         if (jta.getText() == null || "".equals(jta.getText())) {
 63                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 64                         } else {
 65                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 66                         }
 67                         
 68                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);
 69                         dos.writeInt(i+2);                        //向发送方返回确认帧
 70                         Thread.sleep(100);
 71                     }
 72                 }
 73                 else                                            //数据帧内容错误
 74                 {
 75                     if (jta.getText() == null || "".equals(jta.getText())) {
 76                         jta.append("Receive:" + "不是所需要的帧,丢弃等待!");
 77                     } else {
 78                         jta.append("\r\nReceive:" + "不是所需要的帧,丢弃等待!");
 79                     }
 80                     Thread.sleep(600);
 81                     //System.out.println("不是所需要的帧,丢弃等待!");                    
 82                     dos.writeInt(-1);
 83                     i=i-1;
 84                 }    
 85             }
 86             else if(c==‘η‘)                                        //CRC数据帧错误丢弃等待
 87             {
 88                 if (jta.getText() == null || "".equals(jta.getText())) {
 89                     jta.append("Receive:" + "数据帧错误!");
 90                 } else {
 91                     jta.append("\r\nReceive:" + "数据帧错误!");
 92                 }
 93                 //System.out.println("数据帧错误!");                
 94                 i=i-1;
 95                 dos.writeInt(-2);
 96             }        
 97         }
 98         if (jta.getText() == null || "".equals(jta.getText())) {
 99             jta.append( "接收信息成功!");
100         } else {
101             jta.append("\r\n" + "接收信息成功!");
102         }
103         jta.setEnabled(false);
104         //System.out.println("接收信息成功!");
105         client.close();
106     }
107 }

 

  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.*;
  7 import java.net.*;
  8 import java.security.Timestamp;
  9 import java.util.*;
 10 import java.util.prefs.BackingStoreException;
 11 
 12 import javax.swing.*;
 13 
 14 public class IServer extends JFrame {
 15     private static long  time1;
 16     private static    long  time2;
 17     private JPanel jp = new JPanel();
 18     private JTextArea jta = new JTextArea();
 19     private JScrollPane jsp = null;
 20     private JButton jb = new JButton("发送");
 21     private JTextField jtf = new JTextField(30);
 22     private ServerSocket server;
 23     private Socket socket;
 24     DataOutputStream dos; 
 25     DataInputStream dis;
 26     public IServer() throws IOException
 27     {
 28         Toolkit t = Toolkit.getDefaultToolkit();
 29         Dimension Size =t.getScreenSize();
 30         int width =  Size.width; 
 31         int height = Size.height;
 32         setLocationByPlatform(true);
 33         jsp = new JScrollPane(jta);
 34         this.setTitle("发送方");
 35         this.setSize(600, 500);
 36         this.setBounds((width - 600) / 2,
 37                 (height - 500) / 2, 600,500);
 38         jp.add(jtf);
 39         jp.add(jb);
 40         this.add(jsp, BorderLayout.CENTER);
 41         this.add(jp, BorderLayout.SOUTH);
 42         jta.setCaretPosition(jta.getDocument().getLength());// 设置滚动条自动滚动
 43         this.setVisible(true);
 44         this.setAlwaysOnTop(true);
 45         server = new ServerSocket(9000);
 46         socket= server.accept();
 47         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 48         dos = new DataOutputStream(socket.getOutputStream());
 49         dis =new DataInputStream(socket.getInputStream());
 50         jb.addActionListener(new ActionListener(){
 51             public void actionPerformed(ActionEvent event){
 52                 try {
 53                     Send();
 54                 } catch (InterruptedException | IOException e) {
 55                     // TODO Auto-generated catch block
 56                     e.printStackTrace();
 57                 }
 58             }
 59         });
 60     }
 61     public static void main(String[] args) throws IOException, InterruptedException
 62     {
 63             new IServer();                                
 64     }
 65     public void Send() throws InterruptedException, IOException
 66     {
 67         String info = jtf.getText();
 68         jtf.setText("");
 77         char[] Msg = info.toCharArray();
 78         dos.write(Msg.length);
 79         for(int i=0;i<Msg.length;i++)
 80         {                                                
 81             Random random=new java.util.Random();                        //实现数据帧错传,以随机数的概率来实现                        
 82             int result=random.nextInt(21);
 83             if(result>=4)
 84             {    
 85                 if (jta.getText() == null || "".equals(jta.getText())) {
 86                     jta.append("Sending...:" + Msg[i]);
 87                 } else {
 88                     jta.append("\r\nSending...:" + Msg[i]);
 89                 }
 90                 dos.writeChar(Msg[i]);                                    //发送数据帧
 91                 time1 = System.currentTimeMillis();                        //设置超时计时器                    
 92             }
 93             else
 94             {    
 95                 if (jta.getText() == null || "".equals(jta.getText())) {
 96                     jta.append("Sending...:" + Msg[i]);
 97                 } else {
 98                     jta.append("\r\nSending...:" + Msg[i]);
 99                 }
100                 dos.writeChar(‘η‘);                                        //发送数据帧
101                 time1 = System.currentTimeMillis();                         //设置超时计时器                                                                   
102             }                    
103             int c = dis.readInt();                                        //接受客户端确认帧
104             time2 = System.currentTimeMillis();
105             long time = time2-time1;
106             //System.out.println("接收所用时间:"+time);
107             if(time<500)                                                        //确认帧未超时
108             {
109                 if(c==i+2)                                                    //确认帧正确,传输成功,准备传输下一个帧
110                 { 
111                     if(i<Msg.length-1)
112                     {
113                         if (jta.getText() == null || "".equals(jta.getText())) {
114                             jta.append("Receive:" + c);
115                         } else {
116                             jta.append("\r\nReceive:" + c);
117                         }
118                         //System.out.println("接收方返回帧:"+c+"  第"+(i+1)+"号帧传输成功!");
119                     }
120                     else
121                     {
122                         if (jta.getText() == null || "".equals(jta.getText())) {
123                             jta.append("Receive:" + (i+2)+"数据传输成功!");
124                         } else {
125                             jta.append("\r\nReceive:" + (i+2)+"数据传输成功!");
126                             jta.setEnabled(false);
127                             
128                         }
129                         //System.out.println("第"+(i+1)+"号帧传输成功!");
130                         //System.out.println("数据传输成功!");
131                         server.close();
132                     }        
133                 }
134                 else if(c==-2)                                                        //确认帧出错,超时重传
135                 {
136                     if (jta.getText() == null || "".equals(jta.getText())) {
137                         jta.append("Receive:数据帧出错,丢弃等待....."+
138                                 "确认帧接收超时,重新发送"+(i+1)+"帧");
139                     } else {
140                         jta.append("\r\nReceive:Receive:数据帧出错,丢弃等待....."
141                     +"确认帧接收超时,重新发送"+(i+1)+"帧");
142                     }
143                     //System.out.println("数据帧出错,丢弃等待.....");
144                     Thread.sleep(300);
145                     //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
146                     i=i-1;
147                 }
148             }
149             else//超时,重传
150             {
151                 if (jta.getText() == null || "".equals(jta.getText())) {
152                     jta.append("Receive:" +"确认帧接收超时,重新发送"+(i+1)+"帧");
153                 } else {
154                     jta.append("\r\nReceive:" + "确认帧接收超时,重新发送"+(i+1)+"帧");
155                 }
156                 //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
157                 i=i-1;    
158             }    
159         }
160     }
161 }

 

---恢复内容结束---

  1//服务端,发送方
  1 //客户端,接收方
  2 
  3 import java.awt.*;
  4 import java.io.*;
  5 import java.net.*;
  6 import java.util.*;
  7 import javax.swing.*;
  8 
  9 public class IClient extends JFrame  {
 10     JPanel jp = new JPanel();
 11     JTextArea jta = new JTextArea();
 12     JScrollPane jsp = null;
 13 
 14     public IClient()
 15     {
 16         Toolkit t = Toolkit.getDefaultToolkit();
 17         Dimension Size =t.getScreenSize();
 18         int width =  Size.width; 
 19         int height = Size.height;
 20         setLocationByPlatform(true);
 21         this.setAlwaysOnTop(true);
 22         jsp = new JScrollPane(jta);
 23         this.setTitle("接收方");
 24         this.add(jp);
 25         this.setSize(500, 400);
 26         this.setBounds((width - 500) / 2,
 27                 (height - 400) / 2, 600,500);
 28         this.add(jsp, BorderLayout.CENTER);
 29         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 30         this.setVisible(true);
 31     }
 32     public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
 33             new IClient().Receive();        
 34     }
 35 
 36     public void Receive() throws UnknownHostException, IOException, InterruptedException
 37     {
 38         Socket client = new Socket("localhost",9000);
 39         DataOutputStream dos = new DataOutputStream(client.getOutputStream());
 40         DataInputStream dis =new DataInputStream(client.getInputStream());        
 41         int length = dis.read();                                //读取接收数据帧的长度
 42         //System.out.println(length);                                //输出要接受的数据的长度        
 43         for(int i=0;i<length;i++)
 44         {
 45             char c = dis.readChar();                            //接收数据帧,判断是否正确            
 46             if(c!=‘η‘){                                            //CRC数据帧校验正确
 47                 Random ran=new java.util.Random();
 48                 int re=ran.nextInt(13);
 49                 if(re>=4){                                        //数据帧帧数正确
 50                     if(i<length-1)
 51                     {    
 52                         if (jta.getText() == null || "".equals(jta.getText())) {
 53                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 54                         } else {
 55                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 56                         }
 57                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);    //输出接受到的数据帧
 58                         dos.writeInt(i+2);    
 59                     }
 60                     else
 61                     {
 62                         if (jta.getText() == null || "".equals(jta.getText())) {
 63                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 64                         } else {
 65                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 66                         }
 67                         
 68                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);
 69                         dos.writeInt(i+2);                        //向发送方返回确认帧
 70                         Thread.sleep(100);
 71                     }
 72                 }
 73                 else                                            //数据帧内容错误
 74                 {
 75                     if (jta.getText() == null || "".equals(jta.getText())) {
 76                         jta.append("Receive:" + "不是所需要的帧,丢弃等待!");
 77                     } else {
 78                         jta.append("\r\nReceive:" + "不是所需要的帧,丢弃等待!");
 79                     }
 80                     Thread.sleep(600);
 81                     //System.out.println("不是所需要的帧,丢弃等待!");                    
 82                     dos.writeInt(-1);
 83                     i=i-1;
 84                 }    
 85             }
 86             else if(c==‘η‘)                                        //CRC数据帧错误丢弃等待
 87             {
 88                 if (jta.getText() == null || "".equals(jta.getText())) {
 89                     jta.append("Receive:" + "数据帧错误!");
 90                 } else {
 91                     jta.append("\r\nReceive:" + "数据帧错误!");
 92                 }
 93                 //System.out.println("数据帧错误!");                
 94                 i=i-1;
 95                 dos.writeInt(-2);
 96             }        
 97         }
 98         if (jta.getText() == null || "".equals(jta.getText())) {
 99             jta.append( "接收信息成功!");
100         } else {
101             jta.append("\r\n" + "接收信息成功!");
102         }
103         jta.setEnabled(false);
104         //System.out.println("接收信息成功!");
105         client.close();
106     }
107 }

 

  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.*;
  7 import java.net.*;
  8 import java.security.Timestamp;
  9 import java.util.*;
 10 import java.util.prefs.BackingStoreException;
 11 
 12 import javax.swing.*;
 13 
 14 public class IServer extends JFrame {
 15     private static long  time1;
 16     private static    long  time2;
 17     private JPanel jp = new JPanel();
 18     private JTextArea jta = new JTextArea();
 19     private JScrollPane jsp = null;
 20     private JButton jb = new JButton("发送");
 21     private JTextField jtf = new JTextField(30);
 22     private ServerSocket server;
 23     private Socket socket;
 24     DataOutputStream dos; 
 25     DataInputStream dis;
 26     public IServer() throws IOException
 27     {
 28         Toolkit t = Toolkit.getDefaultToolkit();
 29         Dimension Size =t.getScreenSize();
 30         int width =  Size.width; 
 31         int height = Size.height;
 32         setLocationByPlatform(true);
 33         jsp = new JScrollPane(jta);
 34         this.setTitle("发送方");
 35         this.setSize(600, 500);
 36         this.setBounds((width - 600) / 2,
 37                 (height - 500) / 2, 600,500);
 38         jp.add(jtf);
 39         jp.add(jb);
 40         this.add(jsp, BorderLayout.CENTER);
 41         this.add(jp, BorderLayout.SOUTH);
 42         jta.setCaretPosition(jta.getDocument().getLength());// 设置滚动条自动滚动
 43         this.setVisible(true);
 44         this.setAlwaysOnTop(true);
 45         server = new ServerSocket(9000);
 46         socket= server.accept();
 47         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 48         dos = new DataOutputStream(socket.getOutputStream());
 49         dis =new DataInputStream(socket.getInputStream());
 50         jb.addActionListener(new ActionListener(){
 51             public void actionPerformed(ActionEvent event){
 52                 try {
 53                     Send();
 54                 } catch (InterruptedException | IOException e) {
 55                     // TODO Auto-generated catch block
 56                     e.printStackTrace();
 57                 }
 58             }
 59         });
 60     }
 61     public static void main(String[] args) throws IOException, InterruptedException
 62     {
 63             new IServer();                                
 64     }
 65     public void Send() throws InterruptedException, IOException
 66     {
 67         String info = jtf.getText();
 68         jtf.setText("");
 69         /*if (jta.getText() == null || "".equals(jta.getText())) {
 70             jta.append("Client:" + info);
 71         } else {
 72             jta.append("\r\nClient:" + info);
 73         }*/
 74 
 75         
 76         ///////////////////////////////////
 77         char[] Msg = info.toCharArray();
 78         dos.write(Msg.length);
 79         for(int i=0;i<Msg.length;i++)
 80         {                                                
 81             Random random=new java.util.Random();                        //实现数据帧错传,以随机数的概率来实现                        
 82             int result=random.nextInt(21);
 83             if(result>=4)
 84             {    
 85                 if (jta.getText() == null || "".equals(jta.getText())) {
 86                     jta.append("Sending...:" + Msg[i]);
 87                 } else {
 88                     jta.append("\r\nSending...:" + Msg[i]);
 89                 }
 90                 dos.writeChar(Msg[i]);                                    //发送数据帧
 91                 time1 = System.currentTimeMillis();                        //设置超时计时器                    
 92             }
 93             else
 94             {    
 95                 if (jta.getText() == null || "".equals(jta.getText())) {
 96                     jta.append("Sending...:" + Msg[i]);
 97                 } else {
 98                     jta.append("\r\nSending...:" + Msg[i]);
 99                 }
100                 dos.writeChar(‘η‘);                                        //发送数据帧
101                 time1 = System.currentTimeMillis();                         //设置超时计时器                                                                   
102             }                    
103             int c = dis.readInt();                                        //接受客户端确认帧
104             time2 = System.currentTimeMillis();
105             long time = time2-time1;
106             //System.out.println("接收所用时间:"+time);
107             if(time<500)                                                        //确认帧未超时
108             {
109                 if(c==i+2)                                                    //确认帧正确,传输成功,准备传输下一个帧
110                 { 
111                     if(i<Msg.length-1)
112                     {
113                         if (jta.getText() == null || "".equals(jta.getText())) {
114                             jta.append("Receive:" + c);
115                         } else {
116                             jta.append("\r\nReceive:" + c);
117                         }
118                         //System.out.println("接收方返回帧:"+c+"  第"+(i+1)+"号帧传输成功!");
119                     }
120                     else
121                     {
122                         if (jta.getText() == null || "".equals(jta.getText())) {
123                             jta.append("Receive:" + (i+2)+"数据传输成功!");
124                         } else {
125                             jta.append("\r\nReceive:" + (i+2)+"数据传输成功!");
126                             jta.setEnabled(false);
127                             
128                         }
129                         //System.out.println("第"+(i+1)+"号帧传输成功!");
130                         //System.out.println("数据传输成功!");
131                         server.close();
132                     }        
133                 }
134                 else if(c==-2)                                                        //确认帧出错,超时重传
135                 {
136                     if (jta.getText() == null || "".equals(jta.getText())) {
137                         jta.append("Receive:数据帧出错,丢弃等待....."+
138                                 "确认帧接收超时,重新发送"+(i+1)+"帧");
139                     } else {
140                         jta.append("\r\nReceive:Receive:数据帧出错,丢弃等待....."
141                     +"确认帧接收超时,重新发送"+(i+1)+"帧");
142                     }
143                     //System.out.println("数据帧出错,丢弃等待.....");
144                     Thread.sleep(300);
145                     //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
146                     i=i-1;
147                 }
148             }
149             else//超时,重传
150             {
151                 if (jta.getText() == null || "".equals(jta.getText())) {
152                     jta.append("Receive:" +"确认帧接收超时,重新发送"+(i+1)+"帧");
153                 } else {
154                     jta.append("\r\nReceive:" + "确认帧接收超时,重新发送"+(i+1)+"帧");
155                 }
156                 //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
157                 i=i-1;    
158             }    
159         }
160     }
161 }

 

---恢复内容开始---

  1//服务端,发送方
  1 //客户端,接收方
  2 
  3 import java.awt.*;
  4 import java.io.*;
  5 import java.net.*;
  6 import java.util.*;
  7 import javax.swing.*;
  8 
  9 public class IClient extends JFrame  {
 10     JPanel jp = new JPanel();
 11     JTextArea jta = new JTextArea();
 12     JScrollPane jsp = null;
 13 
 14     public IClient()
 15     {
 16         Toolkit t = Toolkit.getDefaultToolkit();
 17         Dimension Size =t.getScreenSize();
 18         int width =  Size.width; 
 19         int height = Size.height;
 20         setLocationByPlatform(true);
 21         this.setAlwaysOnTop(true);
 22         jsp = new JScrollPane(jta);
 23         this.setTitle("接收方");
 24         this.add(jp);
 25         this.setSize(500, 400);
 26         this.setBounds((width - 500) / 2,
 27                 (height - 400) / 2, 600,500);
 28         this.add(jsp, BorderLayout.CENTER);
 29         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 30         this.setVisible(true);
 31     }
 32     public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
 33             new IClient().Receive();        
 34     }
 35 
 36     public void Receive() throws UnknownHostException, IOException, InterruptedException
 37     {
 38         Socket client = new Socket("localhost",9000);
 39         DataOutputStream dos = new DataOutputStream(client.getOutputStream());
 40         DataInputStream dis =new DataInputStream(client.getInputStream());        
 41         int length = dis.read();                                //读取接收数据帧的长度
 42         //System.out.println(length);                                //输出要接受的数据的长度        
 43         for(int i=0;i<length;i++)
 44         {
 45             char c = dis.readChar();                            //接收数据帧,判断是否正确            
 46             if(c!=‘η‘){                                            //CRC数据帧校验正确
 47                 Random ran=new java.util.Random();
 48                 int re=ran.nextInt(13);
 49                 if(re>=4){                                        //数据帧帧数正确
 50                     if(i<length-1)
 51                     {    
 52                         if (jta.getText() == null || "".equals(jta.getText())) {
 53                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 54                         } else {
 55                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 56                         }
 57                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);    //输出接受到的数据帧
 58                         dos.writeInt(i+2);    
 59                     }
 60                     else
 61                     {
 62                         if (jta.getText() == null || "".equals(jta.getText())) {
 63                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 64                         } else {
 65                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 66                         }
 67                         
 68                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);
 69                         dos.writeInt(i+2);                        //向发送方返回确认帧
 70                         Thread.sleep(100);
 71                     }
 72                 }
 73                 else                                            //数据帧内容错误
 74                 {
 75                     if (jta.getText() == null || "".equals(jta.getText())) {
 76                         jta.append("Receive:" + "不是所需要的帧,丢弃等待!");
 77                     } else {
 78                         jta.append("\r\nReceive:" + "不是所需要的帧,丢弃等待!");
 79                     }
 80                     Thread.sleep(600);
 81                     //System.out.println("不是所需要的帧,丢弃等待!");                    
 82                     dos.writeInt(-1);
 83                     i=i-1;
 84                 }    
 85             }
 86             else if(c==‘η‘)                                        //CRC数据帧错误丢弃等待
 87             {
 88                 if (jta.getText() == null || "".equals(jta.getText())) {
 89                     jta.append("Receive:" + "数据帧错误!");
 90                 } else {
 91                     jta.append("\r\nReceive:" + "数据帧错误!");
 92                 }
 93                 //System.out.println("数据帧错误!");                
 94                 i=i-1;
 95                 dos.writeInt(-2);
 96             }        
 97         }
 98         if (jta.getText() == null || "".equals(jta.getText())) {
 99             jta.append( "接收信息成功!");
100         } else {
101             jta.append("\r\n" + "接收信息成功!");
102         }
103         jta.setEnabled(false);
104         //System.out.println("接收信息成功!");
105         client.close();
106     }
107 }

 

  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.*;
  7 import java.net.*;
  8 import java.security.Timestamp;
  9 import java.util.*;
 10 import java.util.prefs.BackingStoreException;
 11 
 12 import javax.swing.*;
 13 
 14 public class IServer extends JFrame {
 15     private static long  time1;
 16     private static    long  time2;
 17     private JPanel jp = new JPanel();
 18     private JTextArea jta = new JTextArea();
 19     private JScrollPane jsp = null;
 20     private JButton jb = new JButton("发送");
 21     private JTextField jtf = new JTextField(30);
 22     private ServerSocket server;
 23     private Socket socket;
 24     DataOutputStream dos; 
 25     DataInputStream dis;
 26     public IServer() throws IOException
 27     {
 28         Toolkit t = Toolkit.getDefaultToolkit();
 29         Dimension Size =t.getScreenSize();
 30         int width =  Size.width; 
 31         int height = Size.height;
 32         setLocationByPlatform(true);
 33         jsp = new JScrollPane(jta);
 34         this.setTitle("发送方");
 35         this.setSize(600, 500);
 36         this.setBounds((width - 600) / 2,
 37                 (height - 500) / 2, 600,500);
 38         jp.add(jtf);
 39         jp.add(jb);
 40         this.add(jsp, BorderLayout.CENTER);
 41         this.add(jp, BorderLayout.SOUTH);
 42         jta.setCaretPosition(jta.getDocument().getLength());// 设置滚动条自动滚动
 43         this.setVisible(true);
 44         this.setAlwaysOnTop(true);
 45         server = new ServerSocket(9000);
 46         socket= server.accept();
 47         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 48         dos = new DataOutputStream(socket.getOutputStream());
 49         dis =new DataInputStream(socket.getInputStream());
 50         jb.addActionListener(new ActionListener(){
 51             public void actionPerformed(ActionEvent event){
 52                 try {
 53                     Send();
 54                 } catch (InterruptedException | IOException e) {
 55                     // TODO Auto-generated catch block
 56                     e.printStackTrace();
 57                 }
 58             }
 59         });
 60     }
 61     public static void main(String[] args) throws IOException, InterruptedException
 62     {
 63             new IServer();                                
 64     }
 65     public void Send() throws InterruptedException, IOException
 66     {
 67         String info = jtf.getText();
 68         jtf.setText("");
 77         char[] Msg = info.toCharArray();
 78         dos.write(Msg.length);
 79         for(int i=0;i<Msg.length;i++)
 80         {                                                
 81             Random random=new java.util.Random();                        //实现数据帧错传,以随机数的概率来实现                        
 82             int result=random.nextInt(21);
 83             if(result>=4)
 84             {    
 85                 if (jta.getText() == null || "".equals(jta.getText())) {
 86                     jta.append("Sending...:" + Msg[i]);
 87                 } else {
 88                     jta.append("\r\nSending...:" + Msg[i]);
 89                 }
 90                 dos.writeChar(Msg[i]);                                    //发送数据帧
 91                 time1 = System.currentTimeMillis();                        //设置超时计时器                    
 92             }
 93             else
 94             {    
 95                 if (jta.getText() == null || "".equals(jta.getText())) {
 96                     jta.append("Sending...:" + Msg[i]);
 97                 } else {
 98                     jta.append("\r\nSending...:" + Msg[i]);
 99                 }
100                 dos.writeChar(‘η‘);                                        //发送数据帧
101                 time1 = System.currentTimeMillis();                         //设置超时计时器                                                                   
102             }                    
103             int c = dis.readInt();                                        //接受客户端确认帧
104             time2 = System.currentTimeMillis();
105             long time = time2-time1;
106             //System.out.println("接收所用时间:"+time);
107             if(time<500)                                                        //确认帧未超时
108             {
109                 if(c==i+2)                                                    //确认帧正确,传输成功,准备传输下一个帧
110                 { 
111                     if(i<Msg.length-1)
112                     {
113                         if (jta.getText() == null || "".equals(jta.getText())) {
114                             jta.append("Receive:" + c);
115                         } else {
116                             jta.append("\r\nReceive:" + c);
117                         }
118                         //System.out.println("接收方返回帧:"+c+"  第"+(i+1)+"号帧传输成功!");
119                     }
120                     else
121                     {
122                         if (jta.getText() == null || "".equals(jta.getText())) {
123                             jta.append("Receive:" + (i+2)+"数据传输成功!");
124                         } else {
125                             jta.append("\r\nReceive:" + (i+2)+"数据传输成功!");
126                             jta.setEnabled(false);
127                             
128                         }
129                         //System.out.println("第"+(i+1)+"号帧传输成功!");
130                         //System.out.println("数据传输成功!");
131                         server.close();
132                     }        
133                 }
134                 else if(c==-2)                                                        //确认帧出错,超时重传
135                 {
136                     if (jta.getText() == null || "".equals(jta.getText())) {
137                         jta.append("Receive:数据帧出错,丢弃等待....."+
138                                 "确认帧接收超时,重新发送"+(i+1)+"帧");
139                     } else {
140                         jta.append("\r\nReceive:Receive:数据帧出错,丢弃等待....."
141                     +"确认帧接收超时,重新发送"+(i+1)+"帧");
142                     }
143                     //System.out.println("数据帧出错,丢弃等待.....");
144                     Thread.sleep(300);
145                     //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
146                     i=i-1;
147                 }
148             }
149             else//超时,重传
150             {
151                 if (jta.getText() == null || "".equals(jta.getText())) {
152                     jta.append("Receive:" +"确认帧接收超时,重新发送"+(i+1)+"帧");
153                 } else {
154                     jta.append("\r\nReceive:" + "确认帧接收超时,重新发送"+(i+1)+"帧");
155                 }
156                 //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
157                 i=i-1;    
158             }    
159         }
160     }
161 }

 

---恢复内容结束---

  1//服务端,发送方
  1 //客户端,接收方
  2 
  3 import java.awt.*;
  4 import java.io.*;
  5 import java.net.*;
  6 import java.util.*;
  7 import javax.swing.*;
  8 
  9 public class IClient extends JFrame  {
 10     JPanel jp = new JPanel();
 11     JTextArea jta = new JTextArea();
 12     JScrollPane jsp = null;
 13 
 14     public IClient()
 15     {
 16         Toolkit t = Toolkit.getDefaultToolkit();
 17         Dimension Size =t.getScreenSize();
 18         int width =  Size.width; 
 19         int height = Size.height;
 20         setLocationByPlatform(true);
 21         this.setAlwaysOnTop(true);
 22         jsp = new JScrollPane(jta);
 23         this.setTitle("接收方");
 24         this.add(jp);
 25         this.setSize(500, 400);
 26         this.setBounds((width - 500) / 2,
 27                 (height - 400) / 2, 600,500);
 28         this.add(jsp, BorderLayout.CENTER);
 29         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 30         this.setVisible(true);
 31     }
 32     public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
 33             new IClient().Receive();        
 34     }
 35 
 36     public void Receive() throws UnknownHostException, IOException, InterruptedException
 37     {
 38         Socket client = new Socket("localhost",9000);
 39         DataOutputStream dos = new DataOutputStream(client.getOutputStream());
 40         DataInputStream dis =new DataInputStream(client.getInputStream());        
 41         int length = dis.read();                                //读取接收数据帧的长度
 42         //System.out.println(length);                                //输出要接受的数据的长度        
 43         for(int i=0;i<length;i++)
 44         {
 45             char c = dis.readChar();                            //接收数据帧,判断是否正确            
 46             if(c!=‘η‘){                                            //CRC数据帧校验正确
 47                 Random ran=new java.util.Random();
 48                 int re=ran.nextInt(13);
 49                 if(re>=4){                                        //数据帧帧数正确
 50                     if(i<length-1)
 51                     {    
 52                         if (jta.getText() == null || "".equals(jta.getText())) {
 53                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 54                         } else {
 55                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 56                         }
 57                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);    //输出接受到的数据帧
 58                         dos.writeInt(i+2);    
 59                     }
 60                     else
 61                     {
 62                         if (jta.getText() == null || "".equals(jta.getText())) {
 63                             jta.append("Receive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 64                         } else {
 65                             jta.append("\r\nReceive:" + "成功接收"+(i+1)+"号帧,内容为:"+c);
 66                         }
 67                         
 68                         //System.out.println("成功接收"+(i+1)+"号帧,内容为:"+c);
 69                         dos.writeInt(i+2);                        //向发送方返回确认帧
 70                         Thread.sleep(100);
 71                     }
 72                 }
 73                 else                                            //数据帧内容错误
 74                 {
 75                     if (jta.getText() == null || "".equals(jta.getText())) {
 76                         jta.append("Receive:" + "不是所需要的帧,丢弃等待!");
 77                     } else {
 78                         jta.append("\r\nReceive:" + "不是所需要的帧,丢弃等待!");
 79                     }
 80                     Thread.sleep(600);
 81                     //System.out.println("不是所需要的帧,丢弃等待!");                    
 82                     dos.writeInt(-1);
 83                     i=i-1;
 84                 }    
 85             }
 86             else if(c==‘η‘)                                        //CRC数据帧错误丢弃等待
 87             {
 88                 if (jta.getText() == null || "".equals(jta.getText())) {
 89                     jta.append("Receive:" + "数据帧错误!");
 90                 } else {
 91                     jta.append("\r\nReceive:" + "数据帧错误!");
 92                 }
 93                 //System.out.println("数据帧错误!");                
 94                 i=i-1;
 95                 dos.writeInt(-2);
 96             }        
 97         }
 98         if (jta.getText() == null || "".equals(jta.getText())) {
 99             jta.append( "接收信息成功!");
100         } else {
101             jta.append("\r\n" + "接收信息成功!");
102         }
103         jta.setEnabled(false);
104         //System.out.println("接收信息成功!");
105         client.close();
106     }
107 }

 

  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.*;
  7 import java.net.*;
  8 import java.security.Timestamp;
  9 import java.util.*;
 10 import java.util.prefs.BackingStoreException;
 11 
 12 import javax.swing.*;
 13 
 14 public class IServer extends JFrame {
 15     private static long  time1;
 16     private static    long  time2;
 17     private JPanel jp = new JPanel();
 18     private JTextArea jta = new JTextArea();
 19     private JScrollPane jsp = null;
 20     private JButton jb = new JButton("发送");
 21     private JTextField jtf = new JTextField(30);
 22     private ServerSocket server;
 23     private Socket socket;
 24     DataOutputStream dos; 
 25     DataInputStream dis;
 26     public IServer() throws IOException
 27     {
 28         Toolkit t = Toolkit.getDefaultToolkit();
 29         Dimension Size =t.getScreenSize();
 30         int width =  Size.width; 
 31         int height = Size.height;
 32         setLocationByPlatform(true);
 33         jsp = new JScrollPane(jta);
 34         this.setTitle("发送方");
 35         this.setSize(600, 500);
 36         this.setBounds((width - 600) / 2,
 37                 (height - 500) / 2, 600,500);
 38         jp.add(jtf);
 39         jp.add(jb);
 40         this.add(jsp, BorderLayout.CENTER);
 41         this.add(jp, BorderLayout.SOUTH);
 42         jta.setCaretPosition(jta.getDocument().getLength());// 设置滚动条自动滚动
 43         this.setVisible(true);
 44         this.setAlwaysOnTop(true);
 45         server = new ServerSocket(9000);
 46         socket= server.accept();
 47         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 48         dos = new DataOutputStream(socket.getOutputStream());
 49         dis =new DataInputStream(socket.getInputStream());
 50         jb.addActionListener(new ActionListener(){
 51             public void actionPerformed(ActionEvent event){
 52                 try {
 53                     Send();
 54                 } catch (InterruptedException | IOException e) {
 55                     // TODO Auto-generated catch block
 56                     e.printStackTrace();
 57                 }
 58             }
 59         });
 60     }
 61     public static void main(String[] args) throws IOException, InterruptedException
 62     {
 63             new IServer();                                
 64     }
 65     public void Send() throws InterruptedException, IOException
 66     {
 67         String info = jtf.getText();
 68         jtf.setText("");
 69         /*if (jta.getText() == null || "".equals(jta.getText())) {
 70             jta.append("Client:" + info);
 71         } else {
 72             jta.append("\r\nClient:" + info);
 73         }*/
 74 
 75         
 76         ///////////////////////////////////
 77         char[] Msg = info.toCharArray();
 78         dos.write(Msg.length);
 79         for(int i=0;i<Msg.length;i++)
 80         {                                                
 81             Random random=new java.util.Random();                        //实现数据帧错传,以随机数的概率来实现                        
 82             int result=random.nextInt(21);
 83             if(result>=4)
 84             {    
 85                 if (jta.getText() == null || "".equals(jta.getText())) {
 86                     jta.append("Sending...:" + Msg[i]);
 87                 } else {
 88                     jta.append("\r\nSending...:" + Msg[i]);
 89                 }
 90                 dos.writeChar(Msg[i]);                                    //发送数据帧
 91                 time1 = System.currentTimeMillis();                        //设置超时计时器                    
 92             }
 93             else
 94             {    
 95                 if (jta.getText() == null || "".equals(jta.getText())) {
 96                     jta.append("Sending...:" + Msg[i]);
 97                 } else {
 98                     jta.append("\r\nSending...:" + Msg[i]);
 99                 }
100                 dos.writeChar(‘η‘);                                        //发送数据帧
101                 time1 = System.currentTimeMillis();                         //设置超时计时器                                                                   
102             }                    
103             int c = dis.readInt();                                        //接受客户端确认帧
104             time2 = System.currentTimeMillis();
105             long time = time2-time1;
106             //System.out.println("接收所用时间:"+time);
107             if(time<500)                                                        //确认帧未超时
108             {
109                 if(c==i+2)                                                    //确认帧正确,传输成功,准备传输下一个帧
110                 { 
111                     if(i<Msg.length-1)
112                     {
113                         if (jta.getText() == null || "".equals(jta.getText())) {
114                             jta.append("Receive:" + c);
115                         } else {
116                             jta.append("\r\nReceive:" + c);
117                         }
118                         //System.out.println("接收方返回帧:"+c+"  第"+(i+1)+"号帧传输成功!");
119                     }
120                     else
121                     {
122                         if (jta.getText() == null || "".equals(jta.getText())) {
123                             jta.append("Receive:" + (i+2)+"数据传输成功!");
124                         } else {
125                             jta.append("\r\nReceive:" + (i+2)+"数据传输成功!");
126                             jta.setEnabled(false);
127                             
128                         }
129                         //System.out.println("第"+(i+1)+"号帧传输成功!");
130                         //System.out.println("数据传输成功!");
131                         server.close();
132                     }        
133                 }
134                 else if(c==-2)                                                        //确认帧出错,超时重传
135                 {
136                     if (jta.getText() == null || "".equals(jta.getText())) {
137                         jta.append("Receive:数据帧出错,丢弃等待....."+
138                                 "确认帧接收超时,重新发送"+(i+1)+"帧");
139                     } else {
140                         jta.append("\r\nReceive:Receive:数据帧出错,丢弃等待....."
141                     +"确认帧接收超时,重新发送"+(i+1)+"帧");
142                     }
143                     //System.out.println("数据帧出错,丢弃等待.....");
144                     Thread.sleep(300);
145                     //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
146                     i=i-1;
147                 }
148             }
149             else//超时,重传
150             {
151                 if (jta.getText() == null || "".equals(jta.getText())) {
152                     jta.append("Receive:" +"确认帧接收超时,重新发送"+(i+1)+"帧");
153                 } else {
154                     jta.append("\r\nReceive:" + "确认帧接收超时,重新发送"+(i+1)+"帧");
155                 }
156                 //System.out.println("确认帧接收超时,重新发送"+(i+1)+"帧");
157                 i=i-1;    
158             }    
159         }
160     }
161 }

 

java利用socket通信模拟实现ARQ停止等待协议