📄 eadf.txt
字号:
* @(#)Ethernet.java
*function :模拟帧的发送过程
*
* @author :seaman
* @version 1.00
import java.util.* ;
import java.lang.Math.* ;
public class Ethernet extends Thread
{
static long Bus=0;
static int CollisionCounter = 20; //最大重试次数
static double collisionWindow = 0.005; //冲突窗口大小
static int randNum = (int)(100 * Math.random() % 3 ); //随机数
public static void main(String args[])
{
A_Host a = new A_Host() ;
B_Host b = new B_Host() ;
A_Host.ID1 = a.getId() ;
a.start() ;
B_Host.ID2 = b.getId() ;
b.start() ;
}
}
class A_Host extends Thread
{
static long ID1 ; //线程a的线程标示
int times_a = 0 ; //发送成功次数计数器
int CollisionCounter =20; //冲突计数器最大计数初值为20
public void run()
{
A_send() ;
}
public boolean A_send()
{
boolean flg = false ; //发送成功标志
Ethernet.Bus= Ethernet.Bus| ID1; //模拟数据发送
try
{
sleep(15); //休眠15毫秒
}
catch(InterruptedException e)
{
System.out.println("线程A休眠发生错误!");
}
if(Ethernet.Bus == ID1) //无冲突
{
System.out.println(ID1 + " ID Send Success ");
flg = true ;
Ethernet.Bus=0; //发送成功后之总线空闲
CollisionCounter = 20; //恢复冲突计数器的内容
try
{
sleep((int)(100 * Math.random() % 10)); //随机延时
}
catch(InterruptedException e)
{
System.out.println("线程A休眠发生错误!");
}
times_a ++; //发送成功次数加1
System.out.println("主机a发送成功次数= " + times_a);
while ( times_a < 10) //发送成功次数不足十次时,继续发送
{
A_send() ;
}
}
else //有冲突时
{
System.out.println(ID1 + " ID Send Collision " + (17 - CollisionCounter) + "次");
Ethernet.Bus = 0; //产生冲突后,置总线空闲
CollisionCounter --; //冲突计数器内容减1
if(CollisionCounter > 0)
{
int temp =(int) Math.pow( 2.0,(CollisionCounter>10)?10.0:(double)CollisionCounter );
try
{
//截止二进制指数退避算法延时
sleep(Ethernet.randNum * temp * (int)(Ethernet.collisionWindow * 10) );
sleep(Ethernet.randNum*(int)Math.pow(2,(CollisionCounter > 10)?10:CollisionCounter) * Ethernet.collisionWindow) ;
}
catch(InterruptedException e)
{
System.out.println("线程A休眠发生错误!");
}
A_send() ; //尝试重新发送数据
}
else
{
System.out.println(ID1 + "ID Send Failuer "); //发送失败!
flg = false ;
}
}
return flg ;
}
}
class B_Host extends Thread
{
static long ID2 ;
int times_b = 0 ;
int CollisionCounter =20 ;
public void run()
{
B_send() ;
}
public boolean B_send()
{
boolean flg = false ;
Ethernet.Bus= Ethernet.Bus| ID2; //模拟数据发送
try
{
sleep(10);
}
catch(InterruptedException e)
{
System.out.println("线程B休眠发生错误!");
}
if(Ethernet.Bus == ID2) //无冲突
{
System.out.println(ID2 + " ID Send Success ");
flg = true ;
Ethernet.Bus=0;
CollisionCounter = 20; //恢复冲突计数器的内容
try
{
sleep((int)(100 * Math.random() % 10)); //随机延时
}
catch(InterruptedException e)
{
System.out.println("线程B休眠发生错误!");
}
times_b ++; //发送成功次数加1
System.out.println("主机b发送成功次数= " + times_b);
while ( times_b < 10) //发送成功次数不足十次时,继续发送
{
B_send() ;
}
}
else //有冲突
{
System.out.println(ID2 + "ID Send Collision "+ (17-CollisionCounter) + "次");
Ethernet.Bus= 0;
CollisionCounter --;
if(CollisionCounter > 0)
{ //二进制退避算法延时
int temp =(int) Math.pow( 2.0,(CollisionCounter>10)?10.0:(double)CollisionCounter );
try
{
sleep(Ethernet.randNum * temp * (int)(Ethernet.collisionWindow * 10) );
sleep(Ethernet.randNum*(int)Math.pow(2,(CollisionCounter > 10)?10:CollisionCounter) * Ethernet.collisionWindow) ;
}
catch(InterruptedException e)
{
System.out.println("线程A休眠发生错误!");
}
B_send() ;
}
else
{
System.out.println(ID2 + "ID Send Failuer ");
flg = false ;
}
}
return flg ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -