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

📄 remotesimdlg.cpp

📁 此文件包含两个独立程序,Sender和Receiver,用于测试网络对RAW类型的IP包的传输
💻 CPP
字号:
// RemoteSimDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RemoteSim.h"
#include "RemoteSimDlg.h"
#include "process.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define RTP_VER 2
#define RTP_PT 77
#define RTP_DELAYTHREHOLD 0xfff0


/////////////////////////////////////////////////////////////////////////////
// CRemoteSimDlg dialog
char g_filename[100];
char g_strIP[20];
char g_strIP1[20];
char g_strIP_to[20];
int g_nProtocol;

int g_PacketReceived=0;
unsigned long g_PacketLost=0;

bool g_bStarted=false;
bool g_bStop=false;

/*void receivethread(LPVOID lpParameter);*/

CRemoteSimDlg::CRemoteSimDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRemoteSimDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRemoteSimDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRemoteSimDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRemoteSimDlg)
		DDX_Text(pDX, IDC_E_IPADDRESS, m_strIP);
		DDX_Text(pDX, IDC_E_IPADDRESS2, m_nProtocol);
		DDX_Text(pDX, IDC_E_LENGTH, m_nLength);
		DDX_Text(pDX, IDC_E_TIMER, m_nTimer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRemoteSimDlg, CDialog)
	//{{AFX_MSG_MAP(CRemoteSimDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_B_CONNECT, OnBnClickedBConnect)
	ON_WM_TIMER()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteSimDlg message handlers

BOOL CRemoteSimDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
	m_strIP="225.1.1.11";
	//m_strIP1="172.96.33.132";
	//sprintf(g_filename,"test.mpg");
	sprintf(g_strIP,(LPCSTR)m_strIP);
	//sprintf(g_strIP1,(LPCSTR)m_strIP1);
	//sprintf(g_strIP_to,"225.1.1.12");
	m_nProtocol=108;
	m_nLength=16*1024;
	m_nTimer=100;
	m_Message.UNION.rtpData.rtpHeader.v=RTP_VER;
	m_Message.UNION.rtpData.rtpHeader.p=0;
	m_Message.UNION.rtpData.rtpHeader.x=0;
	m_Message.UNION.rtpData.rtpHeader.cc=0;
	m_Message.UNION.rtpData.rtpHeader.m=1;
	m_Message.UNION.rtpData.rtpHeader.pt=RTP_PT;
	UpdateData(FALSE);
	SetTimer(100,2000,NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRemoteSimDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRemoteSimDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CRemoteSimDlg::OnBnClickedBConnect()
{
	UpdateData(TRUE);
	sprintf(g_strIP,(LPCSTR)m_strIP);
	g_nProtocol=m_nProtocol;
	if (m_nLength>=16*1024)
		m_nLength=16*1024;
	if (m_nLength<=0)
		m_nLength=1;
	if (m_nTimer<0)
		m_nTimer=1;
	if (m_nTimer>10000)
		m_nTimer=10000;
	//sprintf(g_strIP1,(LPCSTR)m_strIP1);
	static int bStart=0;
	if (bStart==0)
	{
		int err;
	//CFile file;
	
	//file.Open(g_filename,CFile::modeCreate|CFile::modeReadWrite);

	m_s=socket(AF_INET,SOCK_RAW,g_nProtocol);
	
	if (m_s==INVALID_SOCKET)
		::MessageBox(NULL,"Create Mulicast error!","Loopback",MB_OK);
	//if (stcp==INVALID_SOCKET)
	//	MessageBox(NULL,"Create Mulicast error!","Loopback",MB_OK);

	struct sockaddr_in address;
	address.sin_family=AF_INET;
	address.sin_addr.s_addr=htonl(INADDR_ANY);
	address.sin_port=htons(0);

	err=bind(m_s,(struct sockaddr *)&address,sizeof(address));
	if (err!=0)
		::MessageBox(NULL,"Bind socker error!","Loopback",MB_OK);
	//err=bind(stcp,(struct sockaddr *)&address,sizeof(address));
	//if (err!=0)
	//	MessageBox(NULL,"Bind socker error!","Loopback",MB_OK);

	
	ZeroMemory(&m_to,sizeof(m_to));

	m_to.sin_family=AF_INET;
	m_to.sin_addr.s_addr=inet_addr(g_strIP);
	
	//err=connect(stcp,(struct sockaddr *)&to,sizeof(to));
    //if (err!=0)
	//	MessageBox(NULL,"connect error!","Loopback",MB_OK);
	bStart=1;
	SetTimer(101,m_nTimer,NULL);
	}
	else
	{
		closesocket(m_s);
		bStart=0;
		KillTimer(101);
	}
}

void CRemoteSimDlg::OnTimer(UINT nIDEvent)
{
	static unsigned short sn=0;
	static unsigned int timeStamp=0;
	if (nIDEvent==100)
	{
		sprintf(m_strTemp,"%d",g_PacketReceived);
		GetDlgItem(IDC_PACKET_RECEIVED)->SetWindowText(m_strTemp);
		sprintf(m_strTemp,"%d",g_PacketLost);
		GetDlgItem(IDC_PACKET_LOST)->SetWindowText(m_strTemp);
	}
	else if (nIDEvent==101)
	{
		m_Message.dataLen=m_nLength+sizeof(struct RTPHEADER);
		m_Message.UNION.rtpData.rtpHeader.sn=htons(sn);
		m_Message.UNION.rtpData.rtpHeader.timestamp=htonl(timeStamp);
		timeStamp++;
		sn++;
		int rtCode=sendto(m_s,(char *)(m_Message.UNION.data),m_Message.dataLen,
						0,(struct sockaddr *)&m_to,sizeof(m_to));
		if (rtCode==-1||rtCode!=m_Message.dataLen)
			g_PacketLost++;
		else
			g_PacketReceived++;
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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