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

📄 dlg2.cpp

📁 关于网络编程自己做得一个小例子
💻 CPP
字号:
// Dlg2.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "Dlg2.h"
#include "ChatSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlg2 dialog


CDlg2::CDlg2(CWnd* pParent /*=NULL*/)
	: CDialog(CDlg2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlg2)
	m_strName = _T("www");
	m_strServer = _T("127.0.0.1");
	m_Port = 1000;
	m_strMsg = _T("");
	m_pSocket=NULL;
	//}}AFX_DATA_INIT
	

}


void CDlg2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlg2)
	DDX_Control(pDX, IDC_LIST1, m_listMsg);
	DDX_Text(pDX, IDC_EDIT1, m_strMsg);
	DDV_MaxChars(pDX, m_strMsg, 25);
	DDX_Text(pDX, IDC_EDIT2, m_strName);
	DDX_Text(pDX, IDC_EDIT3, m_strServer);
	DDX_Text(pDX, IDC_EDIT4, m_Port);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlg2, CDialog)
	//{{AFX_MSG_MAP(CDlg2)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON1, OnSend)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlg2 message handlers
BOOL CDlg2::OnInitDialog() 
{
	CDialog::OnInitDialog();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void CDlg2::OnDestroy() 
{
	CDialog::OnDestroy();
	if(m_pSocket!=NULL)
	{
		m_pSocket->Close();
		m_pSocket->m_pDlg=NULL;
		delete m_pSocket;
	}	
}

void CDlg2::OnSend()       //发送数据
{
   UpdateData(TRUE);
   SendData(m_strMsg);  
   m_strMsg="";
   UpdateData(FALSE);
}

void CDlg2::ReadData()          //读数据
{
    char buffer[BUFFER_SIZE];

	int nReceived=m_pSocket->Receive(buffer,BUFFER_SIZE,0);       //从服务器接收信息
		buffer[nReceived]=0;
	CString st;
	st=buffer;
	m_df.ReadFrame(st);
	m_listMsg.AddString(st);

	//m_df.WriteFrame(st);
}



void CDlg2::SendData(CString strMsg)  //发送
{
    CString str;
	str.Format("%s:%s",m_strName,strMsg);
	m_df.WriteFrame(str);

	if(m_pSocket!=NULL)
	{
		int len = m_pSocket->Send(str.GetBuffer(0),str.GetLength());    //发给服务器信息
		if(len<=0)
		{
			AfxMessageBox("发送数据失败,请重试!");
			return;
		}
	}

	return;		
}





void CDlg2::OnOK()            //信息确定
{
	if(m_pSocket)
	{
		m_pSocket->Close();
		m_pSocket->m_pDlg=NULL;
		delete m_pSocket;
		m_pSocket=NULL;
	}	
	
	m_pSocket=new CChatSocket(this);
	if(! m_pSocket->Create())
	{		
		m_pSocket->m_pDlg=NULL;
		delete m_pSocket;
		m_pSocket=NULL;
		AfxMessageBox("创建SOCKET失败!");
		return;
	}
	
	if(!m_pSocket->Connect(m_strServer,m_Port))
		{
		     if(AfxMessageBox("连接服务器失败,请重试!",MB_YESNO)==IDNO)
			 {
				 m_pSocket->m_pDlg=NULL;
				 delete m_pSocket;
				 m_pSocket=NULL;
				 return;
			 }
		}
	pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->
						GetDescendantWindow(AFX_IDW_STATUS_BAR);//取得窗口指针
	if(pStatus)
	{			
		pStatus->SetPaneText(2,"连接服务器成功");
	}

}

void CDlg2::OnCancel() 
{
	
	CDialog::OnCancel();
}

void CDlg2::OnButton2()      //清空
{
	m_listMsg.ResetContent();
}

void CDlg2::OnButton3()         //断开连接
{
	if(m_pSocket)
	{
		m_pSocket->Close();
		m_pSocket->m_pDlg=NULL;
		delete m_pSocket;
		m_pSocket=NULL;
		pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->
						GetDescendantWindow(AFX_IDW_STATUS_BAR);//取得窗口指针
		pStatus->SetPaneText(2,"断开连接");
	}
	
}

⌨️ 快捷键说明

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