📄 testtcpdlg.cpp
字号:
// TestTCPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestTCP.h"
#include "TestTCPDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CTestTCPDlg dialog
CTestTCPDlg::CTestTCPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestTCPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestTCPDlg)
// 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 CTestTCPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestTCPDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestTCPDlg, CDialog)
//{{AFX_MSG_MAP(CTestTCPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestTCPDlg message handlers
BOOL CTestTCPDlg::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
if(!m_Socket.Create())
{
AfxMessageBox("socket create error!");
exit(0);
}
if(!m_Socket.Connect("10.25.12.7",8080))
{
AfxMessageBox("connect to host failed!");
}
AfxBeginThread(Thread_GetData,this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestTCPDlg::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 CTestTCPDlg::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 CTestTCPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT CTestTCPDlg::Thread_GetData(PVOID pVoid)
{
CTestTCPDlg * pDlg = (CTestTCPDlg*)pVoid;
BOOL bRes;
char szBuff[1024];
int iByteRec = 0;
while(1)
{
::Sleep(10);
if(pDlg->m_Socket.IsReadible(bRes))
{
memset(szBuff,0,1024);
iByteRec = pDlg->m_Socket.Receive(szBuff,1023);
if(iByteRec !=SOCKET_ERROR)
{
AfxMessageBox(szBuff);
}
}
}
}
char * CTestTCPDlg::Wide2ANSI(char * pSource,char * pBuff,int uLen)
{
int iSourceLen = strlen(pSource);
BOOL bFlag;
wchar_t * pWChar = new wchar_t[iSourceLen/4];
char szTmp[5];
int iUnicode;
for(int i=0;i<iSourceLen/4;i++)
{
strncpy(szTmp,pSource+i*4,4);
szTmp[4]=0;
sscanf(szTmp,"%x",&iUnicode);
pWChar[i]=iUnicode;
}
char * pOut = new char[iSourceLen+1];
memset(pOut,0,iSourceLen+1);
WideCharToMultiByte(CP_ACP,0,pWChar,iSourceLen/4,pOut,iSourceLen+1,NULL,&bFlag);
if(strlen(pOut)>=(unsigned int)uLen)
{
return NULL;
}
strcpy(pBuff,pOut);
delete pWChar;
delete pOut;
return pBuff;
}
char * CTestTCPDlg::ANSI2Wide(char * pSource,char * pBuff,int uLen)
{
int iSourceLen = strlen(pSource);
wchar_t * pWChar = new wchar_t [iSourceLen+2];
//将普通文本转化为UNICODE;
MultiByteToWideChar(CP_ACP,0,pSource,iSourceLen+1,
pWChar,iSourceLen+1);
//为了安全,正常情况下无此必要
pWChar[iSourceLen+1]=0;
unsigned char tmp[128];
CString strOut;
unsigned char * pTmp = (unsigned char *)pWChar;
//将一个unicode数组转换为
//一个字符串,这个字符串
//用16进制表示这个数组
for(int i=0;i<iSourceLen;i++)
{
if(pWChar[i]==0x0000)
break;
//高低位倒置,tmp必须unsigned char 否则出现ffffff
sprintf((char *)tmp,"%02x%02x",*(pTmp+i*2+1),*(pTmp+i*2));
strOut+=tmp;
}
if(strOut.GetLength()>=uLen)
{
return NULL;
}
strcpy(pBuff,strOut);
delete pWChar;
return pBuff;
}
void CTestTCPDlg::OnSend()
{
// TODO: Add your control notification handler code here
unsigned char szUTF8Msg[128];
CString strOrgMsg = "a b c";
CString str2312Msg = theApp.URLGB2312_Encode(strOrgMsg);
AfxMessageBox(str2312Msg);
CString strMiddleMsg = "7 11274 4444 "+strOrgMsg;
memset(szUTF8Msg,0,128);
int iRes = theApp.UTF8_Encode(szUTF8Msg,strMiddleMsg);
AfxMessageBox((char*)szUTF8Msg+2);
m_Socket.Send((const char*)szUTF8Msg,strlen((const char *)szUTF8Msg+2)+2);
}
void CTestTCPDlg::OnButton2()
{
// TODO: Add your control notification handler code here
char szBuff[128];
memset(szBuff,0,128);
//Wide2ANSI("C4E3BAC3",szBuff,127);
Wide2ANSI("BAC3",szBuff,127);
// ANSI2Wide("??",szBuff,127);
AfxMessageBox(szBuff);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -