Exemple #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = title;
                // 本机测试没必要改地址,有需求请注释或删除
                this.txtIpAddress.ReadOnly = true;

                // 加个委托显示msg,因为on系列都是在工作线程中调用的,ui不允许直接操作
                AddMsgDelegate = new ShowMsg(AddMsg);

                // 创建监听器对象
                pListener = HPSocketSdk.Create_HP_TcpServerListener();

                // 创建 Socket 对象
                pServer = HPSocketSdk.Create_HP_TcpServer(pListener);

                // 设置 Socket 监听器回调函数
                OnOnAcceptCallback       = new HPSocketSdk.HP_FN_OnAccept(OnAccept);
                OnSendCallback           = new HPSocketSdk.HP_FN_OnSend(OnSend);
                OnPrepareListenCallback  = new HPSocket.HPSocketSdk.HP_FN_OnPrepareListen(OnPrepareListen);
                OnReceiveCallback        = new HPSocketSdk.HP_FN_OnReceive(OnReceive);
                OnCloseCallback          = new HPSocketSdk.HP_FN_OnClose(OnClose);
                OnErrorCallback          = new HPSocketSdk.HP_FN_OnError(OnError);
                OnServerShutdownCallback = new HPSocketSdk.HP_FN_OnServerShutdown(OnServerShutdown);

                // 设置 Socket 监听器回调函数
                HPSocketSdk.HP_Set_FN_Server_OnPrepareListen(pListener, OnPrepareListenCallback);
                HPSocketSdk.HP_Set_FN_Server_OnAccept(pListener, OnOnAcceptCallback);
                HPSocketSdk.HP_Set_FN_Server_OnSend(pListener, OnSendCallback);
                HPSocketSdk.HP_Set_FN_Server_OnReceive(pListener, OnReceiveCallback);
                HPSocketSdk.HP_Set_FN_Server_OnClose(pListener, OnCloseCallback);
                HPSocketSdk.HP_Set_FN_Server_OnError(pListener, OnErrorCallback);
                HPSocketSdk.HP_Set_FN_Server_OnServerShutdown(pListener, OnServerShutdownCallback);

                SetAppState(EnAppState.ST_STOPED);
            }
            catch (Exception ex)
            {
                AddMsg(ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// 创建socket监听&服务组件
        /// </summary>
        /// <param name="isUseDefaultCallback">是否使用tcpserver类默认回调函数</param>
        /// <returns></returns>
        protected virtual bool CreateListener()
        {
            if (IsCreate == true || pListener != IntPtr.Zero || pServer != IntPtr.Zero)
            {
                return(false);
            }

            pListener = HPSocketSdk.Create_HP_TcpServerListener();
            if (pListener == IntPtr.Zero)
            {
                return(false);
            }
            pServer = HPSocketSdk.Create_HP_TcpServer(pListener);
            if (pServer == IntPtr.Zero)
            {
                return(false);
            }

            IsCreate = true;

            return(true);
        }