模拟Ethernet帧的发送
来源:互联网 发布:决战武林灵武升阶数据 编辑:程序博客网 时间:2024/04/29 22:53
// Ethernet.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Ethernet.h"
//#include "CSMA030390.h"
#include "cmath"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinThread *thread1,*thread2;//定义变量
DWORD ID1,ID2,Bus=0;//初始化共享内存
UINT aThread(LPVOID pParam);//线程A代表主机A
UINT bThread(LPVOID pParam);//线程B代表主机B
using namespace std;
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
//CString strHello;
//strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
thread1=AfxBeginThread(aThread,NULL);//启动线程A
ID1=thread1->m_nThreadID;//线程A的ID号
thread2=AfxBeginThread(bThread,NULL);
ID2=thread2->m_nThreadID;
getchar();
}
return nRetCode;
}
UINT aThread(LPVOID pParam)
{
int i=0;
int CollisionCounter=16;//冲突次数
double collisionWindow=0.005;//冲突窗口取0.005
int randNum=rand()%3;//随机数
Loop:if(Bus==0)
{
Bus=Bus|ID1;//或符号
Sleep(12);
if(Bus==ID1)
{
printf("%d Send Success/n/n",ID1);
Bus=0;
CollisionCounter=16;
Sleep(rand()%10);
i++;
printf("主机A发送成功次数=%d/n/n",i);
if(i<10)
goto Loop;
}
else
{
printf("%d Send Collision/n/n",ID1);
Bus=0;
CollisionCounter--;
if(CollisionCounter>0)
{
Sleep(randNum*(int)pow(2,(CollisionCounter>10)?10:CollisionCounter)*collisionWindow);
goto Loop;
}
else
printf("%ld send Failure/n/n",ID1);
}
}
goto Loop;
return 0;
}
UINT bThread(LPVOID pParam)
{
int j=0;
int CollisionCounter=16;
double collisionWindow=0.005;
int randNum=rand()%3;
Loop:if(Bus==0)
{
Sleep(2);
Bus=Bus|ID2;
Sleep(3);
if(Bus==ID2)
{
printf("%d Send Success/n/n",ID2);
Bus=0;
CollisionCounter=16;
Sleep(rand()%10);
j++;
printf("主机B发送成功次数=%d/n/n",j);
if(j<10)
goto Loop;
}
else
{
printf("%d Send Collision/n/n",ID2);
Bus=0;
CollisionCounter--;
if(CollisionCounter>0)
{
Sleep(randNum*(int)pow(2,(CollisionCounter>10)?10:CollisionCounter)*collisionWindow);
goto Loop;
}
else
printf("%ld send Failure/n/n",ID2);
}
}
goto Loop;
return 0;
}
- 模拟Ethernet帧的发送
- Windows C++发送以太网(Ethernet)帧
- Ethernet的帧格式
- Ethernet的帧格式
- 浅谈Ethernet的帧格式
- Ethernet的帧格式 (转)
- 如何使用Windows C++发送以太网(Ethernet)帧
- 以太网(Ethernet)的帧结构
- Ethernet/Fast Ethernet 的概念问题
- Ethernet那些帧
- Ethernet II帧格式
- Ethernet帧格式
- Ethernet帧格式
- Ethernet帧格式
- ETHERNET帧结构
- Ethernet
- ethernet
- ethernet
- SQL:JOIN之完全用法
- 在.NET 3.5 平台上使用LINQ to SQL创建三层/多层Web应用系统(源代码下载和PDF文档下载)
- SQL中cross join,left join,right join ,full join,inner join 的区别
- FLEX实践:DATAGRID中添加DOUBLECLICK事件
- Grails Web开发,测试过程中修改页面后,报错,返回修改前,依然报原来错误.
- 模拟Ethernet帧的发送
- 解析ARP数据包
- ulimit用法
- asp.net fileupload
- VS2008 快捷键大全
- 解析IP数据包
- 如何建立 iPhone 开发环境(中文windows)上部 (吐血推荐)
- C/C++
- 在windows右键菜单中加入自己的程序