📄 serverdlg.cpp
字号:
// ServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileShare.h"
#include "ServerDlg.h"
#include "ThreadProc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CServerDlg dialog
static SERVICEPARM lp;
extern CStatusBarCtrl g_StatusBar;
CServerDlg::CServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CServerDlg::IDD, pParent)
{
this->m_pServiceThread = NULL;
//{{AFX_DATA_INIT(CServerDlg)
m_nPort = 0;
m_strSavePath = _T("");
m_strMessage = _T("");
//}}AFX_DATA_INIT
this->m_nLastRecv=0;
this->m_nTotalRecv=0;
}
void CServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CServerDlg)
DDX_Control(pDX, IDC_IPADDRESS, m_ctrlip);
DDX_Text(pDX, IDC_PORT, m_nPort);
DDX_Text(pDX, IDC_SAVEPATH, m_strSavePath);
DDX_Text(pDX, IDC_MESSAGE, m_strMessage);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CServerDlg, CDialog)
//{{AFX_MSG_MAP(CServerDlg)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_MESSAGE(WM_THREADMSG,OnThreadMsg)
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BROWSE_PATH, OnBrowsePath)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CServerDlg message handlers
BOOL CServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
HWND hwndCtrl;
char hostname[128];
unsigned char addr[4];
char tmp[32];
struct hostent *ht;
char sz[256];
// TODO: Add extra initialization here
hwndCtrl = this->m_ctrlip.GetSafeHwnd();
if(gethostname(hostname,sizeof(hostname)) == 0)
{
ht = gethostbyname(hostname);
if(ht)
{
memcpy(addr,ht->h_addr_list[0],4);
sprintf(hostname,"%d",(unsigned char)addr[0]);
sprintf(tmp,".%d",addr[1]);
strcat(hostname,tmp);
sprintf(tmp,".%d",addr[2]);
strcat(hostname,tmp);
sprintf(tmp,".%d",addr[3]);
strcat(hostname,tmp);
hwndCtrl = ::GetDlgItem(this->GetSafeHwnd(),IDC_IPADDRESS);
::SetWindowText(hwndCtrl,hostname);
}
}
GetPrivateProfileString(FILESHARE,SERVICEPORT,"",sz,sizeof(sz),CONFIG_FILE);
this->m_nPort=atoi(sz);
GetPrivateProfileString(FILESHARE,SAVEPATH,"",sz,sizeof(sz),CONFIG_FILE);
this->m_strSavePath=sz;
UpdateData(false);
this->SetTimer(RECV_SPEED_TIMER,TIMER_LONG,NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CServerDlg::OnStart()
{
UpdateData(TRUE);
sockaddr_in sockaddr;
DWORD ip;
this->m_sockService = socket(AF_INET,SOCK_STREAM,0);
if(m_sockService==INVALID_SOCKET)
{
AfxMessageBox("Create service socket fail.");
return;
}
this->m_ctrlip.GetAddress(ip);
sockaddr.sin_family=AF_INET;
sockaddr.sin_port=htons(this->m_nPort);
sockaddr.sin_addr.S_un.S_addr=htonl(ip);
if(bind(this->m_sockService,(struct sockaddr *)&sockaddr,sizeof(sockaddr))!=0)
{
AfxMessageBox("bind socket fail,\r\n\r\nplease change a port and try again!");
closesocket(this->m_sockService);
return;
}
if(listen(this->m_sockService,SOMAXCONN)!=0)
{
AfxMessageBox("Start service fail!");
closesocket(this->m_sockService);
return;
}
strcpy(lp.path,this->m_strSavePath.GetBuffer(1));
this->m_strSavePath.ReleaseBuffer();
lp.sockservice=this->m_sockService;
lp.hwndServerDlg = this->GetSafeHwnd();
this->m_pServiceThread=AfxBeginThread(ServiceProc,&lp);
if(this->m_pServiceThread)
{
GetDlgItem(IDC_START)->EnableWindow(false);
GetDlgItem(IDC_STOP)->EnableWindow(true);
GetDlgItem(IDC_START)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_STOP)->ShowWindow(SW_SHOW);
g_StatusBar.SetText("服务已启动",0,NULL);
}
else
{
GetDlgItem(IDC_START)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STOP)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_START)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
g_StatusBar.SetText("服务未启动",0,NULL);
}
}
void CServerDlg::OnStop()
{
closesocket(this->m_sockService);
}
void CServerDlg::OnThreadMsg(WPARAM wParm,LPARAM lParm)
{
char msg[1244];
UpdateData(true);
switch(wParm)
{
case MSG_THREADEXIT:
// GetDlgItem(IDC_SEND)->EnableWindow(true);
break;
case MSG_SHOWMSG:
strcpy(msg,(char*)lParm);
this->m_strMessage+=(char*)msg;
UpdateData(false);
break;
case MSG_SERVICE_STOP:
GetDlgItem(IDC_START)->ShowWindow(SW_SHOW);
GetDlgItem(IDC_STOP)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_START)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
m_pServiceThread=NULL;
g_StatusBar.SetText("服务未启动",0,NULL);
case MSG_RECV_DATA:
this->m_nLastRecv +=(DWORD) lParm;
break;
default:
break;
}
}
void CServerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
HWND h;
h=::GetDlgItem(this->GetSafeHwnd(),IDC_MESSAGE);
::SetWindowPos(h,NULL,0,140,cx,cy-140,0);
}
void CServerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
DWORD nSpeed;
char msg[512];
static char s1[3];
static char s2[3];
float n1,n2;
switch(nIDEvent)
{
case RECV_SPEED_TIMER:
{
nSpeed = this->m_nLastRecv/(TIMER_LONG/1000);
this->m_nTotalRecv += this->m_nLastRecv;
if(m_nTotalRecv/MB)
{
s1[0]='M';
s1[1]='B';
n1=(float)m_nTotalRecv/MB;
}
else if(m_nTotalRecv/KB)
{
s1[0]='K';
s1[1]='B';
n1=(float)m_nTotalRecv/KB;
}
else
{
s1[0]='B';
s1[1]=0;
n1=(float)m_nTotalRecv;
}
if(nSpeed/MB)
{
s2[0]='M';
s2[1]='B';
n2=(float)nSpeed/MB;
}
else if(nSpeed/KB)
{
s2[0]='K';
s2[1]='B';
n2=(float)nSpeed/KB;
}
else
{
s2[0]='B';
s2[1]=0;
n2=(float)nSpeed;
}
sprintf(msg,"接收:%0.3f%s 速度:%0.3f%sps",n1,s1,n2,s2);
this->m_nLastRecv = 0;
g_StatusBar.SetText(msg,2,NULL);
}
break;
default:
CDialog::OnTimer(nIDEvent);
break;
}
}
void CServerDlg::OnDestroy()
{
UpdateData(TRUE);
char sz[32];
sprintf(sz,"%d",this->m_nPort);
WritePrivateProfileString(FILESHARE,SAVEPATH,(LPCTSTR)this->m_strSavePath,CONFIG_FILE);
WritePrivateProfileString(FILESHARE,SERVICEPORT,sz,CONFIG_FILE);
CDialog::OnDestroy();
}
void CServerDlg::OnBrowsePath()
{
char path[256];
UpdateData(true);
if(!C_BrowsePath(this->GetSafeHwnd(),path,"选择接收文件保存路径"))
{
this->m_strSavePath=path;
UpdateData(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -