📄 serverdlg.cpp
字号:
// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include <Winsock2.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define FILESIZE 2667636
#define BUFSIZE 4096
#define WM_MYMESSAGE (WM_USER + 100)
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
DWORD g_ListCount;
DWORD g_RecvTimes;
DWORD g_SendTimes;
DWORD g_nCount;
typedef struct _SOCKET_STREAM_FILE_INFO {
TCHAR szFileTitle[128]; //文件的标题名
DWORD dwFileAttributes; //文件的属性
FILETIME ftCreationTime; //文件的创建时间
FILETIME ftLastAccessTime; //文件的最后访问时间
FILETIME ftLastWriteTime; //文件的最后修改时间
DWORD nFileSizeHigh; //文件大小的高位双字
DWORD nFileSizeLow; //文件大小的低位双字
DWORD dwReserved0; //保留,为0
DWORD dwReserved1; //保留,为0
} SOCKET_STREAM_FILE_INFO, * PSOCKET_STREAM_FILE_INFO;
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CServerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Control(pDX, IDC_LIST_DISPLAY, m_sys_msg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_BN_CLICKED(IDC_STATRT, OnStatrt)
ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
BOOL CServerDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CServerDlg::OnStatrt()
{
HANDLE h_ListenThread = CreateThread(NULL,0,ListrenThread,this,NULL,0);
CloseHandle(h_ListenThread);
GetDlgItem(IDC_STATRT)->EnableWindow(FALSE);
CString str;
str.Format(L"Server is Running.....");
m_sys_msg.InsertString(g_ListCount++,str);
}
void CServerDlg::OnCancel()
{
CDialog::OnCancel();
}
DWORD CServerDlg::ListrenThread(LPVOID lparam)
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested=MAKEWORD(1,1);
if(WSAStartup(wVersionRequested,&wsaData)!=0)
{
AfxMessageBox(L"Init socket error!");
return FALSE;
}
//监听
SOCKET slisten;
SOCKADDR_IN addlisten;
char *ip;
CString tmp;
CServerDlg *pDlg = (CServerDlg*)AfxGetApp()->GetMainWnd();
//创建套节字
if((slisten = socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
{
closesocket(slisten);
AfxMessageBox(L"Create Socket error!");
return FALSE;
}
//绑定主机
addlisten.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addlisten.sin_family = AF_INET;
addlisten.sin_port = htons(6000);
if ((bind(slisten,(SOCKADDR*)&addlisten,sizeof(SOCKADDR))) == SOCKET_ERROR)
{
closesocket(slisten);
AfxMessageBox(L"bind Socket error!");
return FALSE;
}
//监听
if(listen(slisten,5)!= 0)
{
closesocket(slisten);
AfxMessageBox(L"listen before error!");
return FALSE;
}
while (TRUE)
{
int len = sizeof(SOCKADDR);
if((pDlg->sAccept = accept(slisten,(SOCKADDR*)&addlisten,&len)) == INVALID_SOCKET)
{
closesocket(pDlg->sAccept);
AfxMessageBox(L"can not accept Client request!");
return FALSE;
}
ip = inet_ntoa(addlisten.sin_addr);
HANDLE hRecvThread = CreateThread(NULL,0,RecvFileThread,(LPVOID)(pDlg->sAccept),0,NULL);
CloseHandle(hRecvThread);
}
return TRUE;
}
DWORD CServerDlg::RecvFileThread(LPVOID lparam)
{
CServerDlg *pDlg = (CServerDlg*)AfxGetApp()->GetMainWnd();
//监听主线程传来的用户套接字
SOCKET recvSocket = (SOCKET)lparam;
CString str;
//接收文件数据
CFile recvFile;
if (!recvFile.Open(L"\\NPU_LAN_RECV.zip",CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
{
AfxMessageBox(L"open recv file error!");
return FALSE;
}
DWORD dwRead = 0;
char data[BUFSIZE] = {0};
while (dwRead < FILESIZE)
{
UINT dw = recv(recvSocket,data,BUFSIZE,0);
recvFile.Write(data,dw);
dwRead += dw;
}
recvFile.Close();
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);
// closesocket(recvSocket);
pDlg->SendFile();
return TRUE;
}
void CServerDlg::SendFile()
{
HANDLE hSendFileThread = CreateThread(NULL,0,SendFileThread,this,0,NULL);
CloseHandle(hSendFileThread);
}
DWORD CServerDlg::SendFileThread(LPVOID lparam)
{
CServerDlg *pDlg = (CServerDlg*)lparam;
CFile sendFile;
if (!sendFile.Open(L"\\NPU_LAN_RECV.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->sAccept,(const char*)sendBuf,dw,0)) == SOCKET_ERROR)
{
closesocket(pDlg->sAccept);
return FALSE;
}
dwRead += dw;
}
sendFile.Close();
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 CServerDlg::RecvFile()
{
HANDLE hRecvThread = CreateThread(NULL,0,RecvFileThread,(LPVOID)sAccept,0,NULL);
CloseHandle(hRecvThread);
}
void CServerDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
RecvFile();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -