public static Socket ConnectTCPSocket(IPAddressSet addrSet, int port) { IPAddress primary = addrSet.Primary; if (primary == null) { return(null); } Socket s = new Socket(primary.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { s.Connect(new IPEndPoint(primary, port)); return(s); } catch (Exception ex) { IPAddress secondary = addrSet.Secondary; if (secondary == null) { throw ex; } s = new Socket(secondary.AddressFamily, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(secondary, port)); return(s); } }
protected virtual void MakeConnection() { #if false //まずSOCKSを使うべきかどうかを判定する if (_socks != null) { IPAddressSet a = null; try { a = new IPAddressSet(IPAddress.Parse(_socks.DestName)); } catch (FormatException) { try { a = new IPAddressSet(_socks.DestName); } catch (Exception) { //ここで名前解決できずとも、SOCKSサーバが解決できるかもしれないのでエラーにはしない } } if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a)) //SOCKSを使わないことが確定 { _addressSet = a; _host = _socks.DestName; _port = _socks.DestPort; _socks = null; } } string dest = _socks == null ? _host : _socks.ServerName; int port = _socks == null ? _port : _socks.ServerPort; string msg = _socks == null?GetHostDescription() : "SOCKS Server"; if (_addressSet == null) { try { _addressSet = new IPAddressSet(IPAddress.Parse(dest)); //最初からIPアドレス形式のときは手で変換。そうでないとDNSの逆引きをしてタイムアウト、とかややこしいことが起こる } catch (FormatException) { _errorMessage = String.Format("The {0} {1} was not found.", msg, dest); _addressSet = new IPAddressSet(dest); } } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; if (_socks != null) { _errorMessage = "An error occurred while SOCKS negotiation."; _socks.Connect(_socket); _host = _socks.DestName; _port = _socks.DestPort; } #endif //上記の#ifをSOCKSなしに書き換え string dest = _host; int port = _port; string msg = GetHostDescription(); if (_addressSet == null) { IPAddress address; if (IPAddress.TryParse(dest, out address)) { _addressSet = new IPAddressSet(address); } else { _addressSet = new IPAddressSet(dest); //DNSで } } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; _tcpConnected = true; }
public static Socket ConnectTCPSocket(IPAddressSet addrSet, int port) { IPAddress primary = addrSet.Primary; if (primary == null) return null; Socket s = new Socket(primary.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { s.Connect(new IPEndPoint(primary, port)); return s; } catch (Exception ex) { IPAddress secondary = addrSet.Secondary; if (secondary == null) throw ex; s = new Socket(secondary.AddressFamily, SocketType.Stream, ProtocolType.Tcp); s.Connect(new IPEndPoint(secondary, port)); return s; } }
protected virtual void MakeConnection() { #if false //�܂�SOCKS��g���ׂ����ǂ����肷�� if (_socks != null) { IPAddressSet a = null; try { a = new IPAddressSet(IPAddress.Parse(_socks.DestName)); } catch (FormatException) { try { a = new IPAddressSet(_socks.DestName); } catch (Exception) { //�����Ŗ��O����ł����Ƃ�ASOCKS�T�[�o������ł��邩�����Ȃ��̂ŃG���[�ɂ͂��Ȃ� } } if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a)) { //SOCKS��g��Ȃ����Ƃ��m�� _addressSet = a; _host = _socks.DestName; _port = _socks.DestPort; _socks = null; } } string dest = _socks == null ? _host : _socks.ServerName; int port = _socks == null ? _port : _socks.ServerPort; string msg = _socks == null ? GetHostDescription() : "SOCKS Server"; if (_addressSet == null) { try { _addressSet = new IPAddressSet(IPAddress.Parse(dest)); //�ŏ�����IP�A�h���X�`���̂Ƃ��͎�ŕϊ��B�����łȂ���DNS�̋t��������ă^�C���A�E�g�A�Ƃ���₱�������Ƃ��N���� } catch (FormatException) { _errorMessage = String.Format("The {0} {1} was not found.", msg, dest); _addressSet = new IPAddressSet(dest); } } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; if (_socks != null) { _errorMessage = "An error occurred while SOCKS negotiation."; _socks.Connect(_socket); _host = _socks.DestName; _port = _socks.DestPort; } #endif //��L��#if��SOCKS�Ȃ��ɏ������� string dest = _host; int port = _port; string msg = GetHostDescription(); if (_addressSet == null) { IPAddress address; if (IPAddress.TryParse(dest, out address)) _addressSet = new IPAddressSet(address); else _addressSet = new IPAddressSet(dest); //DNS�� } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; _tcpConnected = true; }
protected virtual void MakeConnection() { #if false //まずSOCKSを使うべきかどうかを判定する if (_socks != null) { IPAddressSet a = null; try { a = new IPAddressSet(IPAddress.Parse(_socks.DestName)); } catch (FormatException) { try { a = new IPAddressSet(_socks.DestName); } catch (Exception) { //ここで名前解決できずとも、SOCKSサーバが解決できるかもしれないのでエラーにはしない } } if (a != null && !SocksApplicapable(_socks.ExcludingNetworks, a)) { //SOCKSを使わないことが確定 _addressSet = a; _host = _socks.DestName; _port = _socks.DestPort; _socks = null; } } string dest = _socks == null ? _host : _socks.ServerName; int port = _socks == null ? _port : _socks.ServerPort; string msg = _socks == null ? GetHostDescription() : "SOCKS Server"; if (_addressSet == null) { try { _addressSet = new IPAddressSet(IPAddress.Parse(dest)); //最初からIPアドレス形式のときは手で変換。そうでないとDNSの逆引きをしてタイムアウト、とかややこしいことが起こる } catch (FormatException) { _errorMessage = String.Format("The {0} {1} was not found.", msg, dest); _addressSet = new IPAddressSet(dest); } } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; if (_socks != null) { _errorMessage = "An error occurred while SOCKS negotiation."; _socks.Connect(_socket); _host = _socks.DestName; _port = _socks.DestPort; } #endif //上記の#ifをSOCKSなしに書き換え string dest = _host; int port = _port; string msg = GetHostDescription(); if (_addressSet == null) { IPAddress address; if (IPAddress.TryParse(dest, out address)) _addressSet = new IPAddressSet(address); else _addressSet = new IPAddressSet(dest); //DNSで } _errorMessage = String.Format("Failed to connect {0} {1}. Please check the address and the port.", msg, dest); _socket = NetUtil.ConnectTCPSocket(_addressSet, port); _connectedAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address; _tcpConnected = true; }