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

📄 epctcpip.h

📁 TCP服务器应用开发示例。 EVC 4.0 编译通过 ARMV4I 运行成功
💻 H
字号:
/*****************************Copyright(c)****************************************************************
**                    Guangzhou ZHIYUAN electronics Co.,LTD.
**                                     
**                           http://www.embedtools.com
**
**------File Info-----------------------------------------------------------------------------------------
** File Name:               EPC_TCP_IP.h
** Last modified Date: 	    2008.02.26
** Last Version:            V1.09
** Description:             TCP/IP Class
**                           
**--------------------------------------------------------------------------------------------------------
** Created By:              Fangfang Zhang
** Created date:            2007.09.07
** Version:                 V1.0
** Descriptions:            TCP/IP Class Define
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
*********************************************************************************************************/
#if !defined __EPC_TCP_IP
#define      __EPC_TCP_IP     

#if _MSC_VER > 1000
#pragma once
#endif  

#include <afxsock.h>

#define VOIDMESSAGEBOX(NULL, lpText, lpCaption, uType);

#ifndef _DEBUG
#define __DebugMSG VOIDMESSAGEBOX
#endif

#ifdef  _DEBUG
#define __DebugMSG MessageBox
#endif

#define __FILE_VERSION       0x107                                      /*  Version of the driver       */

/*  
 *  Define Protocol Type  
 */
#define __PT_TCP             IPPROTO_TCP                                /*  Protocol Type: TCP          */                  
#define __PT_UDP             IPPROTO_UDP                                /*  Protocol Type: UDP          */

/*  
 *  Socket Mode 
 */
#define __MODE_SERVER        0x01                                       /*  Socket Mode: Server         */
#define __MODE_CLIENT        0x02                                       /*  Socket Mode: Client         */

/*  
 *  Define the size of Some Buffer
 */
#define __LOCAL_ETHER_MAX    10
#define __HST_NAME_SIZE      20                                         /*  Size of the host name       */ 

/*  
 *  Define some configuration parameters
 */
#define __LISTEN_MAX_NUM     10                                         /*  Maximum of the listen queue */

#define __PORT_MIN           1024                                       /*  Maximum of the port         */
#define __PORT_MAX           5000                                       /*  Minimum of the port         */

#define __DATA_LEN_MAX       1460                                       /*  Maximum of the data length  */

#define __TCP_CONNECTED	     0xffff                                     /*  Flag of the TCP Connection  */
#define __TCP_TERMINATE	     0xfffe                                     /*  Flag of the TCP terminated  */
#define __ERR_RX_THREAD	     0xfffd                                     /*  Flag of the TCP Connection  */



/*  
 *  Local socket struct
 *  The struct links a remote socket ID to a remote IP endpoint 
 */
typedef struct {
    SOCKET         socketRemote;                                        /*  Remote socket ID            */
    SOCKADDR_IN    sockaddrRemote;                                      /*  Remote IP endpoint          */
    bool           state;
} __MSOCKET;

/*********************************************************************************************************
** Function name:           RECEIVEDATA
**
** 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 
*********************************************************************************************************/
typedef void (CALLBACK *EPC_RECEIVE_DATA)(void           *pvOutput, 
                                          unsigned long   ulIpaddr, 
                                          unsigned short  ulPort, 
                                          char           *pcData, 
                                          unsigned short  usDatalength);



class EPC_TCP_IP  
{
private:  

    /*  
     *  Configuration Information of the local protocol  
     */
    SOCKET          socketLocal;                                        /*  Local socket ID             */
    SOCKADDR_IN     sockaddrLocal[__LOCAL_ETHER_MAX];                   /*  Table of the local address  */
    HOSTENT        *phostentLocal;                                      /*  Host information            */
    SOCKADDR_IN     sockaddrLocalnow;                                   /*  Local Address used now      */
    char            pcLocalhstname[__HST_NAME_SIZE];                    /*  Pointer of local host name  */
    int             iProtocoltype;                                      /*  Protocol type: TCP or UDP   */
    int             iMode;                                              /*  Protocol Mode: Server/Client*/

    /*  
     *  Configuration Information of the remote protocol 
     */
    SOCKADDR_IN     sockaddrRemotenow;                                  /*  Remote Address used now     */
    __MSOCKET       msocketRemote[__LISTEN_MAX_NUM];                    /*  Table of the remote msocket */
    int             iRemoteindex;                                       /*  Index of the remote endpoint*/
    int             iRxbufsize;                                         /* Maximum length of the        */
                                                                        /* received buffer              */

    char           *pcRxbuf;                                            /*  Buffer of the received data */
    int             iRxbuflen;                                          /*  Length of the received buf  */    
    
    void           *pvUserinput;                                        /*  The user pointer            */

