⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csma030390.cpp

📁 模拟Ethernet帧发送过程
💻 CPP
字号:
#include "stdafx.h"
#include "csma030390.h"
#include"cmath"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinThread *thread1,*thread2;
DWORD ID1,ID2,Bus=0;
UINT aThread(LPVOID pPram);
UINT bThread(LPVOID pPram);
using namespace std;
 
int _tmain(int argc,TCHAR*argv[],TCHAR*envp[])
{
	int nRetCode = 0;
	if(!AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0))
	{
		cerr<<_T("Fatal Error:MFC initializationfailed")<<endl;
		nRetCode=1;
	}
	else
	{
		thread1=AfxBeginThread(aThread,NULL);
		ID1=thread1->m_nThreadID;
		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;
	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("%1d Send Failure\n\n",ID1);
		 }
	 }
	 else
		 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("%d Send Failure\n\n",ID2);
		 }
	 }
	 else
		 goto Loop;
	 return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -