Esempio n. 1
0
        internal void Start()
        {
            if (_acceptor != null && _acceptor.Started)
            {
                return;
            }

            _acceptor = new AsyncSocketSessionAcceptor(_port);
            _acceptor.Backlog = 200;
            _acceptor.ReuseAddress = true;
            _acceptor.PacketEncoder = new DefaultPacketEncoder(_messageBodyEncoder);
            _acceptor.PacketDecoder = new DefaultPacketDecoder(_messageBodyDecoder);
            _acceptor.SessionAccepted += SessionAccepted;
            _acceptor.ExceptionCaught += AcceptorExceptionCaught;
            _acceptor.Start();
        }
Esempio n. 2
0
        void Init()
        {
            IPAddress[] addreses = Dns.GetHostAddresses(Dns.GetHostName());

            foreach (var address in addreses)
            {
                if (address.AddressFamily == AddressFamily.InterNetwork)
                {
                    tbIP.Text = address.ToString();
                    break;
                }
            }

            btnStart.Click += (sender, e) =>
            {
                if (string.IsNullOrWhiteSpace(tbIP.Text))
                {
                    MessageBox.Show("请输入 IP 地址。");
                }

                if (string.IsNullOrWhiteSpace(tbPort.Text))
                {
                    MessageBox.Show("请输入端口。");
                }

                try
                {
                    AppendInfo("TcpServer Starting!");
                    _acceptor = new AsyncSocketSessionAcceptor(
                        new IPEndPoint(IPAddress.Parse(tbIP.Text), int.Parse(tbPort.Text)));
                    _acceptor.Backlog       = 200;
                    _acceptor.ReuseAddress  = true;
                    _acceptor.PacketEncoder = new DefaultPacketEncoder(obj => Encoding.UTF8.GetBytes(obj.ToString()));
                    _acceptor.PacketDecoder = new DefaultPacketDecoder((buffer, startIndex, length, type) =>
                                                                       Encoding.UTF8.GetString(buffer.ByteArray, startIndex, length));
                    _acceptor.SessionAccepted += AcceptorSessionAccepted;
                    _acceptor.ExceptionCaught += AcceptorExceptionCaught;
                    _acceptor.Start();
                    AppendInfo("TcpServer Started!");
                }
                catch (Exception ex)
                {
                    AppendInfo("TcpServer Start Fail!" + ex.ToString());
                }
            };
        }