ConnectTCPSocket() public static method

public static ConnectTCPSocket ( IPAddress addr, int port, int timeout ) : Socket
addr System.Net.IPAddress
port int
timeout int
return Socket
Exemplo n.º 1
0
        public void TestTimeout()
        {
            Exception e = null;

            try {
                Socket s = NetUtil.ConnectTCPSocket(new IPAddress46Pair(IPAddress.Parse("1.1.1.1")), 10); //接続できないはずのものをテスト
            } catch (Exception ex) {
                e = ex;
            }

            Assert.IsTrue(e != null);
            Assert.AreEqual("TIMEOUT", e.Message);
        }
Exemplo n.º 2
0
        protected virtual void MakeConnection()
        {
            //まずSOCKSを使うべきかどうかを判定する
            IProtocolOptions opt = ProtocolsPlugin.Instance.ProtocolOptions;

            if (opt.UseSocks && SocksApplicapable(opt.SocksNANetworks, IPAddressList.SilentGetAddress(_host)))
            {
                _socks            = new Socks();
                _socks.Account    = opt.SocksAccount;
                _socks.Password   = opt.SocksPassword;
                _socks.DestName   = _host;
                _socks.DestPort   = (short)_port;
                _socks.ServerName = opt.SocksServer;
                _socks.ServerPort = (short)opt.SocksPort;
            }

            string dest = _socks == null ? _host : _socks.ServerName;
            int    port = _socks == null ? _port : _socks.ServerPort;

            IPAddress address = null;

            if (IPAddress.TryParse(dest, out address))
            {
                _addressSet = new IPAddressList(address); //最初からIPアドレス形式のときは手で変換。そうでないとDNSの逆引きをしてタイムアウト、とかややこしいことが起こる
            }
            else                                          //ホスト名形式
            {
                StartOverridingErrorMessage(String.Format(PEnv.Strings.GetString("Message.AddressNotResolved"), dest));
                _addressSet = new IPAddressList(dest);
                EndOverridingErrorMessage();
            }

            StartOverridingErrorMessage(String.Format(PEnv.Strings.GetString("Message.FailedToConnectPort"), dest, port));
            _socket = NetUtil.ConnectTCPSocket(_addressSet, port);
            EndOverridingErrorMessage();
            _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address;

            if (_socks != null)
            {
                _errorMessage = "An error occurred in SOCKS negotiation.";
                _socks.Connect(_socket);
                //接続成功したら_host,_portを元に戻す
                _host = _socks.DestName;
                _port = _socks.DestPort;
            }

            _tcpConnected = true;
        }
Exemplo n.º 3
0
        public void TestSuccessful()
        {
            Socket s = NetUtil.ConnectTCPSocket(new IPAddress46Pair(IPAddress.Loopback), 8888); //listenしてる適当なポートにつなぐ

            Assert.IsTrue(s.Connected);
        }