?? dlg2.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 + -