Exemplo n.º 1
0
        private void BtnSend_Click(object sender, EventArgs e)
        {
            string sendContent = TxtMsg.Text;

            if (sendContent.Length < 1)
            {
                return;
            }
            if (ChkLbClients.Items.Count < 1)
            {
                return;
            }

            try
            {
                byte[] sendBytes = Encoding.Default.GetBytes(sendContent);

                for (int i = 0; i < ChkLbClients.Items.Count; i++)
                {
                    IntPtr connId = (IntPtr)Convert.ToInt32(ChkLbClients.Items[i]);
                    if (ChkLbClients.GetItemChecked(i))
                    {
                        _server.Send(connId, sendBytes, sendBytes.Length);
                    }
                }

                TxtMsg.Text = string.Empty;
            }
            catch (Exception exc)
            {
                AddLog($"发送失败:{exc.Message}");
            }
        }
Exemplo n.º 2
0
 private HandleResult OnClose(IServer sender, IntPtr connId, SocketOperation socketOperation, int errorCode)
 {
     if (errorCode == 0)
     {
         AddLog($"客户端断开连接,连接ID:{connId}");
     }
     else
     {
         AddLog($"客户端连接异常, ,连接ID:{connId},错误代码{errorCode}");
     }
     ChkLbClients.BeginInvoke((Action)(() => { ChkLbClients.Items.Remove(connId.ToString()); }));
     return(HandleResult.Ok);
 }
Exemplo n.º 3
0
        private HandleResult OnAccept(IServer sender, IntPtr connId, IntPtr client)
        {
            // 获取客户端地址
            if (!sender.GetRemoteAddress(connId, out var ip, out var port))
            {
                return(HandleResult.Error);
            }

            string connIdStr = connId.ToString();

            AddLog($"接受客户端连接请求,连接ID({connIdStr}), ip: {ip}, port: {port}");

            if (!ChkLbClients.Items.Contains(connIdStr))
            {
                ChkLbClients.BeginInvoke((Action)(() => { ChkLbClients.Items.Add(connIdStr); }));
            }

            return(HandleResult.Ok);
        }
Exemplo n.º 4
0
        private void BtnDisConnection_Click(object sender, EventArgs e)
        {
            if (ChkLbClients.Items.Count > 0)
            {
                try
                {
                    for (int i = 0; i < ChkLbClients.Items.Count; i++)
                    {
                        IntPtr connId = (IntPtr)Convert.ToInt32(ChkLbClients.Items[i]);

                        if (ChkLbClients.GetItemChecked(i))
                        {
                            _server.Disconnect(connId, true);
                        }
                    }
                }
                catch (Exception exc)
                {
                    AddLog(exc.Message);
                }
            }
        }