📄 crcsend.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.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class CrcSend extends JFrame{
String number;
int min;
String Date;
Socket s = null;
int[] P;
public CrcSend(int[] p){
this.P=p;
this.pcount=p.length-1;
this.min=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 {
s = new Socket("127.0.0.1", 7777);
//new SendThread(s).start();
new GetInfoThread(s).start();
} 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];
JButton Bweishu ;
JButton b1 = new JButton("提交");
JButton b2 = new JButton("生成码字");
JButton b3 = new JButton("send(无噪声)");
JButton b4 = new JButton("send(含噪声)");
JButton b5 = new JButton("随机生成");
JTextField Tweishu = new JTextField();
TextArea mazi = new TextArea();
JTextField info = new JTextField("info");
TextArea T1 = new TextArea();
/*JTextField T2 = new JTextField();
JTextField T3 = new JTextField();
JTextField T4 = new JTextField();
JTextField T5 = new JTextField();
JTextField T6 = new JTextField();
JTextField T7 = new JTextField();
JTextField T8 = new JTextField();
JTextField T9 = new JTextField();
JTextField T10 = new JTextField();*/
JLabel send = new JLabel(" 发送方");
ArrayList<TextArea> Texts=new ArrayList<TextArea>();
void SendView() {
Bweishu = new JButton("传输位数("+this.min+"~50)");
this.connect();
for(int i=0;i<20;i++){
zero[i]=0;
}
int[] zero=new int[20];
this.setSize(600,450);
this.setLocation(20, 100);
this.setVisible(true);
this.setLayout(null);
Bweishu.setBounds(50,100,140,20);
//Bweishu.setEnabled(false);
send.setBounds(50, 50, 100, 30);
send.setBorder(new BevelBorder(1,Color.BLUE,Color.BLUE));
b1.setBounds(300,100,140,20);
b2.setBounds(50,240,140,25);
b3.setBounds(50,350,140,25);
b4.setBounds(50,320,140,25);
b5.setBounds(50,150,140,25);
//b5.setVisible(false);
info.setBounds(200, 350, 200, 25);
//info.setEnabled(false);
Tweishu.setBounds(220,100,70,20);
mazi.setBounds(200,240,300,60);
//mazi.setEnabled(false);
T1.setBounds(200,150,300,60);
/*T2.setBounds(240,150,30,20);
T3.setBounds(280,150,30,20);
T4.setBounds(320,150,30,20);
T5.setBounds(360,150,30,20);
T6.setBounds(400,150,30,20);
T7.setBounds(440,150,30,20);
T8.setBounds(480,150,30,20);
T9.setBounds(520,150,30,20);
T10.setBounds(560,150,30,20);*/
T1.setVisible(false);
/*T2.setVisible(false);
T3.setVisible(false);
T4.setVisible(false);
T5.setVisible(false);
T6.setVisible(false);
T7.setVisible(false);
T8.setVisible(false);
T9.setVisible(false);
T10.setVisible(false);*/
Texts.add(T1);
/*Texts.add(T2);
Texts.add(T3);
Texts.add(T4);
Texts.add(T5);
Texts.add(T6);
Texts.add(T7);
Texts.add(T8);
Texts.add(T9);
Texts.add(T10);*/
this.add(Bweishu);
this.add(Tweishu);
this.add(mazi);
this.add(info);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(T1);
/*this.add(T2);
this.add(T3);
this.add(T4);
this.add(T5);
this.add(T6);
this.add(T7);
this.add(T8);
this.add(T9);
this.add(T10);*/
this.add(send);
this.setBackground(Color.lightGray);
b1Lis c1= new b1Lis();
b2Lis c2= new b2Lis();
b3Lis c3= new b3Lis();
b4Lis c4= new b4Lis();
b5Lis c5= new b5Lis();
b1.addActionListener(c1);
b2.addActionListener(c2);
b3.addActionListener(c3);
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 b1Lis implements ActionListener{
public void actionPerformed(ActionEvent e) {
b5.setVisible(false);
T1.setVisible(false);
try {
mcount = Integer.parseInt(Tweishu.getText());
num = mcount;
if(mcount>= min){
b5.setVisible(true);
T1.setVisible(true);
}
} catch (NumberFormatException e1) {
}
}
}
class b5Lis implements ActionListener{//随机数
public void actionPerformed(ActionEvent e) {
String ss="";
for(int j=0;j<num-1;j++){
double x=Math.random();
int t;
if(x>0.5)
t=1;
else
t=0;
ss=ss+t;
if(j==0){
ss=ss+1;
}
}
T1.setText(ss);
}
}
class b2Lis implements ActionListener{// 得到数
public void actionPerformed(ActionEvent e) {
String ss = T1.getText();
mcount = num;
ma=0;
qcount=0;
for(int i=0;i<mcount;i++){
int j=Integer.parseInt(ss.substring(i, i+1)/*Texts.get(i).getText()*/);
System.out.println(j);
if(j>1&&j<0){
ma=0;
return;
}
M[i]=j;
ma = ma+j*(int)(Math.pow(2,(mcount-1-i)));
}
System.out.println(ma);
for(int i=1;i<=pcount;i++){ //补零2nM
M[mcount]=0;
mcount++;
}
for(int i=0;i<=mcount-1-pcount;i++){
if(i==0){
for(int k=0;k<=pcount;k++)
R[k]=M[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]=M[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]=M[i+pcount+1];
else
R[j]=R[j+1];
}
}
}
}
mcount = mcount - pcount;
for(int i=0;i<pcount;i++){
M[mcount]= R[i];
mcount++;
}
for(int i=0;i<mcount;i++){
System.out.print(M[i]);
}
System.out.println("");
int num=0;
String s="";
for(int i=0;i<mcount;i++){
s = s+M[i];
}
Date = s;
mazi.setText(s);
}
}
class b3Lis implements ActionListener{
public void actionPerformed(ActionEvent e) {
String sss="";
for(int i=0;i<mcount;i++){
sss = sss+M[i];
}
Date = sss;
DataOutputStream dos;
try {
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(Date);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class b4Lis implements ActionListener{
public void actionPerformed(ActionEvent e) {
for(int i=0;i<mcount;i++){
GetM[i]=M[i];
}
int x=(int)(Math.random()*(mcount-pcount));
if(GetM[x]==1){
GetM[x]=0;
}
else{
GetM[x]=1;
}
String sss="";
for(int i=0;i<mcount;i++){
sss = sss+GetM[i];
}
Date = sss;
DataOutputStream dos;
try {
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(Date);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/*class SendThread extends Thread{
private Socket sk = null;
public SendThread(Socket s){
sk = s;
}
public void run() {
try {
while(true){
if(flag){
DataOutputStream dos = new DataOutputStream(sk.getOutputStream());
dos.writeUTF(Date);
flag = false;
}
}
} catch (IOException e) {
//lwarm.setText("接收方断开");
}
}
}*/
class GetInfoThread extends Thread{
private Socket sk = null;
public GetInfoThread(Socket s){
sk = s;
}
public void run() {
try {
while(true){
if(true){
DataInputStream dis = new DataInputStream(sk.getInputStream());
int x=dis.readInt();
if(x==0){
info.setText("收到传输正确反馈,可以发送下一帧数据");
}
else{
info.setText("收到重发请求,请求重发");
}
}
}
}catch (IOException e) {
//lwarm.setText("接收方断开");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -