TcpClient tc = new TcpClient();
IPAddress test = IPAddress.Parse("192.168.1.100");
tc.Connect(test, 80);和
tc.Connect("192.168.1.100", 80);
IPAddress test = IPAddress.Parse("192.168.1.100");
tc.Connect(test, 80);和
tc.Connect("192.168.1.100", 80);
解决方案 »
- 求一个 VS2010开发 OFFICE (主要是WORD) 的DEMO
- 如何在桌面应用程序中加日历!!!
- 关于BHO,webbrowser,定位截屏及呼出模态窗口的问题
- 50分了,进来吧
- 求教,关于将DataTable中的数据到入到Access中
- 怎么样才能查看到设置的是连接的哪个图片或图标?
- 紧急!!!请高手指点:怎样实现动态控制小数位数
- (查无结果,比较难)C#。NET中数据按月份来分页
- 有没有c#中读取acess的memo字段(含图片及文本)到richtextbox的例子,谢谢!
- listView怎么实现图片滑动效果???????
- 用asp.net代码拼写的sql语句,关键字为"中文"时,检索结果为空
- 请教:我使用CodeSmith自带的模板怎样建立一个完整的实体类?
{
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);
}
}
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();
}
}
}
tc.Connect(test, 80);
这个里面只可以是IP地址字符串。不能写成IPAddress.Parse("www.baidu.com");
tc.Connect("192.168.1.100", 80);这个里面可以是IP字符串,也可以是主机,比如www.baidu.com
Connect(String, Int32)会将这个主机或IP的字符串转化为IPAddress的一个数组,然后遍历这个循环,根据不同情况,可能调用Connect(IPAddress, Int32),也可能调用Connect(IPEndPoint)
而Connect(IPAddress, Int32)调用的是Connect(IPEndPoint)
所以,就你的提问,这两个方法最终都会调用Connect(IPEndPoint),只是传入字符串的话会做更多的操作……明白了不……
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);
能通
看函数签名,这个string参数应该传入的是主机名,而不是IP
可见你没有看懂代码 IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);