📄 testudpclientld.java
字号:
import java.net.*;
import java.io.*;
public class TestUDPClientld
{
public static void main(String args[]) throws Exception
{
Client Try = new Client();
Try.Execute();
}
}
class Client
{
int b,j=1,n=2;//n表示滑动窗口数目为2.j表示读进来的字节存储在data中的位置.b用来保存当前读入的字节.
byte buf1[] = new byte[1024];//用于发送的缓冲区
byte buf2[] = new byte[1024];//存储刚发送的数据,丢包时重新发送步buf2
byte buf3[] = new byte[1024];//用于接受server返回消息.
char[] data = new char[50];//接受读入数据
char k1='\u0001';//buf1包中的序号
char k2='\u0001';//buf2包中的序号
FileInputStream in = null;
DatagramSocket ds = null;
int m;//设定的丢包概率
int qingkuang;//设置丢包情况:1为两包都丢,2为1包丢,3为2包丢,4为都不丢
int t1;//包1状态
int t2;//包2状态
Client() throws Exception
{
in = new FileInputStream("C:/Documents and Settings/sc_admin/桌面/test1.txt") ;
ds = new DatagramSocket(9999);
}
void Execute() throws Exception
{
qingkuang = 3;
switch(qingkuang)
{
case 1:{t1=0;t2=0;break;}
case 2:{t1=0;t2=1;break;}
case 3:{t1=1;t2=0;break;}
case 4:{t1=1;t2=1;break;}
}
try
{
while((b=in.read())!= -1)
{
if(j !=41)
{
data[j++] = (char)b;
}
else
{
//把包装好
SendPacket();
if(n==0)//收消息
Receive();
j=1;//重新将读入字节存储到data中
data[j++] = (char)b;
k1++;
}
}//while
//最后一个包
FinalPacket();
in.close();
}
catch(FileNotFoundException e2)
{
System.out.println("File Not Found");
System.exit(-1);
}
catch(IOException e1)
{
System.out.println("File Copy error");
System.exit(-1);
}
}//Execcute
void SendPacket() throws Exception
{//将封装好的包发给Server
//int probability;//要发送包的丢包概率
//probability=(int)(Math.random()*100);
data[0] = k1;//加入序号.
String str = new String(data);
buf1 = str.getBytes();
DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length, new InetSocketAddress("127.0.0.1", 5678));
if(n!=0)
{//probability>=m
if(t1==1)
{
System.out.println((int)k1+"传递包1");
ds.send(dp1);
}
else
{
System.out.println((int)k1+"不传递包1");
}
if(n==2)
{
Save(buf2,buf1);
k2=k1;
}
n--;
}
}//SendPacket
void Save(byte[] a,byte[] b)
{
for(int j = 0;j<50;j++)
a[j]=b[j];
}//Save
void Receive() throws Exception
{
DatagramPacket dp2 = new DatagramPacket(buf3,buf3.length);
while(true)
{
// System.out.println("dengdai");
ds.setSoTimeout(1150);
try
{// System.out.println(buf3[0]);
// System.out.println((int)k1);
ds.receive(dp2);
}
catch(SocketTimeoutException ex)
{}
// System.out.println("yiguo");
if(buf3[0]==(int)k2)
{
if(k2=='\u0000')
System.out.println("最后一包已经到达");
else
System.out.println("包"+(int)k2+"已经到达");
if(k1==k2)
break;
k2++;
ds.setSoTimeout(5);
try
{
ds.receive(dp2);
}
catch(SocketTimeoutException ex)
{}
if(buf3[0]==(int)k1)
{
System.out.println("包"+(int)k2+"已经到达");
break;
}
else
{
DatagramPacket dp4 = new DatagramPacket(buf1, buf1.length, new InetSocketAddress("127.0.0.1", 5678));
ds.send(dp4);
System.out.println("重传了"+(int)k2);
if(k1=='\u0000')
k2=k1;
}
}
else
{
DatagramPacket dp3 = new DatagramPacket(buf2, buf2.length, new InetSocketAddress("127.0.0.1", 5678));
System.out.println("重传了"+(int)k2);
ds.send(dp3);
DatagramPacket dp4 = new DatagramPacket(buf1, buf1.length, new InetSocketAddress("127.0.0.1", 5678));
System.out.println("重传了"+(int)(k2+1));
ds.send(dp4);
}
}
n=2;
}//Receive
void FinalPacket() throws Exception
{
j=j-1;//j-1后才表示的是data中最后一个元素的下标
if(j==40)
{
SendFinal();
if(n==1)//最后一包为偶数包
Receive();
else//最后一包为奇数包
FinalReceive();
}//1if
else
{
j++;
for(;j<=41;j++)
data[j]='\u0000';
SendFinal();
if(n==1)//最后一包为偶数包
Receive();
else//最后一包为奇数包
FinalReceive();
}//1else
}//FinalPacket
void SendFinal() throws Exception
{
//int probability;
//probability=(int)(Math.random()*100);
data[42]=k1;
k1='\u0000';
data[0] = k1;
String str = new String(data);
buf1 = str.getBytes();
DatagramPacket dp1 = new DatagramPacket(buf1, buf1.length, new InetSocketAddress("127.0.0.1", 5678));
// System.out.println("m is"+m+"k1 is"+(int)(k2+1));
if(t2==1)
{
System.out.println((int)(k2+1)+"传递包2");
ds.send(dp1);
}
else
{
System.out.println((int)(k2+1)+"不传递包2");
}
}//SendFinal
void FinalReceive() throws Exception
{
while(true)
{
DatagramPacket dp2 = new DatagramPacket(buf3,buf3.length);
// System.out.println("shouZQbao");
ds.setSoTimeout(5);
try
{// System.out.println(buf3[0]);
// System.out.println((int)k1);
ds.receive(dp2);
}
catch(SocketTimeoutException ex)
{}
if(buf3[0]==(int)k1)
{
k2++;
System.out.println("3包"+(int)k2+"已经到达");
break;
}
else
{
DatagramPacket dp3 = new DatagramPacket(buf1, buf1.length, new InetSocketAddress("127.0.0.1", 5678));
ds.send(dp3);
}
}
}//FinalReceive
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -