/// <summary>
        /// 异步获取指定服务契约的通道
        /// </summary>
        public void GetChannelAsync()
        {
            if (_connectAction == null)
            {
                _connectAction = new Connect(OnConnect);
            }

            _connectAction.BeginInvoke(null, null);
        }
Exemplo n.º 2
0
        public StreetDBServer(int port)
        {
            Socket     s  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress  ip = IPAddress.Any;
            IPEndPoint ep = new IPEndPoint(IPAddress.Any, port);

            try
            {
                s.Bind(ep);
                s.Listen(10);
                Console.WriteLine("Server started");
                while (true)
                {
                    Socket ns = s.Accept();
                    Console.WriteLine(ns.RemoteEndPoint.ToString());
                    StringBuilder strBuilder = new StringBuilder();
                    byte[]        buff       = new byte[1024];
                    int           l          = 0;
                    do
                    {
                        l = ns.Receive(buff);
                        strBuilder.Append(Encoding.Unicode.GetString(buff));
                    } while (ns.Available > 0);
                    Connect conn = new Connect(SendResult);
                    string  str  = strBuilder.ToString().TrimEnd('\0');
                    conn.BeginInvoke(ns, str, null, null);
                    Console.WriteLine($"Reveived : {strBuilder.ToString()}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                s?.Shutdown(SocketShutdown.Both);
                s?.Close();
                s = null;
            }
        }