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

📄 smtest1dlg.cpp

📁 TELNET手机客户端程序
💻 CPP
字号:
// SMTest1Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "SMTest1.h"
#include "SMTest1Dlg.h"
#include "DialogIPAddr.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSMTest1Dlg 对话框

CSMTest1Dlg::CSMTest1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSMTest1Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSMTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_EditMSG);
	DDX_Control(pDX, IDC_LIST1, m_ListMSG);
}

BEGIN_MESSAGE_MAP(CSMTest1Dlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CSMTest1Dlg::OnBnClickedButtonConnect)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_SEND, &CSMTest1Dlg::OnBnClickedButtonSend)
END_MESSAGE_MAP()


// CSMTest1Dlg 消息处理程序

BOOL CSMTest1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

#ifdef WIN32_PLATFORM_WFSP
	if (!m_dlgCommandBar.Create(this) ||
	    !m_dlgCommandBar.InsertMenuBar(IDR_MAINFRAME))
	{
		TRACE0("未能创建 CommandBar\n");
		return FALSE;      // 未能创建
	}
#endif // WIN32_PLATFORM_WFSP
	// TODO: 在此添加额外的初始化代码
	
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CSMTest1Dlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_SMTEST1_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_SMTEST1_DIALOG));
	}
}
#endif


void CSMTest1Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	//发送  命令给  服务器端
	CString *pstr=new CString("abc");
	//AfxMessageBox((LPCTSTR)"大刀",0,0);
	//::MessageBox((HWND)this,(LPCTSTR)"abc",(LPCWSTR)"a",NULL);
}


void CSMTest1Dlg::SetListBoxXScrool() 
{
	// TODO: Add your control notification handler code here
	CString str;
    CSize sz;
    int dx=0;
    TEXTMETRIC tm;
    CDC* pDC=m_ListMSG.GetDC();
    CFont* pFont=m_ListMSG.GetFont();

// Select the listbox font, save the old font
    CFont* pOldFont = pDC->SelectObject(pFont);
// Get the text metrics for avg char width
    pDC->GetTextMetrics(&tm); 

    for(int i=0; i<m_ListMSG.GetCount();i++)
	{
       m_ListMSG.GetText(i, str);
       sz=pDC->GetTextExtent(str);

   // Add the avg width to prevent clipping
       sz.cx+=tm.tmAveCharWidth;
       if (sz.cx>dx)
            dx=sz.cx;
	}
// Select the old font back into the DC
    pDC->SelectObject(pOldFont);
    m_ListMSG.ReleaseDC(pDC);

// Set the horizontal extent so every character of all strings 
// can be scrolled to.
    m_ListMSG.SetHorizontalExtent(dx);

}
void CSMTest1Dlg::OnBnClickedButtonConnect()
{
//socket环境 初始化
   WSADATA wsd;
   WSAStartup(MAKEWORD(1,1),&wsd);

// Open 函数
   ResetEvent(m_exitThreadEvent);
   //存储父窗口句柄
   m_pOwnerWnd = pWnd;
   //创建TCP套接字 
   m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
   if (m_socket == SOCKET_ERROR)
   {
	   return FALSE;
   }
   //创建通讯线程
   m_tcpThreadHandle = CreateThread(NULL,0,SocketThreadFunc,this,0,NULL);
   if (m_tcpThreadHandle == NULL)
   {
	   closesocket(m_socket);
   }
// Open 函数 结束



    CDialogIPAddr m_dlgIP;
	if (m_dlgIP.DoModal()==IDOK && !m_dlgIP.m_strIP.IsEmpty())
	{ 
		memcpy(m_szWServerAdr,m_dlgIP.m_strIP,sizeof(m_szServerAdr));
		//WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
		//宽多字符转换代码段
        DWORD dwMinSize;
        dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,m_szWServerAdr,-1,NULL,0,NULL,FALSE);
        WideCharToMultiByte(CP_OEMCP,NULL,m_szWServerAdr,-1,m_szServerAdr,sizeof(m_szWServerAdr)/sizeof(m_szWServerAdr[0]),NULL,FALSE);
//宽多字符转换代码段    结束

		m_szPort=m_dlgIP.m_nPort;
	    sockaddr_in socketAddr;
	   // socketAddr.sin_addr.s_addr=inet_addr("10.213.13.37");
	    socketAddr.sin_addr.s_addr=inet_addr(m_szServerAdr);
	    socketAddr.sin_family=AF_INET;
	    socketAddr.sin_port=htons(m_szPort); 

	    clientSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

	    int err;
        err=connect(clientSocket,(sockaddr*)&(socketAddr),sizeof(socketAddr));

        if (err == SOCKET_ERROR) 
       {
	        AfxMessageBox((LPCTSTR)_T("clientSocket not connected"),0,0);
       }
   //设置通讯模式为异步模式
       DWORD ul= 1;
       ioctlsocket(clientSocket,FIONBIO,&ul);
	}
}

void CSMTest1Dlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
/*
	if (m_clientSocket.m_hSocket==INVALID_SOCKET)
	{
		//BOOL bFlag=m_clientSocket.Create(0,SOCK_STREAM,FD_CONNECT,NULL); 
		BOOL bFlag=m_clientSocket.Create();
		if(!bFlag) 
		{ 
//			AfxMessageBox("m_clientSocket.Create Error!");//""
			AfxMessageBox((LPCTSTR)_T("m_clientSocket.Create Error!"),0,0);
			m_clientSocket.Close();
			PostQuitMessage(0);
			return; 
		}
	}
	//m_clientSocket.Connect((LPCTSTR)m_szServerAdr,m_szPort);
	m_clientSocket.Connect(_T("10.213.13.37"),1088);
	nTryCount++;
	
	if (nTryCount >=10 || m_clientSocket.m_bConnected)
	{ 
		KillTimer(1); 
		if (nTryCount >=10) 
			//AfxMessageBox("Connect Failed!"); 
		    AfxMessageBox(_T("Connect Failed!"),0,0);//(LPCTSTR)
		return;
	}
*/

	CDialog::OnTimer(nIDEvent);
}

void CSMTest1Dlg::OnBnClickedButtonSend()
{
	// TODO: 在此添加控件通知处理程序代码
	wchar_t sendBufferW[256];
    char sendBuffer[256];

    m_EditMSG.GetWindowText(sendBufferW, sizeof(sendBufferW)); 
//宽多转换
    DWORD dwMinSize;
    dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,sendBufferW,-1,NULL,0,NULL,FALSE);
    WideCharToMultiByte(CP_OEMCP,NULL,sendBufferW,-1,sendBuffer,sizeof(sendBufferW)/sizeof(sendBufferW[0]),NULL,FALSE);
//发送数据 多字符
    send(clientSocket,sendBuffer,sizeof(sendBuffer),0);

    m_EditMSG.SetWindowText(_T(""));

}



////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////  宽多字符转换封装函数
////////////////////////////////////////////////////////////////


//-------------------------------------------------------------------------------------
  //Description:
  // This function maps a character string to a wide-character (Unicode) string
  //
  //Parameters:
  // lpcszStr: [in] Pointer to the character string to be converted 
  // lpwszStr: [out] Pointer to a buffer that receives the translated string. 
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  // 
  //Example:
  // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  //---------------------------------------------------------------------------------------
/*
  BOOL CSMTest1Dlg::MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  {
    // Get the required size of the buffer that receives the Unicode 
    // string. 
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
  
    if(dwSize < dwMinSize)
    {
     return FALSE;
    }
  
    
    // Convert headers from ASCII to Unicode.
    MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);  
    return TRUE;
  }
  
  //-------------------------------------------------------------------------------------
  //Description:
  // This function maps a wide-character string to a new character string
  //
  //Parameters:
  // lpcwszStr: [in] Pointer to the character string to be converted 
  // lpszStr: [out] Pointer to a buffer that receives the translated string. 
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  // 
  //Example:
  // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  //---------------------------------------------------------------------------------------
  
  BOOL CSMTest1Dlg::WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  {
   DWORD dwMinSize;
   dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
   if(dwSize < dwMinSize)
   {
    return FALSE;
   }
   WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
   return TRUE;
  }
  
  
  使用方法也很简单,示例如下:
  wchar_t wText[10] = {L"函数示例"};
  char sText[20]= {0};
  WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));
  MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));
  
*/

/////////////////////////////////////    其他例子
/*
//  测试普通字符到宽字符的转换
	//////(1)声明转换前的字符串
    char chSource_Char[] = "hello world";

    //////(2)调用MultiByteToWideChar,为lpWideCharStr传递NULL , cchWideChar 传递0
    int iWide_Buffer_Size = MultiByteToWideChar(CP_ACP , 0 , chSource_Char , -1 , NULL , 0);

    //////(3)声明宽字符缓冲区
    WCHAR * pwchWide_Char = new WCHAR[iWide_Buffer_Size + 1];
    if(pwchWide_Char == NULL)
    {
            AfxMessageBox(L"内存分配失败");
            return;
    }
    memset(pwchWide_Char , 0 , (iWide_Buffer_Size + 1)*sizeof(WCHAR));

    //////(4)转化
    MultiByteToWideChar(CP_ACP , 0 , chSource_Char , -1 , pwchWide_Char , iWide_Buffer_Size);

	AfxMessageBox(pwchWide_Char,0,0);
//  测试普通字符到宽字符的转换  END success


///////////////////// 测试宽字符到普通字符的转换 ////////////////
	    //char chSource_Char[] = "hello world";
//		WCHAR pW_Char[]=_T("hello world";);
//	    AfxMessageBox(pW_Char,0,0);             ////以上用法成立


	//wchar_t wText[20] = {L"宽字符转换实例!OK!"};
	WCHAR wText[]=_T("hello world";);
    DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);
    char *psText;
    psText = new char[dwNum];
    if(!psText)
    {
       delete []psText;
    }
    WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);
    	
	AfxMessageBox((LPCTSTR)psText,0,0);
	AfxMessageBox((LPCTSTR)chSource_Char,0,0);
/////////////////////    测试宽字符到普通字符的转换    END SUCCESS    ///////////////
*/


⌨️ 快捷键说明

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