示例#1
0
        public static IPAddress GetLocalIP(ServerBindType bindType)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

            foreach (IPAddress address in hostEntry.AddressList)
            {
                if (address.AddressFamily == AddressFamily.InterNetwork)
                {
                    switch (bindType)
                    {
                    case ServerBindType.Any:
                        return(address);

                    case ServerBindType.Public:
                    case ServerBindType.PublicPrivate:
                        if (TcpServerBase <TClient> .IsPrivateIP(address))
                        {
                            break;
                        }
                        return(address);

                    case ServerBindType.Private:
                    case ServerBindType.PrivatePublic:
                        if (!TcpServerBase <TClient> .IsPrivateIP(address))
                        {
                            break;
                        }
                        return(address);
                    }
                }
            }
            foreach (IPAddress address3 in hostEntry.AddressList)
            {
                if (address3.AddressFamily == AddressFamily.InterNetwork)
                {
                    switch (bindType)
                    {
                    case ServerBindType.PublicPrivate:
                        if (!TcpServerBase <TClient> .IsPrivateIP(address3))
                        {
                            break;
                        }
                        return(address3);

                    case ServerBindType.PrivatePublic:
                        if (TcpServerBase <TClient> .IsPrivateIP(address3))
                        {
                            break;
                        }
                        return(address3);
                    }
                }
            }
            throw new SocketException(10049);
        }
示例#2
0
 public void Start(JobProcessor jobProcessor, IPAddress bindAddress, int port)
 {
     if (this.active)
     {
         throw new InvalidOperationException("Already activated.");
     }
     this.active       = true;
     this.jobProcessor = jobProcessor;
     this.socket       = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     this.socket.Bind(new IPEndPoint(bindAddress, port));
     this.socket.Listen(200);
     this.socket.BeginAccept(0, new AsyncCallback(this.EndAccept), this.socket);
     this.LocalEndPoint = new IPEndPoint((bindAddress == IPAddress.Any) ? TcpServerBase <TClient> .GetLocalIP(ServerBindType.PublicPrivate) : bindAddress, (this.socket.LocalEndPoint as IPEndPoint).Port);
 }
示例#3
0
 public void Start(JobProcessor jobProcessor, ServerBindType bindType, int port)
 {
     this.Start(jobProcessor, TcpServerBase <TClient> .GetLocalIP(bindType), port);
 }