    /*  
     *  Definition of some server threads    
     */
    HANDLE          handleTcpaccept;                                    /*  Handle of TCP accept thread */
    HANDLE          handleTcpreceive;                                   /*  Handle of TCP Receive thread*/
    HANDLE          handleEvent[2];                                     /*  Handle of the events        */

    unsigned long   ulTcpacceptID;                                      /*  ID of the TCP accept thread */
    unsigned long   ulTcpReceiveID;                                     /*  ID of the TCP receive thread*/


/*********************************************************************************************************
** Function name:           EPC_TCP_IP
**
** Descriptions:            It's a construction function.
**
** input parameters:        callbackFun    Callback function of the protocol receive data
** output parameters:       pulIpaddr      List of the local IP address 
**                          piNum          Total number of the local IP address
**                          iRxbufsize     Maximum length of the received buffer
**                          pvInput        Pointer of the user
** Returned value:          None 
*********************************************************************************************************/
public:	EPC_TCP_IP(unsigned long     *pulIpaddr, 
                   int               *piNum, 
                   int                iRxbufsize,
                   EPC_RECEIVE_DATA   callbackFun, 
                   void              *pvInput);

/*********************************************************************************************************
** Function name:           ~EPC_TCP_IP
**
** Descriptions:            It's a destruction function.
**
** input parameters:        None
** output parameters:       None
** Returned value:          None 
*********************************************************************************************************/           
public: virtual ~EPC_TCP_IP();

/*********************************************************************************************************
** Function name:           epcsProtocolStartup
**
** Descriptions:            Startup protocol by some way 
**
** input parameters:        iProtocoltype       Protocol type
**                          ulLocalipaddr       Local IP address
**                          usLocalport         Local port
**                          ulRemoteipaddr      Remote IP address 
**                          usRemoteport        Remote Port
**                          iMode               Protocol Mode
** output parameters:       None                
** Returned value:          true: Startup is success   false: Startup is fail 
*********************************************************************************************************/        
public: virtual bool epcProtocolStartup(int            iProtocoltype, 
                                unsigned long  ulLocalipaddr, 
                                unsigned short usLocalport, 
                                unsigned long  ulRemoteipaddr, 
                                unsigned short usRemoteport, 
                                int            iMode); 

/*********************************************************************************************************
** Function name:           epcsProtocolSenddata
**
** Descriptions:            Send data to the remote IP endpoint.
**
** input parameters:        ulIpaddr       Remote IP Address
**                          ulPort         Remote port
**                          pcData         Pointer of the received data
**                          iDatalen       Length of the received data 
** output parameters:       None
** Returned value:          true: Send data is success   false: Send data is fail  
*********************************************************************************************************/ 
public: virtual bool epcProtocolSenddata(unsigned long   ulIpaddr, 
                                 unsigned short  usPort, 
                                 char           *pcData, 
                                 int             iDatalen);

/*********************************************************************************************************
** Function name:           epcsProtocolClose
**
** Descriptions:            Close the IP endpoint.
**
** input parameters:        ulIpaddr  the IP Address
**                          ulPort    the port                          
** output parameters:       None
** Returned value:          true: Closing is success   false: Closing data is fail  
*********************************************************************************************************/ 
public: virtual bool epcProtocolClose(unsigned long ulIpaddr, unsigned short usPort);

/*********************************************************************************************************
** Function name:           epcProtocolGetVersion
**
** Descriptions:            Get version of the driver
**
** input parameters:        None                         
** output parameters:       None
** Returned value:          Version of the driver. For example, 
**                          if the value is '0x100', the version is 'V1.00'
*********************************************************************************************************/ 
public: virtual unsigned long epcProtocolGetVersion(void);    

/*********************************************************************************************************
** Function name:           protocolReceivedata
**
** 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 
*********************************************************************************************************/ 
public: EPC_RECEIVE_DATA epcProtocolReceivedata;

};

/*********************************************************************************************************
** Function name:           CreateObjectofEPC_TCP_IP
**
** Descriptions:            Export EPC_TCP_IP class
**
** input parameters:        callbackFun    Callback function of the protocol receive data
** output parameters:       pulIpaddr      List of the local IP address 
**                          piNum          Total number of the local IP address
**                          iRxbufsize     Maximum length of the received buffer
**                          pvInput        Pointer of the user
** Returned value:          None 
*********************************************************************************************************/
extern "C" __declspec(dllexport) EPC_TCP_IP* CreateObjectofEPC_TCP_IP(unsigned long      *pulIpaddr, 
                                                                      int                *piNum, 
                                                                      int                 iRxbufsize,
                                                                      EPC_RECEIVE_DATA    callbackFun, 
                                                                      void               *pvInput);

#endif                                                                  /*   __EPC_TCP_IP             */

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

⌨️ 快捷键说明

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