📄 clientdlg.cpp
字号:
// clientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "client.h"
#include "clientDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_server = _T("");
m_port = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_EDIT4, m_send_message);
DDX_Control(pDX, IDC_EDIT1, m_message);
DDX_Text(pDX, IDC_EDIT2, m_server);
DDX_Text(pDX, IDC_EDIT3, m_port);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_CONNECT_BUTTON, OnConnectButton)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_CLIENT_READ,OnClientRead)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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
Init();
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::Init()
{
m_port=7000;
m_server="127.0.0.1";
UpdateData(FALSE);
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClientDlg::AddMessageToMessageBar(CString strMessage)
{
CTime tm=CTime::GetCurrentTime();
CString strTime=tm.Format("[%Y-%m-%d %H:%M:%S]");
CString str;
m_message.GetWindowText(str);
str=strTime+strMessage+"\r\n"+str;
m_message.SetWindowText(str);
}
//////////////////////////////////////////////////////////////////////////////////////////
void CClientDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if(mySocket.m_bConnect)
{
closesocket(mySocket.m_clientSocket);
}
}
BOOL CClientDlg::ConnectToServer()
{
if(mySocket.m_bConnect)return TRUE;
if(!UpdateData(TRUE))return FALSE;
WSADATA wsaData;
SOCKADDR_IN sockAddr;
if(WSAStartup(WINSOCK_VERSION,&wsaData))
{
AddMessageToMessageBar("不能使用SOCKET,WSAStartup失败");
WSACleanup();
return FALSE;
}
WSACleanup();
mySocket.m_clientSocket=socket(PF_INET,SOCK_STREAM,0);
memset((void*)&sockAddr,0,sizeof(sockAddr));
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(m_port);
sockAddr.sin_addr.s_addr=inet_addr(m_server);
int iConnect=connect(mySocket.m_clientSocket,(LPSOCKADDR)&sockAddr,sizeof(sockAddr));
if(iConnect)
{
AddMessageToMessageBar("连接失败");
return FALSE;
}
else
{
AddMessageToMessageBar("连接成功");
}
int iErrorCode=WSAAsyncSelect(mySocket.m_clientSocket,m_hWnd,WM_CLIENT_READ,FD_READ | FD_CLOSE);
if(iErrorCode==SOCKET_ERROR)
{
AddMessageToMessageBar("设定响应事件失败");
}
else
{
AddMessageToMessageBar("设定响应事件成功");
}
mySocket.m_bConnect=TRUE;
return TRUE;
}
LRESULT CClientDlg::OnClientRead(WPARAM wParam,LPARAM lParam)
{
if(!mySocket.m_bConnect)return 0L;
if(WSAGETSELECTEVENT(lParam) == FD_CLOSE)
{
AddMessageToMessageBar("你被踢出去了");
closesocket( mySocket.m_clientSocket );
mySocket.m_clientSocket = INVALID_SOCKET;
mySocket.m_bConnect = FALSE;
return 0;
}
if(WSAGETSELECTEVENT(lParam) != FD_READ) return 0;
if(mySocket.m_clientSocket!=wParam)return 0L;
mySocket.m_nLength=recv(mySocket.m_clientSocket,(LPSTR)mySocket.buf,sizeof(mySocket.buf),0);
if(mySocket.m_nLength>0)
{
CString str;
str.Format("接收到数据:%s",mySocket.buf);
AddMessageToMessageBar(str);
mySocket.Init();
}
return 1L;
}
void CClientDlg::OnSendButton()
{
if(!mySocket.m_bConnect)return;
mySocket.m_nLength=m_send_message.GetWindowText((char*)mySocket.buf,sizeof(mySocket.buf));
if(mySocket.m_nLength==0)
{
AddMessageToMessageBar("不能发送空消息!");
return;
}
int iErrorCode=send(mySocket.m_clientSocket,(LPSTR)mySocket.buf,mySocket.m_nLength,0);
if(iErrorCode==SOCKET_ERROR)
{
CString str;
str.Format("发送消息[%s]失败",mySocket.buf);
AddMessageToMessageBar(str);
}
mySocket.Init();
}
void CClientDlg::OnConnectButton()
{
ConnectToServer();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -