private void frmClient_Load(object sender, EventArgs e) { try { // 加个委托显示msg,因为on系列都是在工作线程中调用的,ui不允许直接操作 AddMsgDelegate = new ShowMsg(AddMsg); // 创建监听器对象 pListener = HPSocketSdk.Create_HP_TcpClientListener(); // 创建 Socket 对象 pClient = HPSocketSdk.Create_HP_TcpClient(pListener); // 设置 Socket 监听器回调函数 OnSendCallback = new HPSocketSdk.HP_FN_OnSend(OnSend); OnConnectCallback = new HPSocket.HPSocketSdk.HP_FN_OnConnect(OnConnect); OnReceiveCallback = new HPSocketSdk.HP_FN_OnReceive(OnReceive); OnCloseOnClose = new HPSocketSdk.HP_FN_OnClose(OnClose); OnErrorCallback = new HPSocketSdk.HP_FN_OnError(OnError); // 设置 Socket 监听器回调函数 HPSocketSdk.HP_Set_FN_Client_OnConnect(pListener, OnConnectCallback); HPSocketSdk.HP_Set_FN_Client_OnSend(pListener, OnSendCallback); HPSocketSdk.HP_Set_FN_Client_OnReceive(pListener, OnReceiveCallback); HPSocketSdk.HP_Set_FN_Client_OnClose(pListener, OnCloseOnClose); HPSocketSdk.HP_Set_FN_Client_OnError(pListener, OnErrorCallback); SetAppState(EnAppState.ST_STOPED); } catch (Exception ex) { AddMsg(ex.Message); } }
/// <summary> /// 创建socket监听&服务组件 /// </summary> /// <param name="isUseDefaultCallback">是否使用tcpserver类默认回调函数</param> /// <returns></returns> protected virtual bool CreateListener() { if (IsCreate == true || pListener != IntPtr.Zero || pClient != IntPtr.Zero) { return(false); } pListener = HPSocketSdk.Create_HP_TcpClientListener(); if (pListener == IntPtr.Zero) { return(false); } pClient = HPSocketSdk.Create_HP_TcpClient(pListener); if (pClient == IntPtr.Zero) { return(false); } IsCreate = true; return(true); }