⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talkdlg.cpp

📁 一个简单的服务器客户端通信程序。适用于网络编程入门
💻 CPP
字号:
// TalkDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Talk.h"
#include "TalkDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTalkDlg dialog

CTalkDlg::CTalkDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTalkDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTalkDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTalkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTalkDlg)
	DDX_Control(pDX, IDC_LIST1, m_MSG);
	DDX_Control(pDX, IDC_EDIT1, m_Message);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTalkDlg, CDialog)
	//{{AFX_MSG_MAP(CTalkDlg)
	//ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTalkDlg message handlers

BOOL CTalkDlg::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
	
	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 CTalkDlg::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 CTalkDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTalkDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here
	m_clientSocket.ShutDown(2);
	m_clientSocket.m_hSocket = INVALID_SOCKET;
	m_clientSocket.m_bConnected = FALSE;

	CAddrDlg m_AddrDlg;
	m_AddrDlg.m_Port = 3333;
	m_AddrDlg.m_Address = "127.0.0.1";
	
	if (m_AddrDlg.DoModal()==IDOK && !m_AddrDlg.m_Address.IsEmpty())
	{
		memcpy (m_szServerAdr, m_AddrDlg.m_Address, sizeof(m_szServerAdr));
		m_szPort = m_AddrDlg.m_Port;

		SetTimer (1, 1000, NULL);
		TryCount =0;
	}
/*
	m_AddrDlg.DoModal();
	CString m_nAddress = m_AddrDlg.m_Address;
	CString m_nPort = m_AddrDlg.m_Port;
	AfxMessageBox(m_nAddress);
	AfxMessageBox(m_nPort);
*/
}

void CTalkDlg::OnTimer( UINT nIDEvent)
{

	if(m_clientSocket.m_hSocket == INVALID_SOCKET)
	{
		BOOL bFlag = m_clientSocket.Create(0, SOCK_STREAM, FD_CONNECT);
		if( !bFlag)
		{
			AfxMessageBox("Socket Error!");
			m_clientSocket.Close();
			PostQuitMessage(0);
			return;
		}
	}
	m_clientSocket.Connect(m_szServerAdr, m_szPort);
	TryCount++;
	if (TryCount >=3 || m_clientSocket.m_bConnected)
	{
		KillTimer(1);
		if (TryCount >=3 )
			AfxMessageBox("Connect Failed!");
		return;
	}
	CDialog::OnTimer(nIDEvent);
}

void CTalkDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	m_clientSocket.ShutDown(2);
	EndDialog(0);
	
}

void CTalkDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	if (m_clientSocket.m_bConnected)
	{
		m_clientSocket.AsyncSelect(FD_WRITE);
	}
	else
	{
		AfxMessageBox("Please Connect the Server first!");
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -