📄 simpletcpserverdlg.cpp
字号:
// SimpleTcpServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SimpleTcpServer.h"
#include "SimpleTcpServerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
//CSimpleTcpClientDlg-〉CDialog-〉CWnd-〉CCmdTarget
// CSimpleTcpClientApp-〉CWinApp-〉CWinThread-〉CCmdTarget
//updata自动调用下列函数
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimpleTcpServerDlg dialog
CSimpleTcpServerDlg::CSimpleTcpServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimpleTcpServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSimpleTcpServerDlg)
// 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);
exit=false;
/////// m_list.ResetContent();//删除listbox里的所有内容
//UpdateData(true);//当参数为true:是更新数据,用于ok,cancel时数据不能更新;
//当参数为false:用于在对话框显示前,初始化对话框
//******* 以上二命令不能再次运行,可能因为初始化时尚未建立
}
void CSimpleTcpServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSimpleTcpServerDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSimpleTcpServerDlg, CDialog)
//{{AFX_MSG_MAP(CSimpleTcpServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimpleTcpServerDlg message handlers
//DoModal调用此函数
BOOL CSimpleTcpServerDlg::OnInitDialog()
{//DoModal时传WM_INITDIALOG 消息给此函数,并调用此函数
//窗口通过标准全局对话框过程调用此函数,不需要为此函数设消息映射
CDialog::OnInitDialog(); //此函数需先调用基类函数
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
//参数为false 时菜单可改,返回菜单句柄,true恢复为默认菜单
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSimpleTcpServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CSimpleTcpServerDlg::OnPaint()
{
if (IsIconic())//指定窗口是否被最小化
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
//sendmessage(n1,n2,n3):n1消息名称,n2、n3附加信息。此函数
//调用窗口过程,直到窗口过程处理完消息才返回。
//wm_iconerasebkgnd:在画图标前,图标的背景必须被填充,框架为图标的
//最小化窗口调用此函数。图标类必须被定义成窗口的默认实现。
//getsafeHdc返回Output设备上下文
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);//把CWnd客户区的坐标拷贝进rect;
//左上(0,0),右下为宽度和高度
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 CSimpleTcpServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSimpleTcpServerDlg::OnOK()
{ m_list.ResetContent();//删除listbox里的所有内容
UpdateData(true);//当参数为true:是更新数据,用于ok,cancel时数据不能更新;
//当参数为false:用于在对话框显示前,初始化对话框
//*********此二命令在此运行,说明时间应该在此时才可运行
AfxBeginThread(StartDownload,this);
}
UINT CSimpleTcpServerDlg::StartDownload(LPVOID pParam )
{
CSimpleTcpServerDlg* pDlg=(CSimpleTcpServerDlg*)pParam;
pDlg->Connect();
return 0;
}
void CSimpleTcpServerDlg::Connect()
{ CString s;
SOCKET server;
WSADATA wsaData;
sockaddr_in local;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret!=0)
{ s.Format("Initialized Winsock Failed.");
m_list.AddString(s);
return ;
}
s.Format("Initialized Winsock Successfully.");
m_list.AddString(s);
local.sin_family=AF_INET; // 地址家族Address family
local.sin_addr.s_addr=INADDR_ANY; // 要求使用当前主机配置的所有IP地址
local.sin_port=htons((u_short)20248); // 使用的端口
server=socket(AF_INET,SOCK_STREAM,0);
if(server==INVALID_SOCKET)
{ s.Format("Initialized server socket Failed.");
m_list.AddString(s);
return ;
}
s.Format("Initialized server socket Successfully.");
m_list.AddString(s);
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{ s.Format("Bind server socket Failed.");
m_list.AddString(s);
closesocket(server);
WSACleanup();
return ;
}
s.Format("Server socket has been bond successfully.");
m_list.AddString(s);
if(listen(server,10)!=0)
{ s.Format("Server socket Failed to listen.");
m_list.AddString(s);
closesocket(server);
WSACleanup();
return ;
}
s.Format("Server socket is listening.");
m_list.AddString(s);
SOCKET client;
sockaddr_in from;
int error=0;
int fromlen=sizeof(from);
while (!exit)
{
// accept() 将接受到了的客户连接
client=accept(server,
(struct sockaddr*)&from,&fromlen);
if (client==SOCKET_ERROR){
TRACE("Accept Error:%d\n",(error=WSAGetLastError()));
s.Format("Client socket connected is error.");
m_list.AddString(s);
closesocket(client);
closesocket(server);
WSACleanup();
return ;
}
s.Format("Accept a client.");
m_list.AddString(s);
TransferFile(client);
closesocket(client);
}
closesocket(server);
WSACleanup();
s.Format("Socket has been closed.You can close the window.");
m_list.AddString(s);
}
void CSimpleTcpServerDlg::TransferFile(SOCKET clientsocket)
{
char buff[512];
CString cmd;
CString filename;
CString s;
SOCKET client=clientsocket;
CFile f;
int n;
if (!exit)
{
n=recv(client,buff,512,0);//接收请求,
if(n==SOCKET_ERROR )
{ s.Format("!!!Transferring the filename fails.");
m_list.AddString(s);
strcpy(buff,"!!!Transferring the filename fails.");
send(client,buff,strlen(buff),0);//发送第一次接受失败
return;
}
buff[n]=0;
ParseURL(buff,filename);
BOOL p=f.Open(filename,CFile::modeRead);
int y;
int x;
if(!p){
s.Format("!!!file can't be open.");
m_list.AddString(s);
s.Format(filename);
m_list.AddString(s);
strcpy(buff,"!!!file can't be open.");
send(client,buff,strlen(buff),0);//发送文件打开失败
return ;
}
s.Format("file is being transferred...");
m_list.AddString(s);
while(!exit)
{
y=f.Read(buff,512);
x=send(client,buff,y,0);
if(y<512)
{
f.Close();
return;
}
} //while
}//if
f.Close();
return;
}
void CSimpleTcpServerDlg::ParseURL(char *str, CString &fname)
{
int n;
CString temp=str;
temp.TrimLeft();
temp.TrimRight();
n=temp.Find(' ');
fname=temp.Right(temp.GetLength()-(n+1));
}
void CSimpleTcpServerDlg::OnCancel()
{
exit=true;
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -