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); }
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; }
public void TestSuccessful() { Socket s = NetUtil.ConnectTCPSocket(new IPAddress46Pair(IPAddress.Loopback), 8888); //listenしてる適当なポートにつなぐ Assert.IsTrue(s.Connected); }