📄 echoclientdlg.cpp
字号:
// EchoClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EchoClient.h"
#include "EchoClientDlg.h"
#include "NEWDLG.h"
#include<process.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()
/////////////////////////////////////////////////////////////////////////////
// CEchoClientDlg dialog
CEchoClientDlg::CEchoClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEchoClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEchoClientDlg)
m_tosend = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEchoClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEchoClientDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_TOSEND, m_tosend);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEchoClientDlg, CDialog)
//{{AFX_MSG_MAP(CEchoClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEchoClientDlg message handlers
BOOL CEchoClientDlg::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
StarUp();
SYSTEMTIME lpsystemtime;
GetLocalTime(&lpsystemtime);
return TRUE; // return TRUE unless you set the focus to a control
}
void CEchoClientDlg::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 CEchoClientDlg::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 CEchoClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEchoClientDlg::StarUp()
{
////////////////初始化环境
WSADATA wsaData;
WORD version=MAKEWORD(1,1);//版本号为1.1
int ret =WSAStartup(version,&wsaData);
if(ret !=0)
{
TRACE("初始化失败");
return ;
}
}
void CEchoClientDlg::CleanUp()
{
CString str;
str.Format("%s"," 离开了");
str=name+str;
int ret;
ret=send(m_socket,str.GetBuffer(0),str.GetLength(),0);//发送登陆名
if(ret!=str.GetLength()){
TRACE("Send data error:%d\n",WSAGetLastError());
return ;
}
////////清除,释放资源
if(WSACleanup()!=0)
{
TRACE("UnInitilize Error:%d\n",WSAGetLastError());
}
}
BOOL CEchoClientDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CleanUp();
return CDialog::DestroyWindow();
}
void CEchoClientDlg::OnConnect()
{
// TODO: Add your control notification handler code here
NEWDLG dlg;
dlg.m_server.Format("%s","192.168.49.38");
dlg.m_protocol.Format("%s","TCP");
if(dlg.DoModal()==IDOK){
if(m_socket!=NULL){
closesocket(m_socket);
m_socket=NULL;
}
if(m_socket==NULL)
{
m_socket=socket(AF_INET,SOCK_STREAM,0);
}
m_addr.sin_family=AF_INET;
m_addr.sin_addr.S_un.S_addr=inet_addr(dlg.m_server.GetBuffer(0));
m_addr.sin_port=htons(dlg.m_port);//绑定地址
//连接服务器
int ret=0;
int error=0;
ret=connect(m_socket,(LPSOCKADDR)&m_addr,sizeof(m_addr));
if(ret==SOCKET_ERROR){
TRACE("Connect Error:%d\n",(error=WSAGetLastError()));
if(error==10061)
AfxMessageBox("请确认服务器确实已经打开并作在同样的端口!");
return;
}
m_list.AddString(_T("连接成功"));
CString str=dlg.m_name;
name.Format("%s"," :");
name=dlg.m_name+name;
CString strin;
strin.Format("%s","到来了");
str=str+strin;
ret=send(m_socket,str.GetBuffer(0),str.GetLength(),0);//发送登陆名
if(ret!=str.GetLength()){
TRACE("Send data error:%d\n",WSAGetLastError());
return;
}
//m_list.AddString(str);
///////////////
CEchoClientDlg* pdlg=this;
HANDLE hThread;
hThread=(HANDLE)_beginthreadex(NULL,0,receve,(void*)pdlg,0,0);
/* CTime time = CTime::GetCurrentTime();
CString m_time = time.Format("%Y-%m-%d %H:%M:%S");
///////////////////
char buff[256];
ret=recv(m_socket,buff,256,0);
buff[ret]='\0';
CString str1,str2;
str2.Format("%s"," ");
str1.Format("%s","echo: ");
m_list.AddString(str1+m_time+str2+buff);*/
}
}
void CEchoClientDlg::OnSend()
{
// TODO: Add your control notification handler code here
int ret;
UpdateData(true);
CString str=name+m_tosend;
ret=send(m_socket,str.GetBuffer(0),str.GetLength(),0);//发送登陆名
if(ret!=str.GetLength()){
TRACE("Send data error:%d\n",WSAGetLastError());
return;
}
//m_list.AddString(str);
///////////////
return;
}
unsigned __stdcall receve(void * p){
CEchoClientDlg* m_Dlg=(CEchoClientDlg*)p;
while(true)
{
CTime time = CTime::GetCurrentTime();
CString m_time = time.Format("%Y-%m-%d %H:%M:%S");
///////////////////
char buff[256];
int ret;
ret=recv(m_Dlg->m_socket,buff,256,0);
buff[ret]='\0';
CString str1,str2;
str2.Format("%s"," ");
str1.Format("%s","echo: ");
m_Dlg->m_list.AddString(str1+m_time+str2+buff);
}
return 0;
}
void CEchoClientDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CEchoClientDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -