Exemplo n.º 1
0
        /// <summary>
        /// 连接服务端
        /// </summary>
        private void BtnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ip   = txtboxIP.Text;
                int    port = Convert.ToInt32(txtboxPort.Text);

                // 如果当前已经存在Socket,则先关闭当前Socket
                if (clientSocket != null)
                {
                    clientSocket.Close();
                }

                clientSocket = new TSocketClient(ip, port);

                //clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //clientSocket.Connect(new IPEndPoint(ip, port));
                //txtboxInfo.Text = $"【连接成功】 我方端口 {clientSocket.LocalEndPoint.ToString()}\r\n" + txtboxInfo.Text;
                //Thread th = new Thread(ReceiveMsg)
                //{
                //    IsBackground = true
                //};
                //th.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("【连接失败】 " + ex.Message);
                return;
            }
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// 接收服务端发来的信息
        ///// </summary>
        //public void ReceiveMsg()
        //{
        //    byte[] partialBuffer = null;
        //    while (true)
        //    {

        //        try
        //        {
        //            byte[] result = new byte[2048];

        //            ////通过clientSocket接收数据
        //            //int receiveNumber = connection.Receive(result);
        //            ////把接受的数据从字节类型转化为字符类型
        //            //string recStr = Encoding.Unicode.GetString(result, 0, receiveNumber);


        //            int receiveNumber = clientSocket.Receive(result);
        //            byte[] buffer = new byte[receiveNumber];
        //            Buffer.BlockCopy(result, 0, buffer, 0, receiveNumber);

        //            if (partialBuffer != null)
        //            {
        //                receiveNumber += partialBuffer.Length;
        //                buffer = partialBuffer.Concat(buffer).ToArray();
        //            }

        //            ProtocolHelper.MMO_MemoryStream ms = new ProtocolHelper.MMO_MemoryStream(buffer);
        //            int length = ms.ReadUShort();
        //            ms.Close();

        //            if (length < buffer.Length)
        //            {
        //                partialBuffer = buffer;
        //                continue;
        //            }


        //            partialBuffer = null;
        //            byte[] unpackedMsg = ProtocolHelper.UnpackData(buffer);
        //            string recStr = Encoding.Unicode.GetString(unpackedMsg, 0, unpackedMsg.Length);



        //            //获取当前客户端的ip地址
        //            IPAddress clientIP = (clientSocket.RemoteEndPoint as IPEndPoint).Address;
        //            //获取客户端端口
        //            int clientPort = (clientSocket.RemoteEndPoint as IPEndPoint).Port;
        //            string sendStr = clientIP + ":" + clientPort.ToString() + "--->" + recStr;
        //            //显示内容
        //            txtboxInfo.Dispatcher.BeginInvoke(

        //                    new Action(() => { txtboxInfo.Text = $"【接收信息】 {sendStr}\r\n" + txtboxInfo.Text; }), null);

        //        }
        //        catch (Exception ex)
        //        {
        //            // 出现异常,关闭连接
        //            clientSocket.Shutdown(SocketShutdown.Both);
        //            clientSocket.Close();
        //            txtboxInfo.Dispatcher.BeginInvoke(

        //                    new Action(() => { txtboxInfo.Text = "\r\n" + $"【信息接收异常】 {ex.Message}\r\n" + txtboxInfo.Text; }), null);
        //            break;
        //        }
        //    }
        //}

        /// <summary>
        /// 连接服务端
        /// </summary>
        private void BtnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 如果当前已经存在Socket,则先关闭当前Socket
                if (ClientSocket != null)
                {
                    ClientSocket.Close();
                }

                ClientSocket = new TSocketClient(txtboxIP.Text, Convert.ToInt32(txtboxPort.Text));

                txtboxInfo.Text = $"【连接成功】 我方端口 {ClientSocket._Socket.LocalEndPoint.ToString()}\r\n" + txtboxInfo.Text;
                //Thread th = new Thread(ClientSocket.RecvMsg)
                //{
                //    IsBackground = true
                //};
                //th.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("【连接失败】 " + ex.Message);
                return;
            }
        }
Exemplo n.º 3
0
        public void ReceiveMsg(object clientSocket)
        {
            TSocketClient connection = (TSocketClient)clientSocket;

            while (true)
            {
                try
                {
                    if (!this.IsDispose && connection._Socket.Connected)
                    {
                        ReceiveSize = connection._Socket.Receive(this.Buffers);
                        if (ReceiveSize > 0)
                        {
                            byte[] rbuff = new byte[ReceiveSize];
                            Array.Copy(this.Buffers, rbuff, ReceiveSize);
                            var msgs = PHelper.UnpackData(rbuff, ReceiveSize);
                            foreach (var msg in msgs)
                            {
                                connection.Receive(msg);
                            }
                        }
                    }
                }
                catch
                {
                    connection.Close();
                    break;
                }
            }
        }