测试的代码:
using System;
using System.Net;
using System.Net.Sockets;namespace Jiuyong.ToolBox.PointToPoint.Client
{
/// <summary>
/// SocketTest 的摘要说明。
/// </summary>
public class SocketTest
{
static Socket sct =new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);
public SocketTest()
{
IPEndPoint[] Servers=new IPEndPoint[]{new IPEndPoint(IPAddress.Parse("61.132.74.16"),80)
 ,new IPEndPoint(IPAddress.Parse("61.132.74.15"),80)
 ,new IPEndPoint(IPAddress.Parse("61.132.74.18"),80)
 };
foreach(IPEndPoint s in Servers)
{
Console.WriteLine(GuestTo(s));
}
} public static string GuestTo(IPEndPoint s)
{
byte[] p=new byte[1024];
Console.WriteLine("访问主机: "+s.ToString());
sct.Connect(s);//连接第二个服务器报错。 Console.WriteLine(">>> "+sct.Send(System.Text.Encoding.Default.GetBytes("GET / HTTP/1.1 \r\n\r\n")).ToString());
Console.WriteLine("<<< "+ sct.Receive(p).ToString()); sct.Shutdown(SocketShutdown.Both);
sct.Close(); return System.Text.Encoding.Default.GetString(p).TrimEnd('\0');
}
}
}
但是出现了异常:
未处理的“System.ObjectDisposedException”类型的异常出现在 system.dll 中。其他信息: 无法访问名为“System.Net.Sockets.Socket”的已处置对象。