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

📄 clientdlg.cpp

📁 在两个开发板上实现文件的循环收发
💻 CPP
字号:
// ClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"

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


#define		FILESIZE		2667636
#define		BUFSIZE			4096

/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog

DWORD g_ListCount;
DWORD g_RecvTimes;
DWORD g_SendTimes;
DWORD g_nCount;

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

void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CClientDlg)
	DDX_Control(pDX, IDC_DISPLAY_LIST, m_sys_msg);
	DDX_Text(pDX, IDC_REMOTE_ADDR, m_strRemoteAddr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
	//{{AFX_MSG_MAP(CClientDlg)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_SEND_FILE, OnSendFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CClientDlg::OnConnect() 
{
	GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);
	
	//初始化套接字库
	if (Initwinsock() == FALSE)
	{
		AfxMessageBox(L"Init socket error!");
		return;
	}
	CString str;
	str.Format(L"Init socket successful!");
	m_sys_msg.InsertString(g_ListCount++,str);
	
	//连接服务器	
	int len = m_strRemoteAddr.GetLength();
	char *remoteip = new char[len];
	for (int i=0;i<len;i++)
	{
		remoteip[i] = m_strRemoteAddr.GetAt(i);
	}

	SOCKADDR_IN addSrv;
	addSrv.sin_addr.s_addr = inet_addr(remoteip);
	addSrv.sin_family = AF_INET;
	addSrv.sin_port   = htons(6000);
	
	ssocket = socket(AF_INET,SOCK_STREAM,0);
	
	if (connect(ssocket,(SOCKADDR*)&addSrv,sizeof(SOCKADDR)) == SOCKET_ERROR)
	{
		closesocket(ssocket);		
		AfxMessageBox(L"connect Server error!");		
		return;
	}

	str.Format(L"Connect %s Successful..",m_strRemoteAddr);
	m_sys_msg.InsertString(g_ListCount++,str);

}

void CClientDlg::OnSendFile() 
{
	GetDlgItem(IDC_SEND_FILE)->EnableWindow(FALSE);

	HANDLE hSendFileThread = CreateThread(NULL,0,SendFileThread,this,0,NULL);
	CloseHandle(hSendFileThread);
}

BOOL CClientDlg::Initwinsock()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	
	wVersionRequested=MAKEWORD(2,2);
	
	if(WSAStartup(wVersionRequested,&wsaData)==0)
	{
		return TRUE;
	}
	else
	{
		int err = WSAGetLastError();
		CString str;
		str.Format(L"%d",err);
		AfxMessageBox(str);

		WSACleanup();
		return FALSE;
	}
}


DWORD CClientDlg::SendFileThread(LPVOID lparam)
{
	CClientDlg *pDlg = (CClientDlg*)lparam;

	CFile sendFile;
	if (!sendFile.Open(L"\\NPU_LAN_SEND.zip",CFile::modeRead | CFile::typeBinary))
	{
		AfxMessageBox(L"Open send file error!");
		return FALSE;
	}

	//发送文件
	DWORD dwRead = 0;
	char sendBuf[BUFSIZE] = {0};
	
	while (dwRead < FILESIZE)
	{
		UINT dw = sendFile.Read(sendBuf,BUFSIZE);
		if ((send(pDlg->ssocket,(const char*)sendBuf,dw,0)) == SOCKET_ERROR)
		{
			closesocket(pDlg->ssocket);
			return FALSE;
		}
		dwRead += dw;
	}
	sendFile.Close();
	
	pDlg->GetDlgItem(IDC_SEND_FILE)->EnableWindow(TRUE);

	CString str;
	str.Format(L"send file %ld successful",++g_SendTimes);
	pDlg->m_sys_msg.InsertString(g_ListCount++,str);
	g_nCount = pDlg->m_sys_msg.GetCount();
	pDlg->m_sys_msg.SetCurSel(g_nCount-1);

	pDlg->RecvFile();

	return TRUE;
}

void CClientDlg::RecvFile()
{
	HANDLE hRecvFileThread = CreateThread(NULL,0,RecvFileThread,this,0,NULL);
	CloseHandle(hRecvFileThread);
}


DWORD CClientDlg::RecvFileThread(LPVOID lparam)
{
	CClientDlg *pDlg = (CClientDlg*)lparam;

	CFile recvFile;
	if (!recvFile.Open(L"\\NPU_LAN_SEND.zip",CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
	{
		AfxMessageBox(L"Open recv file error!");
		return FALSE;
	}
	
	//接收文件
	DWORD dwRecv = 0;
	char recvBuf[BUFSIZE] = {0};

	while (dwRecv < FILESIZE)
	{
		UINT dw = recv(pDlg->ssocket,recvBuf,BUFSIZE,0);
		recvFile.Write(recvBuf,dw);		
		dwRecv += dw;
	}
	recvFile.Close();
	
	CString str;
	str.Format(L"Recv File %ld Successful!",++g_RecvTimes);

	pDlg->m_sys_msg.InsertString(g_ListCount++,str);
	g_nCount = pDlg->m_sys_msg.GetCount();
	pDlg->m_sys_msg.SetCurSel(g_nCount-1);

	pDlg->OnSendFile();
	
	return TRUE;
}

⌨️ 快捷键说明

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