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

📄 udp_serverdlg.cpp

📁 UDP通讯开发实例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// udp_serverDlg.cpp : implementation file
//

#include "stdafx.h"
#include "udp_server.h"
#include "udp_serverDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CUdp_serverDlg dialog

CUdp_serverDlg::CUdp_serverDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUdp_serverDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUdp_serverDlg)
		// 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 CUdp_serverDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUdp_serverDlg)
	DDX_Control(pDX, IDC_EDIT_remoteport, m_remoteport);
	DDX_Control(pDX, IDC_EDIT_tx, m_tx);
	DDX_Control(pDX, IDC_EDIT_rx, m_rx);
	DDX_Control(pDX, IDC_BUTTON_open, m_open);
	DDX_Control(pDX, IDC_EDIT_remotip, m_remoteip);
	DDX_Control(pDX, IDC_COMBO_localip, m_localip);
	DDX_Text(pDX, IDC_EDIT_localport, m_localport);
	DDV_MinMaxInt(pDX, m_localport, 1024, 5000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUdp_serverDlg, CDialog)
	//{{AFX_MSG_MAP(CUdp_serverDlg)
	ON_WM_HELPINFO()
	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()


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

    CString csTmpstr;

    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);

    
    /*
     *  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             */
}
/////////////////////////////////////////////////////////////////////////////
// CUdp_serverDlg message handlers

BOOL CUdp_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;                                               /*  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.SetWindowText(_T("0"));                          /*  Initialize the remote port  */
    this->m_remoteip.SetWindowText(_T("0.0.0.0"));                      /*  Initialize the remote IP    */
                                                                        /*  address                     */

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

⌨️ 快捷键说明

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