📄 filesenderdlg.cpp
字号:
// FileSenderDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileSender.h"
#include "FileSenderDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
const int SERVER_PORT = 8000;
const int IPADDR_LENGTH = 15;
const int MAX_BLOCKSIZE = 1024;
DWORD WINAPI FileTransfer(LPVOID lpParam)
{
CFileSenderDlg *dlg = reinterpret_cast<CFileSenderDlg *>(lpParam);
// bool m_bOnLine = false;
size_t readsize;
size_t sentsize = 0;
DWORD size;
FILE *fp;
SOCKET s;
//open file
char filename[MAX_PATH];
dlg->m_pFile->GetWindowText(filename, MAX_PATH);
WIN32_FIND_DATA wfd;
HANDLE hFindFile = FindFirstFile(filename, &wfd);
if (INVALID_HANDLE_VALUE == hFindFile)
{
dlg->MessageBox("File not exist", "Error", MB_OK|MB_ICONEXCLAMATION);
goto end_thread;
}
//here, i consider the file is shorter than 2GB
size = wfd.nFileSizeLow;
size = size;
fp = fopen(filename, "rb");
if (NULL == fp)
{
dlg->MessageBox("Unable to open file", "Error", MB_OK|MB_ICONEXCLAMATION);
goto end_thread;
}
//start socket
// s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
s = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
// server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET)
{
dlg->MessageBox("Unable to create socket", "Error", MB_OK|MB_ICONEXCLAMATION);
goto end_thread;
}
//////////chxli gai
//connect to the server
char szIP[IPADDR_LENGTH + 1];
dlg->m_pIP->GetWindowText(szIP, IPADDR_LENGTH);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_addr.s_addr = inet_addr(szIP);
sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);
if (connect(s, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)))
{
dlg->MessageBox("Unable to connect to server", "Error", MB_OK|MB_ICONEXCLAMATION);
closesocket(s);
goto end_thread;
}
//////////listen ,wait connect ,make sure it and send file
/*
if(server_socket.listen() == false)
{
dlg->MessageBox("Socket Listen Error");
return;
}
SetWindowText("正在监听连接!");
if(server_socket.Accept(s) == FALSE)
{
AfxMessageBox("Socket Accept Error");
return;
}
// m_bOnLine = TRUE;
SetWindowText("有客户连接");
*/
//send the file length
send(s, reinterpret_cast<char *>(&size), sizeof(DWORD), 0);
char readbuf[MAX_BLOCKSIZE];
while (!feof(fp))
{
readsize = fread(readbuf, 1, MAX_BLOCKSIZE, fp);
//convert little-endian to big-endian
for (size_t j=0; j<readsize; j+=4)
{
*reinterpret_cast<long *>(readbuf + j) = htonl(*reinterpret_cast<long *>(readbuf + j));
}
// send(s, reinterpret_cast<char *>(&sentsize), sizeof(DWORD), 0);
send(s, readbuf, static_cast<int>(readsize), 0);
Sleep(0.5);
sentsize += readsize;
dlg->m_pProgress->SetPos(static_cast<int>(
static_cast<float>(sentsize) / static_cast<float>(size) * 100));
}
end_thread:
dlg->m_pBrowse->EnableWindow();
dlg->m_pSend->EnableWindow();
dlg->m_pIP->EnableWindow();
dlg->m_pFile->EnableWindow();
return 0;
}
// CFileSenderDlg dialog
CFileSenderDlg::CFileSenderDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileSenderDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileSenderDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CFileSenderDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDBROWSE, CFileSenderDlg::OnBnClickedBrowse)
ON_WM_CLOSE()
ON_BN_CLICKED(IDSEND, CFileSenderDlg::OnBnClickedSend)
END_MESSAGE_MAP()
// CFileSenderDlg message handlers
BOOL CFileSenderDlg::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_pIP = (CEdit *)GetDlgItem(IDC_IP);
m_pFile = (CEdit *)GetDlgItem(IDC_FILE);
m_pSend = (CButton *)GetDlgItem(IDSEND);
m_pBrowse = (CButton *)GetDlgItem(IDBROWSE);
m_pProgress = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS);
//WinSock Startup
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa) != 0)
{
MessageBox("Unable to initialize WinSock", "Error", MB_OK|MB_ICONEXCLAMATION);
exit(-1);
}
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 CFileSenderDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFileSenderDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CFileSenderDlg::OnBnClickedBrowse()
{
// TODO: Add your control notification handler code here
TCHAR szFilters[] = "All Files (*.*)|*.*||";
CFileDialog opendialog(TRUE, NULL, NULL,
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, szFilters, this);
if (IDOK == opendialog.DoModal())
{
m_pFile->SetWindowText(opendialog.GetPathName());
}
}
void CFileSenderDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
WSACleanup();
CDialog::OnClose();
}
void CFileSenderDlg::OnBnClickedSend()
{
// TODO: Add your control notification handler code here
//start a new thread for file transferring
m_pBrowse->EnableWindow(FALSE);
m_pSend->EnableWindow(FALSE);
m_pIP->EnableWindow(FALSE);
m_pFile->EnableWindow(FALSE);
CreateThread(NULL, 0, FileTransfer, this, 0, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -