?? clientsocket.cpp
字號:
// ClientSocket.cpp : implementation file
//
#include "stdafx.h"
#include "DialTest.h"
#include "ClientSocket.h"
#include "DialTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket(CDialog* pDlg)
{
m_pDlg = pDlg;
}
CClientSocket::~CClientSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
void CClientSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
#define MAX_RECEIVE_BUF 4096
char szData[MAX_RECEIVE_BUF+1];
int nRecv = 0;
if (nErrorCode == 0)
{
nRecv = this->Receive(szData, MAX_RECEIVE_BUF);
if (nRecv > 0 && nRecv <= MAX_RECEIVE_BUF)
{
szData[nRecv] = '\0';
if (m_pDlg)
{
((CDialTestDlg*)m_pDlg)->AddReceiveData(szData, nRecv);
}
m_strReceived += szData;
if (m_strReceived.Find("</wml>") > 0)
{
Close();
if (m_pDlg)
{
((CDialTestDlg*)m_pDlg)->DoParsePage(m_strReceived);
}
}
}
if (nRecv == 0)
{
Close();
}
}
CAsyncSocket::OnReceive(nErrorCode);
}
void CClientSocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if (nErrorCode != 0)
{
if (m_pDlg)
{
((CDialTestDlg*)m_pDlg)->MessageBox("OnConnect 連接 10.0.0.172:80 失敗");
}
}
else
{
Send(m_strSendContent, m_strSendContent.GetLength());
if (m_pDlg)
{
CString strSplit = "\r\n**********************************\r\n";
((CDialTestDlg*)m_pDlg)->AddReceiveData(m_strSendContent, m_strSendContent.GetLength());
((CDialTestDlg*)m_pDlg)->AddReceiveData(strSplit, strSplit.GetLength());
}
}
CAsyncSocket::OnConnect(nErrorCode);
}
void CClientSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if (m_pDlg)
{
((CDialTestDlg*)m_pDlg)->DoParsePage(m_strReceived);
}
CAsyncSocket::OnClose(nErrorCode);
}
void CClientSocket::ConnectAndSend(LPCTSTR lpszBindIp, LPCTSTR lpszConnectIp, USHORT nConnectPort, LPCTSTR lpszSendContent)
{
Close();
Create(0, SOCK_STREAM);
Bind(0, lpszBindIp);
Connect(lpszConnectIp, nConnectPort);
CString strSplit = "\r\n\r\n\r\n==========================================\r\n";
if (m_pDlg)
{
((CDialTestDlg*)m_pDlg)->AddReceiveData(strSplit, strSplit.GetLength());
}
m_strSendContent = lpszSendContent;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -