📄 sdlg.cpp
字号:
// sDlg.cpp : implementation file
//
#include "stdafx.h"
#include "s.h"
#include "sDlg.h"
#include "sock.h"
#include "socklist.h"
#include "sockpoint.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSDlg dialog
CSDlg::CSDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSDlg)
m_txt = _T("");
m_pot = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT1, m_txt);
DDX_Text(pDX, IDC_EDIT2, m_pot);
DDV_MinMaxUInt(pDX, m_pot, 1, 65534);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSDlg, CDialog)
//{{AFX_MSG_MAP(CSDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSDlg message handlers
BOOL CSDlg::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_pot=6000;
this->UpdateData(false);
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
m_buf[0]='\0';
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 CSDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_ssock=new Csock(this);
if(!m_ssock->Create(m_pot)) //建立监听套接字
{
m_txt+="*套接字建立失败!\r\n";
delete m_ssock;
UpdateData(false);
return ;
}
if(!m_ssock->Listen()) //建立监听
{
m_txt+="*套接字监听失败!\r\n";
delete m_ssock;
UpdateData(false);
return ;
}
m_txt+="-服务已开启!\r\n";
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
this->GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
this->GetDlgItem(IDC_EDIT2)->EnableWindow(false);
UpdateData(false);
m_csock=new Csocklist(this);
}
void CSDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_ssock->Close();
delete m_ssock;
m_txt+="-服务已关闭!\r\n";
if(m_csock->hand->next!=NULL) //如果有客户端连接
m_txt+="-客户端均已断开!\r\n";
delete m_csock;
m_csock=NULL;
this->GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
this->GetDlgItem(IDC_BUTTON1)->EnableWindow(true);
this->GetDlgItem(IDC_EDIT2)->EnableWindow(true);
m_list.ResetContent();
UpdateData(false);
}
CSDlg::OnAccept()
{
Csockpoint *temp;
temp=m_csock->addnew(this);
m_ssock->Accept(*temp->psock);
UINT m_cpot;
temp->psock->GetPeerName(temp->psock->m_ip_pot,m_cpot);
CString strtemp;
strtemp.Format("%d",m_cpot);
temp->psock->m_ip_pot+="-[";
temp->psock->m_ip_pot+=strtemp;
temp->psock->m_ip_pot+="] ";
strtemp=temp->psock->m_ip_pot+"-加入!";
m_txt+=strtemp;
m_list.AddString(temp->psock->m_ip_pot);
strcpy(m_buf,strtemp.GetBuffer(strtemp.GetLength()));
strtemp.ReleaseBuffer();
m_txt+="\r\n";
m_csock->showall();
UpdateData(false);
temp=NULL;
}
CSDlg::OnClose(Csock *ps)
{
m_list.DeleteString(m_list.SelectString(0,ps->m_ip_pot));
CString strtemp;
strtemp=ps->m_ip_pot+"-断开!";
strcpy(m_buf,strtemp.GetBuffer(strtemp.GetLength()));
strtemp.ReleaseBuffer();
m_txt+=strtemp;
m_txt+="\r\n";
m_csock->showall();
m_csock->delone(ps);
UpdateData(false);
}
CSDlg::OnReceive(Csock *ps)
{
ps->Receive(m_buf,1024);
CString temp;
temp.Format("%s",m_buf);
temp=ps->m_ip_pot+temp;
strcpy(m_buf,temp.GetBuffer(temp.GetLength()));
temp.ReleaseBuffer();
m_txt+=temp;
m_txt+="\r\n";
m_csock->showall();
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -