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

📄 tcp_clientdlg.cpp

📁 TCP客户端应用开发示例。 EVC 4.0 编译通过 ARMV4I 运行成功
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// tcp_clientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "tcp_client.h"
#include "tcp_clientDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTcp_clientDlg dialog

CTcp_clientDlg::CTcp_clientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTcp_clientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTcp_clientDlg)
	m_localport = 0;
	m_remoteport = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTcp_clientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTcp_clientDlg)
	DDX_Control(pDX, IDC_BUTTON_open, m_open);
	DDX_Control(pDX, IDC_EDIT_tx, m_tx);
	DDX_Control(pDX, IDC_EDIT_rx, m_rx);
	DDX_Control(pDX, IDC_EDIT_remotip, m_remoteip);
	DDX_Control(pDX, IDC_COMBO_localip, m_localip);
	DDX_Text(pDX, IDC_EDIT_localport, m_localport);
	DDX_Text(pDX, IDC_EDIT_remoteport, m_remoteport);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTcp_clientDlg, CDialog)
	//{{AFX_MSG_MAP(CTcp_clientDlg)
	ON_BN_CLICKED(IDC_BUTTON_open, OnBUTTONopen)
	ON_BN_CLICKED(IDC_BUTTON_close, OnBUTTONclose)
	ON_BN_CLICKED(IDC_BUTTON_rxclr, OnBUTTONrxclr)
	ON_BN_CLICKED(IDC_BUTTON_txclr, OnBUTTONtxclr)
	ON_BN_CLICKED(IDC_BUTTON_send, OnBUTTONsend)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTcp_clientDlg message handlers
/*********************************************************************************************************
** Function name:           mytcpipReceivedata
**
** Descriptions:            It's a callback function. Socket receive data from the remote IP endpoint.
**
** input parameters:        ulIpaddr       Remote IP Address
**                          ulPort         Remote port
**                          pcData         Pointer of the received data
**                          usDatalength   Length of the received data 
** output parameters:       pvOutput       Point at an output pointer
** Returned value:          None 
*********************************************************************************************************/
void mytcpipReceivedata (void           *pvOutput, 
                         unsigned long   ulIpaddr, 
                         unsigned short  ulPort, 
                         char           *pcData, 
                         unsigned short  usDatalength) 
{
    CTcp_clientDlg *cupdlgTmp = (CTcp_clientDlg*)pvOutput;              /*  Convert the user's pointer  */         

    int iTmp;

    iTmp = ulIpaddr;
    iTmp = ulPort;    

    if (usDatalength == __TCP_CONNECTED )
        return;

    if (usDatalength == __TCP_TERMINATE ) {
        MessageBox(NULL,
                   _T("Remote connection was terminated!"), 
                   _T("Information"), 
                   MB_ICONHAND);
        return;
    }

    if (usDatalength == __ERR_RX_THREAD ) {
        MessageBox(NULL,
                   _T("Remote connection was error!"), 
                   _T("Error"), 
                   MB_ICONHAND);
        return;
    }

    /*
     *  Check size of the received buffer,
     *  if the buffer is overflow, drop the received data.
     */
    if ((cupdlgTmp->usIndex + usDatalength) > RX_BUF_SIZE) {          
        MessageBox(NULL,
                   _T("Receive Buf is Full!"), 
                   _T("Warning"), 
                   MB_ICONHAND);
        return;
    }

    MultiByteToWideChar(CP_ACP, 
                        0, 
                        pcData, 
                        usDatalength, 
                        &cupdlgTmp->pusRxbuf[cupdlgTmp->usIndex],
                        usDatalength);                                  /*  Map the received data to the*/
                                                                        /*  wide-character string       */

    
    cupdlgTmp->pusRxbuf[cupdlgTmp->usIndex+usDatalength] = 0;           /*  Set last character of the   */
                                                                        /*  received data with 0        */

    cupdlgTmp->m_rx.SetWindowText(cupdlgTmp->pusRxbuf);                 /*  Update text of the received */
                                                                        /*  window                      */

    cupdlgTmp->usIndex = (unsigned short)(cupdlgTmp->usIndex + usDatalength);                                 
                                                                        /*  Update index of the         */
                                                                        /*  received buffer             */
}


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

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

    unsigned long ulLocalip[5];                                         /*  Table of the local IP       */
                                                                        /*  address                     */

    int           iIpnum = 0;                                           /*  Number of the table of the  */
                                                                        /*  local IP address            */

    ctcpipLocal = new EPC_TCP_IP(ulLocalip,
                                 &iIpnum,
                                 RX_BUF_SIZE,
                                 mytcpipReceivedata,
                                 this);                                /*  Call constructor of the     */
                                                                        /*  'CLASS_TCP_IP' class, gain  */
                                                                        /*  the table of the local IP   */
                                                                        /*  address                     */ 

    if (ulLocalip == NULL) {                                            /*  Constructor to execute is   */
                                                                        /*  error. Release resource.    */
        delete ctcpipLocal;
        return 0;
    }
    
    int     i;
    CString csTmpstr;

    for (i=0; i < iIpnum; i++) {

        csTmpstr.Format(_T("%d.%d.%d.%d"), ulLocalip[i]&0xff, 
                        (ulLocalip[i] >> 8) & 0xff,
                        (ulLocalip[i] >> 16) & 0xff, 
                        (ulLocalip[i] >> 24) & 0xff);                   /*  Convert a unsigned long     */
                                                                        /*  to a dotted IP address      */
        

        this->m_localip.AddString(csTmpstr);                            /*  Add the string of the IP    */
                                                                        /*  address to the  window      */
    
    }
    this->m_localip.SetCurSel(0);                                       /*  Set item of the list box of */
                                                                        /*  the local IP address        */
    
    this->usIndex = 0;                                                  /*  Initialize index of the     */
                                                                        /*  received buffer             */

    this->m_localport  = 1024;                                          /*  Initialize the local port   */
    this->m_remoteport = 1024;                                          /*  Initialize the remote port  */
    this->m_remoteip.SetWindowText(_T("192.168.0.230"));                /*  Initialize the remote IP    */
                                                                        /*  address                     */

    UpdateData(false);                                                  /*  Initialize the current      */
                                                                        /*  dialog box of the           */

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}




void CTcp_clientDlg::OnBUTTONopen() 
{
    unsigned short pusTmpip[16];                                        /*  Temp buffer of the IP       */
                                                                        /*  address from window         */

⌨️ 快捷键说明

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