⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crcrecieve.java

📁 网路原理实验中的循环冗余校验算法
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;




public class CrcRecieve extends JFrame{
	DataInputStream dis;
	DataOutputStream dos ;
	String Date;
	boolean flag = false;
	ServerSocket ss = null;
	Socket s1=null;
	int[] P;
	public CrcRecieve(int[] p){
		this.P=p;
		this.pcount=p.length-1;
		System.out.println(P.length);
		for(int i=0;i<p.length;i++){
			System.out.println(p[i]);
		}
	}
	

	public void connect() {
		
		try {
			ss = new ServerSocket(7777);
			s1=ss.accept();
			flag = true;
			new RecieveThread(s1).start();
			System.out.println("有SEND");
		} catch (UnknownHostException e) {
			
			//lwarm.setText("接收方未启动");
		} catch (IOException e) {
			//lwarm.setText("连接失败");
		}
	}
	
	int xor(int x,int y)
	{
	        if(x==y)
	                return 0;
	        else
	                return 1;
	}
	int mcount,ma=0;
	int num;
	int pcount;
	int qcount=0;
	int rcount=0;
	int[] zero=new int[100];
	int[] M=new int[100];
	//int[] P=new int[5];
	int[] Q=new int[100];
	int[] R=new int[100];
	int[] GetM=new int [100];
	
	TextArea mazi = new TextArea();
	JTextField info = new JTextField("info");
	JLabel rec = new JLabel("         接受方");
		
	void RecieveView() {
			this.connect();
			for(int i=0;i<20;i++){
				zero[i]=0;
			}
			JButton bt=new JButton("请求重发");
			int[] zero=new int[20];
			this.setSize(400,250);
			this.setLocation(550, 200);
			this.setVisible(true);
			this.setLayout(null);
			rec.setBounds(20, 20, 100, 30);
			rec.setBorder(new BevelBorder(1,Color.BLUE,Color.BLUE));
			info.setBounds(20, 150, 200, 30);
			info.setEnabled(false);
			bt.setBounds(140, 20, 100, 30);
			mazi.setBounds(20,60,350,60);
			//mazi.setEnabled(false);
			btLis b1= new btLis();
			bt.addActionListener(b1);
			this.add(mazi);
			this.add(bt);
			this.add(info);
			this.add(rec);
			this.setBackground(Color.lightGray);
			
			
			//b4Lis c4= new b4Lis();
		
			
			//b4.addActionListener(c4);
		//	b5.addActionListener(c5);
			//this.setResizable(false);
			
			this.setTitle("计科061319 肖亮");
			this.addWindowListener(new MyWindowMonitor());
			
			

		}
		
		class MyWindowMonitor extends WindowAdapter {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				// notconnect();
				System.exit(0);
			}
		}
		
		class btLis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				try {
					dos = new DataOutputStream(s1.getOutputStream());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					dos.writeInt(1);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}	
		}
		class RecieveThread extends Thread{
			 Socket sc = null;
			public RecieveThread(Socket s){
				sc = s;
			}
			public void run() {
				 try {
					 while(true){
						
						 if(flag){
							dis = new DataInputStream(sc.getInputStream());
							
							Date = dis.readUTF();
							mcount = Date.length();
							System.out.println("Date:"+Date);
							mazi.setText(Date);
							for(int i=0;i<Date.length();i++){
								int j=Date.charAt(i)-48;
									GetM[i]=j;
							}
							qcount = 0;
							for(int i=0;i<mcount;i++){
								//System.out.print(GetM[i]);
							}
							for(int i=0;i<mcount;i++){
								R[i]=zero[i];
							}
							for(int i=0;i<mcount;i++){
								Q[i]=zero[i];
							}
							  //数组清零
							for(int i=0;i<=mcount-1-pcount;i++){
				                if(i==0){
				                        for(int k=0;k<=pcount;k++)
				                                R[k]=GetM[k];        //初次运算
				                }
				                if(R[0]==1){
				                        Q[qcount++]=1;
				                        for(int j=0;j<=pcount;j++){        //位数次异或运算
				                                R[j]=xor(R[j],P[j]);        //异或运算        
				                        }
				                        if(i!=mcount-pcount){
				                                for(int j=0;j<=pcount;j++){        //余数移位及获取m的对


				                                        if(j==pcount)
				                                                R[pcount]=GetM[i+pcount+1];
				                                        else
				                                                R[j]=R[j+1];
				                                }
				                        }
				                }
				                else{
				                     Q[qcount++]=0;
				                     if(i!=mcount-pcount){
				                     for(int j=0;j<=pcount;j++){        //余数移位及获取m的对

				                    if(j==pcount)
				                       R[pcount]=GetM[i+pcount+1];
				                    else
				                        R[j]=R[j+1];
				                                }
				                    }
				                }
							}
							int count=0;
							for(int i=0;i<R.length;i++){
								if(R[i]==1){
									count++;
									info.setText("检测到传输数据错误,请求重发");
									System.out.println("检测到传输数据错误,请求重发");
								
								}
							}
							if(count==0){
								info.setText("传输正确,等待发送下一帧数据");
								System.out.println("传输正确,等待发送下一帧数据");
								//dos.writeInt(0);
							}
						}
						 
						 
					 }
					 
				} catch (IOException e) {
				//	lwarm.setText("与发送方断开");
				}	 
			}
		}
			
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -