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

📄 tcp_serverdlg.cpp

📁 TCP服务器应用开发示例。 EVC 4.0 编译通过 ARMV4I 运行成功
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                                                                        /*  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           */
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CTcp_serverDlg::OnBUTTONopen() 
{
    unsigned short pusTmpip[16];                                        /*  Temp buffer of the IP       */
                                                                        /*  address from window         */
    
    int            usTmplen = 0;                                        /*  The temp length parameter   */
    char           pcTmpip[16];                                         /*  Temp buffer of the IP       */
                                                                        /*  addressto convert           */

    unsigned long  ulTmplocalip  = 0;                                   /*  Temp parameter of the local */
                                                                        /*  IP address                  */

    unsigned long  ulTmpremoteip = 0;                                   /*  Temp parameter of the remote*/
                                                                        /*  IP address                  */
    unsigned short ulTmpremoteport = 0;

    bool           bTmpret = false;                                     /*  Temp value of the return    */
    


    usTmplen = this->m_localip.GetWindowText(pusTmpip, 16);             /*  Get text from window  of the*/  
                                                                        /*  local IP address            */ 

    WideCharToMultiByte(CP_ACP, 
                        0, 
                        pusTmpip, 
                        usTmplen, 
                        pcTmpip, 
                        usTmplen, 
                        NULL, 
                        NULL);                                          /*  Convert the wide-character  */
                                                                        /*  string to the character     */
                                                                        /*  string                      */

    pcTmpip[usTmplen] = 0;                   
    
    ulTmplocalip = inet_addr(pcTmpip);                                  /*  Convert the dotted IP       */
                                                                        /*  address to the unsigned long*/
  
    UpdateData(true);
   
    bTmpret = ctcpipLocal->epcProtocolStartup(__PT_TCP,
                                              ulTmplocalip,
                                              (unsigned short)this->m_localport,
                                              ulTmpremoteip,    
                                              ulTmpremoteport,
                                              __MODE_SERVER);           /*  Startup UDP client server by*/
                                                                        /*  method of the TCP/IP class  */
    
    
    /* 
     *  If startup is success, disable the button to open.
     */
    if (bTmpret) {                                                      
        this->m_open.EnableWindow(false);    
        this->m_localip.EnableWindow(false);
        CEdit *ceditTmp = (CEdit *)GetDlgItem(IDC_EDIT_localport); 
        ceditTmp->EnableWindow(false);
    } else {
        MessageBox(_T("TCP服务器启动失败!"),
                   _T("失败"),
                   MB_ICONERROR);
    }
}

void CTcp_serverDlg::OnBUTTONclose() 
{    
    this->m_open.EnableWindow(true);	                                /*  Enable the button to open   */
    this->m_localip.EnableWindow(true);
    CEdit *ceditTmp = (CEdit *)GetDlgItem(IDC_EDIT_localport); 
    ceditTmp->EnableWindow(true);
    delete ctcpipLocal;
    this->EndDialog(0);
}

void CTcp_serverDlg::OnBUTTONrxclr() 
{
    /*
     *  Clear buffer and index of the received buffer
     */
    this->usIndex = 0;                                  
    this->m_rx.SetWindowText(_T(""));	
}

void CTcp_serverDlg::OnBUTTONtxclr() 
{
    this->m_tx.SetWindowText(_T(""));	                                /*  Clear buffer of the window  */
                                                                        /*  to send                     */
}

void CTcp_serverDlg::OnBUTTONsend() 
{
    unsigned short pusTmpip[16];
    unsigned short pusTmpport[6]; 
    int            usTmplen;
    char           pcTmpip[16];
    unsigned long  ulTmpremoteip;
    unsigned short ulTmpremoteport;
    
    /*
     *  Get the remote IP address and the remote IP port
     *  from the window, and convert to the specify style.
     */
    usTmplen = this->m_remoteip.GetWindowText(pusTmpip, 16);
    WideCharToMultiByte(CP_ACP, 
                        0, 
                        pusTmpip, 
                        usTmplen, 
                        pcTmpip, 
                        usTmplen, 
                        NULL, 
                        NULL);
    pcTmpip[usTmplen] = 0;
    ulTmpremoteip = inet_addr(pcTmpip);

    usTmplen = this->m_remoteport.GetWindowText(pusTmpport, 6);
    _stscanf(pusTmpport, _T("%d"), &ulTmpremoteport);

    

    char           pcTmpsendbuf[RX_BUF_SIZE];                           /*  Temp buffer of the send data*/
                                                                        /*  to send                     */

    unsigned short pusTmpsendbuf[RX_BUF_SIZE];                          /*  Temp buffer of the send data*/
                                                                        /*  to convert                  */                    
   
    usTmplen = this->m_tx.GetWindowText(pusTmpsendbuf, RX_BUF_SIZE);    /* Get the send data from the   */
                                                                        /*  window                      */

    WideCharToMultiByte(CP_ACP, 
                        0, 
                        pusTmpsendbuf, 
                        usTmplen, 
                        pcTmpsendbuf, 
                        usTmplen, 
                        NULL, 
                        NULL);                                          /*  Convert the wide-character  */
                                                                        /*  string to the character     */
                                                                        /*  string                      */

	this->ctcpipLocal->epcProtocolSenddata(ulTmpremoteip,
                                           ulTmpremoteport,
                                           pcTmpsendbuf,
                                           (unsigned short)usTmplen);	/*  Send data by method of the  */
                                                                        /*  TCP/IP class                */	
}

⌨️ 快捷键说明

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