开发者

Is connecting a TcpClient object with host string and port number slower than using an IPEndpoint?

TcpClient has one Connect method which takes a hostname as a string and a port.

TcpClient.Connect(String hostname, Int32 port)

There is another variant which takes an IPEndPoint.

TcpClient.Connect(IPEndPoint endpoint)

I assume that the first variant above has to use a DNS lookup to get the IP address from the 开发者_C百科hostname before establishing the TCP connection while the second does not need to do so. I don't know if the first one is optimized to check whether the hostname just contains the direct IP address or if it does a DNS lookup each time.

So my question is, Is the second variant faster when I already know the IP address?

P.S.

I am developing for Windows Mobile (6.1) and testing it over a router in my local network. When the router is connected to the open internet, the connections happen fast and I get a quick response from the server. But when I am only using my local network (i.e. router is disconnected from the open internet), connections seem to take forever. Don't know if the open internet somehow speeds up DNS resolution.


It happens just the way you assumed, the first overload (hostname, port) internally performs a DNS lookup and then calls the second overload (IPEndPoint). If you want to see what exactly happens, you could download .NET Reflector (http://www.red-gate.com/products/dotnet-development/reflector/) and inspect the TcpClient class.

By reading the background information it looks like the device still tries to use the DNS it can no longer connect to, hitting the timeout. I don't know enough about networking to back that assumption up, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