📄 winsockclientdlg.cpp
字号:
// WinSockClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WinSockClient.h"
#include "WinSockClientDlg.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
}
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()
/////////////////////////////////////////////////////////////////////////////
// CWinSockClientDlg dialog
CWinSockClientDlg::CWinSockClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWinSockClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWinSockClientDlg)
m_strData = _T("");
m_strChat = _T("");
m_strName = _T("");
m_iPort = 1001;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWinSockClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWinSockClientDlg)
DDX_Control(pDX, IDC_ServPort, m_ctlPort);
DDX_Control(pDX, IDC_ServName, m_ctlName);
DDX_Control(pDX, IDC_Send, m_btnSend);
DDX_Control(pDX, IDC_Exit, m_btnExit);
DDX_Control(pDX, IDC_DisConnect, m_btnDisConn);
DDX_Control(pDX, IDC_Connect, m_btnConn);
DDX_Control(pDX, IDC_Clear, m_btnClear);
DDX_Control(pDX, IDC_Chat, m_lstChat);
DDX_Control(pDX, IDC_Data, m_ctlData);
DDX_Text(pDX, IDC_Data, m_strData);
DDX_LBString(pDX, IDC_Chat, m_strChat);
DDX_Text(pDX, IDC_ServName, m_strName);
DDX_Text(pDX, IDC_ServPort, m_iPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWinSockClientDlg, CDialog)
//{{AFX_MSG_MAP(CWinSockClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_Exit, OnExit)
ON_BN_CLICKED(IDC_Connect, OnClientConnect)
ON_BN_CLICKED(IDC_Send, OnMsgSend)
ON_BN_CLICKED(IDC_DisConnect, OnDisConnect)
ON_BN_CLICKED(IDC_Clear, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWinSockClientDlg message handlers
BOOL CWinSockClientDlg::OnInitDialog()
{
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);
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
//初始的时候,一些控件是不可用的:
m_ctlData.EnableWindow(false);
m_btnSend.EnableWindow(false);
m_btnDisConn.EnableWindow(false);
SetWindowText("客户端程序");
m_cSocket.SetParent(this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWinSockClientDlg::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 CWinSockClientDlg::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 CWinSockClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWinSockClientDlg::OnCancel()
{
// TODO: Add extra cleanup here
//CDialog::OnCancel();
}
void CWinSockClientDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CWinSockClientDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
OnDisConnect();
CDialog::OnCancel();
}
void CWinSockClientDlg::OnExit()
{
// TODO: Add your control notification handler code here
OnDisConnect();
CDialog::OnCancel();
}
//与Socket类相关的事件函数
void CWinSockClientDlg::OnSend()
{
}
void CWinSockClientDlg::OnConnect()
{
//连接成功后,一些控件可用:
m_ctlData.EnableWindow(true);
m_btnSend.EnableWindow(true);
m_btnDisConn.EnableWindow(true);
//一些控件不可用
m_btnConn.EnableWindow(false);
}
void CWinSockClientDlg::OnSocketClose()
{
m_cSocket.Close();
//服务器断开后,客户端控件作出相应的变化
m_ctlData.EnableWindow(false);
m_btnSend.EnableWindow(false);
m_btnDisConn.EnableWindow(false);
m_btnConn.EnableWindow(true);
}
void CWinSockClientDlg::OnReceive()
{
char *pBuf = new char[1025];
int iBufSize = 1024;
int iRecvd;
CString strRecvd;
// 接收信息,并返回接收到的字节数
iRecvd = m_cSocket.Receive(pBuf, iBufSize);
// 判断是否有错误发生
if (iRecvd == SOCKET_ERROR)
{
delete pBuf;
return;
}
else
{
// 利用NULL来截去接收缓存区中多余的字符
pBuf[iRecvd] = NULL;
// 将接收到的有用的信息赋值给CString变量
strRecvd = pBuf;
// 将接收到的信息加入到列表框中去
//m_lstChat.AddString(strRecvd);
m_lstChat.SendMessage(LB_INSERTSTRING, -1, (LPARAM)pBuf);
//将对应的变量的变化显示在控件上
UpdateData(FALSE);
delete pBuf;
}
}
void CWinSockClientDlg::OnClientConnect()
{
// TODO: Add your control notification handler code here
int iConn;
UpdateData(true);
if (""==m_strName)
{
MessageBox("服务器名不能为空!");
m_ctlName.SetFocus();
return;
}
m_cSocket.Create();
iConn = m_cSocket.Connect(m_strName, m_iPort);
//if (iConn==0)
//{
// m_cSocket->Close();
// delete m_cSocket;
//}
}
void CWinSockClientDlg::OnMsgSend()
{
// TODO: Add your control notification handler code here
int iLen;//发送字符串的长度
int iSend;//发送函数返回的结果
UpdateData(true);
if(""==m_strData)
{
MessageBox("不能发送空字符串!");
m_ctlData.SetFocus();
return;
}
iLen = m_strData.GetLength();//取得字符串的长度
iSend = m_cSocket.Send(LPCTSTR(m_strData), iLen);
//检查是否出错
if(SOCKET_ERROR==iSend)
{
MessageBox("发送信息失败!");
return;
}
}
void CWinSockClientDlg::OnDisConnect()
{
// TODO: Add your control notification handler code here
m_cSocket.Close();
//断开连接后,一些控件是不可用的:
m_ctlData.EnableWindow(false);
m_btnSend.EnableWindow(false);
m_btnDisConn.EnableWindow(false);
//一些控件可用
m_btnConn.EnableWindow(true);
}
void CWinSockClientDlg::OnClear()
{
// TODO: Add your control notification handler code here
//清空列表框内容
m_lstChat.SendMessage(LB_RESETCONTENT,0,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -