📄 clienthdlg.cpp
字号:
// clientHDlg.cpp : implementation file
//
#include "stdafx.h"
#include "clientH.h"
#include "clientHDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
SOCKET sockclient;
sockaddr_in sockaddrclient;
UINT threadRecvSend(LPVOID lpvoid);
UINT threadSendData(LPVOID lpvoid);
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()
/////////////////////////////////////////////////////////////////////////////
// CClientHDlg dialog
CClientHDlg::CClientHDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientHDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientHDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientHDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientHDlg)
DDX_Control(pDX, IDC_EDIT4, m_msgsend);
DDX_Control(pDX, IDC_messageList, m_msglist);
DDX_Control(pDX, IDC_BtnConnect, m_btnconnect);
DDX_Control(pDX, IDC_EdtStatus, m_state);
DDX_Control(pDX, IDC_StateList, m_statelist);
DDX_Control(pDX, IDC_EdtPort, m_port);
DDX_Control(pDX, IDC_EdtIP, m_ip);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientHDlg, CDialog)
//{{AFX_MSG_MAP(CClientHDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BtnConnect, OnBtnConnect)
ON_BN_CLICKED(IDC_BtnSend, OnBtnSend)
ON_BN_CLICKED(IDC_DisConnect, OnDisConnect)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientHDlg message handlers
BOOL CClientHDlg::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
//版本检查
WSADATA wsa;
if(WSAStartup(0x102,&wsa))
{
AfxMessageBox("套接字初始化错误!",NULL,MB_OK|MB_ICONSTOP);
WSACleanup();
return FALSE;
}
if(LOBYTE(wsa.wVersion)!=2 || HIBYTE(wsa.wVersion)!=1)
{
AfxMessageBox("版本匹配错误!",NULL,MB_OK||MB_ICONSTOP);
WSACleanup();
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientHDlg::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 CClientHDlg::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 CClientHDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClientHDlg::OnBtnConnect()
{
// TODO: Add your control notification handler code here
//获取IP地址
CString ipaddress;
m_ip.GetWindowText(ipaddress);
sockaddrclient.sin_addr.s_addr=inet_addr(ipaddress);
sockaddrclient.sin_family=AF_INET;
//获取端口
CString port;
m_port.GetWindowText(port);
sockaddrclient.sin_port=atoi(port);//htons(5000);
//创建socket
sockclient=socket(AF_INET,SOCK_STREAM,0);
if(sockclient==INVALID_SOCKET)
{
m_statelist.AddString("创建socket失败!");
return;
}
int constate=connect(sockclient,(sockaddr*)&sockaddrclient,sizeof(sockaddrclient));
if(constate!=0)
{
m_statelist.AddString("连接服务器失败!");
return ;
}
else
m_statelist.AddString("连接服务器成功!");
m_btnconnect.EnableWindow(false);
//启动线程
AfxBeginThread(threadRecvSend,0);
}
UINT threadRecvSend(LPVOID lpvoid)
{
//连接到服务器
CClientHDlg *dlg=(CClientHDlg*)AfxGetApp()->GetMainWnd();
//int constate=connect(sockclient,(sockaddr*)&sockaddrclient,sizeof(sockaddrclient));
dlg->SetForegroundWindow();
//定义缓冲区
char buff[100];
//循环获得数据
while(true)
{
//调用recv函数接收数据
SOCKET s=recv(sockclient,buff,100,0);
if(s==INVALID_SOCKET) break;
dlg->SetForegroundWindow();
dlg->m_msglist.AddString(buff);
}
return 0;
}
void CClientHDlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
//发送断开命令
AfxBeginThread(threadSendData,0);
//closesocket(sockclient);
}
void CClientHDlg::OnDisConnect()
{
// TODO: Add your control notification handler code here
::closesocket(sockclient);
this->m_btnconnect.EnableWindow(true);
}
UINT threadSendData(LPVOID lpvoid)
{
CString msg1;
CClientHDlg *dlg=(CClientHDlg*)AfxGetApp()->GetMainWnd();
dlg->m_msgsend.GetWindowText(msg1);
send(sockclient,msg1,100,0);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -