TcpClient tc = new TcpClient();
            IPAddress test = IPAddress.Parse("192.168.1.100");
            tc.Connect(test, 80);和
tc.Connect("192.168.1.100", 80);

解决方案 »

  1.   

    两者的源码:public void Connect(string hostname, int port)
    {
        if (Logging.On)
        {
            Logging.Enter(Logging.Sockets, this, "Connect", hostname);
        }
        if (this.m_CleanedUp)
        {
            throw new ObjectDisposedException(base.GetType().FullName);
        }
        if (hostname == null)
        {
            throw new ArgumentNullException("hostname");
        }
        if (!ValidationHelper.ValidateTcpPort(port))
        {
            throw new ArgumentOutOfRangeException("port");
        }
        if (this.m_Active)
        {
            throw new SocketException(SocketError.IsConnected);
        }
        IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);
        Exception exception = null;
        Socket socket = null;
        Socket socket2 = null;
        try
        {
            if (this.m_ClientSocket == null)
            {
                if (Socket.OSSupportsIPv4)
                {
                    socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
                if (Socket.OSSupportsIPv6)
                {
                    socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                }
            }
            foreach (IPAddress address in hostAddresses)
            {
                try
                {
                    if (this.m_ClientSocket == null)
                    {
                        if ((address.AddressFamily == AddressFamily.InterNetwork) && (socket2 != null))
                        {
                            socket2.Connect(address, port);
                            this.m_ClientSocket = socket2;
                            if (socket != null)
                            {
                                socket.Close();
                            }
                        }
                        else if (socket != null)
                        {
                            socket.Connect(address, port);
                            this.m_ClientSocket = socket;
                            if (socket2 != null)
                            {
                                socket2.Close();
                            }
                        }
                        this.m_Family = address.AddressFamily;
                        this.m_Active = true;
                        goto Label_01BF;
                    }
                    if (address.AddressFamily == this.m_Family)
                    {
                        this.Connect(new IPEndPoint(address, port));
                        this.m_Active = true;
                        goto Label_01BF;
                    }
                }
                catch (Exception exception2)
                {
                    if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException))
                    {
                        throw;
                    }
                    exception = exception2;
                }
            }
        }
        catch (Exception exception3)
        {
            if (((exception3 is ThreadAbortException) || (exception3 is StackOverflowException)) || (exception3 is OutOfMemoryException))
            {
                throw;
            }
            exception = exception3;
        }
        finally
        {
            if (!this.m_Active)
            {
                if (socket != null)
                {
                    socket.Close();
                }
                if (socket2 != null)
                {
                    socket2.Close();
                }
                if (exception != null)
                {
                    throw exception;
                }
                throw new SocketException(SocketError.NotConnected);
            }
        }
    Label_01BF:
        if (Logging.On)
        {
            Logging.Exit(Logging.Sockets, this, "Connect", (string) null);
        }
    }public void Connect(IPAddress address, int port)
    {
        if (Logging.On)
        {
            Logging.Enter(Logging.Sockets, this, "Connect", address);
        }
        if (this.m_CleanedUp)
        {
            throw new ObjectDisposedException(base.GetType().FullName);
        }
        if (address == null)
        {
            throw new ArgumentNullException("address");
        }
        if (!ValidationHelper.ValidateTcpPort(port))
        {
            throw new ArgumentOutOfRangeException("port");
        }
        IPEndPoint remoteEP = new IPEndPoint(address, port);
        this.Connect(remoteEP);
        if (Logging.On)
        {
            Logging.Exit(Logging.Sockets, this, "Connect", (string) null);
        }
    }
      

  2.   

    简化一下,其源代码基本上是这样的IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);
    Socket socket = null;
    Socket socket2 = null;
    if (this.m_ClientSocket == null)
    {
        if (Socket.OSSupportsIPv4)
        {
            socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        if (Socket.OSSupportsIPv6)
        {
            socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
        }
    }
    foreach (IPAddress address in hostAddresses)
    {
        if ((address.AddressFamily == AddressFamily.InterNetwork) && (socket2 != null)) //使用ipv4
        {
            socket2.Connect(address, port);
            this.m_ClientSocket = socket2;
            if (socket != null)
            {
                socket.Close();
            }
        }
        else if (socket != null)    //使用ipv6
        {
            socket.Connect(address, port);
            this.m_ClientSocket = socket;
            if (socket2 != null)
            {
                socket2.Close();
            }
        }
    }
      

  3.   

    IPAddress test = IPAddress.Parse("192.168.1.100");
    tc.Connect(test, 80);
    这个里面只可以是IP地址字符串。不能写成IPAddress.Parse("www.baidu.com");
    tc.Connect("192.168.1.100", 80);这个里面可以是IP字符串,也可以是主机,比如www.baidu.com
      

  4.   

    源代码都给你了还不会看……
    Connect(String, Int32)会将这个主机或IP的字符串转化为IPAddress的一个数组,然后遍历这个循环,根据不同情况,可能调用Connect(IPAddress, Int32),也可能调用Connect(IPEndPoint)
    而Connect(IPAddress, Int32)调用的是Connect(IPEndPoint)
    所以,就你的提问,这两个方法最终都会调用Connect(IPEndPoint),只是传入字符串的话会做更多的操作……明白了不……
      

  5.   

    那如果tc.Connect("192.168.1.100", 80);不通
    TcpClient tc = new TcpClient();
                IPAddress test = IPAddress.Parse("192.168.1.100");
                tc.Connect(test, 80);
    肯定不通吧?
    现在是tc.Connect("192.168.1.100", 80);不通 但是TcpClient tc = new TcpClient();
                IPAddress test = IPAddress.Parse("192.168.1.100");
                tc.Connect(test, 80);
    能通
      

  6.   

    那可真不一定,Connect(string hostname, int port)
    看函数签名,这个string参数应该传入的是主机名,而不是IP
      

  7.   


    可见你没有看懂代码  IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);
      

  8.   

    人家可以仿照源代码,来通过dns类来获得IPAddress啊。学.ney还是要以自己独立看懂一些源代码为基础能力,而不是听一般的所谓解释。