测试的代码:
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”的已处置对象。
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”的已处置对象。
解决方案 »
- Bitmap bitmap = new Bitmap(stream) 参数无效
- 发了两次贴都没解决
- datagridview checkbox无法排序?
- asp.net 合并word
- 求C#读取并合并两个wmf文件或emf文件成1个文件的代码,马上给分
- 放分了
- 条形码用字体是怎么来实现的啊?
- 为什么安装Studio.net2003时,一直提示系统某些组件与软件要求不符而不能安装?我的系统是XP.SP2.请教是什么原因?
- AE开发频繁打开和编辑同一个要素类出现系统资源不足的异常
- 如何捕捉用户点击关闭钮时产生的事件?
- 如何在程序中修改Exchange中某个属性的值???请指教!!!!
- 找两个东西,50分,进者有分!
sct = new TcpClient("第二次服务器",端口号);