发送方:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class Cl extends Frame implements ActionListener
{
TextArea ta=new TextArea(5,10);
TextField tf=new TextField(10);
Socket c;
double m;
int i=0;
int q=0;
static int n;
String ss="";
static char abc[];
DataInputStream in=null;
DataOutputStream out=null;
String s;
double m1;
public Cl()
{
super("发送方");
s=null;
setSize(250,250);
tf.addActionListener(this);
add("North",tf);
add("Center",ta);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tf)
{
String sss=tf.getText();
abc=sss.toCharArray();
try
{
c=new Socket("200.200.202.58",8888);
in=new DataInputStream(c.getInputStream());
out=new DataOutputStream(c.getOutputStream());
while(true)
{
n=0;
m=Math.random()*100;
m1=Math.random()*100;
if((m1>=0)&&(m1<50))
{
q=1;
}
else
{
q=0;
}
ss=String.valueOf(abc[i]);
String ss1=String.valueOf(q);
ss1=ss1.concat(" ");
ss1=ss1.concat(ss);
ta.append("发送:"+ss1);
ta.append("\n");
if(m>20&&m<=100)
{
out.writeUTF(ss1);
ss1="";
}
System.out.println("启动超时计时器:");
for(int zz=0;zz<3;zz++)
{System.out.println(n++);
Thread.sleep(1000);
if(m>20&&m<=100)
{
break;
}
}
if(n==3)
{
ta.append("超时重发");
ta.append("\n");
continue;
}
while(true)
{
s=in.readUTF();
if(s.equals("ack"))
{
ta.append("发送正确继续发"+"\n");
i=i+1;
break;
}
if(s.equals("nak"))
{
ta.append("发送错误重新发"+"\n");
break;
}
}
if(i==abc.length){break;}
}
}
catch(InterruptedException e1){}
catch(IOException e2){}
}
}
}
public class fa
{
public static void main(String args[])
{
new Cl();
}
}
接收方:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class Se extends Frame
{
TextField tf=new TextField(10);
TextArea ta=new TextArea(5,10);
Socket c;
public Se()
{
super("接收方");
setSize(250,250);
ServerSocket sever=null;
Socket you=null;
String s=null;
DataOutputStream out=null;
DataInputStream in=null;
double m1,m2;
String s1,s2,s3;
s1="";s2="";s3="";
int i=0;
int q=0;
int n;
add("North",tf);
add("Center",ta);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
show();
try
{
sever=new ServerSocket(8888);
you=sever.accept();
in=new DataInputStream(you.getInputStream());
out=new DataOutputStream(you.getOutputStream());
while(true)
{
s=in.readUTF();
m1=Math.random()*100;
m2=Math.random()*100;
if(m2>0&&m2<50)
{
q=1;
}
else
{
q=0;
}
char a[]=s.toCharArray();
if((m1>20&&m1<=100)&&((a[0]-'0')==q))
{
ta.append("收到:"+s);
ta.append("\n");
s2=a[2]+"";
s3=s3.concat(s2);
tf.setText(s3);
out.writeUTF("ack");
}
if(m1>=0&&m1<=20)
{
ta.append("CRC检测错误!");
ta.append("\n");
out.writeUTF("nak");
}
else if((a[0]-'0')!=q)
{
ta.append("序号不对应丢弃帧");
ta.append("\n");
out.writeUTF("nak");
}
}
}catch(IOException e1){}
}
}
public class jie
{
public static void main(String args[])
{
new Se();
}
}