c#套接字没有超时

时间:2010-09-30 10:10:21

标签: .net sockets c#-4.0

我有以下c#代码来查询whois服务器,偶尔我会点击服务器而套接字永远不会超时接收(不幸的是,如果我突然再次点击同一台服务器问题就会消失)。我已经设置了ReceiveTimeout值,有人知道这里出了什么问题吗?

string whoisServer = "whois.moniker.com";
Socket s = null;
IPAddress ipAddress;
ipAddress = Dns.GetHostEntry(whoisServer).AddressList[0];                

s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ReceiveTimeout = 10000;
s.SendTimeout = 10000;
s.Connect(new IPEndPoint(ipAddress, 43));
s.Send(Encoding.ASCII.GetBytes(domain + "\r\n"));
byte[] buffer = new byte[1024];
int recv = s.Receive(buffer);
while (recv > 0)
{
    ret += Encoding.ASCII.GetString(buffer, 0, recv);
    recv = s.Receive(buffer);
}
s.Shutdown(SocketShutdown.Both);
s.Close();

0 个答案:

没有答案