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

📄 tcp_serverdlg.cpp

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

#include "stdafx.h"
#include "tcp_server.h"
#include "tcp_serverDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTcp_serverDlg dialog

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

void CTcp_serverDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTcp_serverDlg)
	DDX_Control(pDX, IDC_EDIT_remoteport, m_remoteport);
	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);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTcp_serverDlg, CDialog)
	//{{AFX_MSG_MAP(CTcp_serverDlg)
	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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTcp_serverDlg 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_serverDlg *cupdlgTmp = (CTcp_serverDlg *)pvOutput;             /*  Convert the user's pointer  */  

    CString csTmpstr;

    if (usDatalength == __TCP_TERMINATE || usDatalength == __ERR_RX_THREAD) {        
        cupdlgTmp->m_open.EnableWindow(true);	                        /*  Enable the button to open   */
        cupdlgTmp->m_localip.EnableWindow(true);
        CEdit *ceditTmp = (CEdit *)cupdlgTmp->GetDlgItem(IDC_EDIT_localport); 
        ceditTmp->EnableWindow(true);
        cupdlgTmp->m_remoteip.EnableWindow(true);
        cupdlgTmp->m_remoteport.EnableWindow(true);
        return;
    }
    
    if (usDatalength == __TCP_CONNECTED) {

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

        cupdlgTmp->m_remoteip.SetWindowText(csTmpstr);                  /*  Add the string of the IP    */
                                                                        /*  address to the  window      */
        csTmpstr.Format(_T("%d"), ulPort);
        cupdlgTmp->m_remoteport.SetWindowText(csTmpstr);

        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_serverDlg::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[6];                                         /*  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     */

⌨️ 快捷键说明

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