我加上捕获异常之后,用socketexception 捕获,提示:无法访问名为system.net.sockets.socket的已处置对象。
我的监听程序:
ListenSoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
ListenSoc.Bind(ipLocalEndPoint);
ListenSoc.Listen(5);
ListenSoc.BeginAccept(new AsyncCallback(AcceptCallback),ListenSoc);
接收回掉函数:
public static void AcceptCallback(IAsyncResult ar)
{
connectDone.Set();
Socket listener=(Socket)ar.AsyncState;
Socket handler=listener.EndAccept(ar);
StateObject state=new StateObject();
state.workSocket=handler;
handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback),state)
receiveDone.WaitOne();
}
到底是哪里错了
我的监听程序:
ListenSoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
ListenSoc.Bind(ipLocalEndPoint);
ListenSoc.Listen(5);
ListenSoc.BeginAccept(new AsyncCallback(AcceptCallback),ListenSoc);
接收回掉函数:
public static void AcceptCallback(IAsyncResult ar)
{
connectDone.Set();
Socket listener=(Socket)ar.AsyncState;
Socket handler=listener.EndAccept(ar);
StateObject state=new StateObject();
state.workSocket=handler;
handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback),state)
receiveDone.WaitOne();
}
到底是哪里错了
无法访问名为system.net.sockets.socket的已处置对象。
出错:
无法访问名为system.net.sockets.socket的已处置对象。:
创建一个项目,windows应用程序,放两个按钮,一个启动,一个停止:
启动里面:
IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.18"),1111);
ListenSoc = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp );
ListenSoc.Bind(ipLocalEndPoint);
ListenSoc.Listen(5);
ListenSoc.BeginAccept(new AsyncCallback(AcceptCallback),ListenSoc);
停止按钮里面:
try
{
//ListenSoc.Shutdown(SocketShutdown.Both);
Close();
if(ListenSoc.Connected) MessageBox.Show("无法关闭");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
在家两个回掉函数:
public static void AcceptCallback(IAsyncResult ar)
{
try
{
//connectDone.Set();
byte[] bytes=new byte[1024];
Socket listener=(Socket)ar.AsyncState;
Socket handler=listener.EndAccept(ar);
handler.BeginAccept(new AsyncCallback(AcceptCallback),handler);
handler.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(ReceiveCallback),handler);//handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReadCallback),state);
//receiveDone.WaitOne();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
} }
public static void ReceiveCallback(IAsyncResult ar)
{
Socket aa=(Socket)ar.AsyncState;
int bb=aa.EndReceive(ar);
}
先启动,在关闭,根本不行
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9999);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind((EndPoint)ip);
socket.Listen(10);
socket.BeginAccept(null,null);
} private void button2_Click(object sender, System.EventArgs e)
{
socket.Close();
}
你那个Close是什么???
你说没错,我说有错,是因为你的回掉函数是空,如果你换乘我的程序那样,写一个回掉函数,就会出错了
handler.BeginAccept(new AsyncCallback(AcceptCallback),handler);
handler.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(ReceiveCallback),handler);//handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReadCallback),state);这句是错的
listener.BeginAccept();
handler.BeginReceive(..);
private void button1_Click(object sender, System.EventArgs e)
{
client = new TcpClient("Server",9999);
Byte[] data = System.Text.Encoding.Unicode.GetBytes("我是中国人。");
NetworkStream stream = client.GetStream();
stream.Write(data,0,data.Length);
} private void button2_Click(object sender, System.EventArgs e)
{
if(client != null)
{
client.Close();
}
}服务端
private void button1_Click(object sender, System.EventArgs e)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 9999);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind((EndPoint)ip);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(OnAccept),null);
}
private void Receive()
{
if(packet == null)
{
packet = new byte[4096];
}
commSocket.BeginReceive(packet,0,packet.Length,SocketFlags.None,new AsyncCallback(OnReceive),packet.Length);
}
private void OnAccept(IAsyncResult ar)
{
commSocket = socket.EndAccept(ar);
socket.BeginAccept(new AsyncCallback(OnAccept),null);
Receive();
}
private void OnReceive(IAsyncResult ar)
{
int r = commSocket.EndReceive(ar);
if((r==0) || (packet == null))
{
MessageBox.Show("接收数据时发生错误。。");
return;
}
else
{
string msg = System.Text.Encoding.Unicode.GetString(packet,0,packet.Length);
MessageBox.Show(msg);
}
if(commSocket.Poll(400,SelectMode.SelectRead))
{
Receive();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
CloseSocket();
}
public void CloseSocket()
{
if ((commSocket!=null)&&(commSocket.Connected))
{
commSocket.Shutdown(SocketShutdown.Both);
commSocket.Close();
}
if(socket != null)
{
socket.Close();
}
}
我明不明白也要向你证明吗?
这句话你理解错误:我开始让服务器监听,然后不想让服务器监听了,所以才关闭。我代码里的错误,按着你说的改了,还是有错。你让我改的地方:1。加了个acceptCallback在accept回掉函数
2。关闭的时候,
if ((commSocket!=null)&&(commSocket.Connected))
{
commSocket.Shutdown(SocketShutdown.Both);
commSocket.Close();
}
if(socket != null)
{
socket.Close();
}
也是按你说的改的。
我是不是没改全,你说得好象就这些吧我没别的意思,看你气的,这个问题解决了,我送你50分赔礼怎样
handler.BeginAccept(new AsyncCallback(AcceptCallback),handler);
handler.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(ReceiveCallback),handler);//handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReadCallback),state);说这段不对是因为:handler是返回的用于通信的套接字,是同尝试连接的客户端建立的新套接字,用于读写数据。应该让listener.BeginAccept(。。);这样做是让最初的那个套接字继续接受传入的连接,这样,你才可以继续接受新的传入的连接。不知这段你有没有改正。除这些之外,没看出你现在的代码段有什么错误。
启动按钮里的:
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.0.18"), 9999);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ip);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(AcceptCallback),null);
//接收回调函数
public static void AcceptCallback(IAsyncResult ar)
{
try
{
byte[] bytes=new byte[1024];
handler=socket.EndAccept(ar);
socket.BeginAccept(new AsyncCallback(AcceptCallback),null);
handler.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(OnReceive),null);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}private static void OnReceive(IAsyncResult ar)
{
int r = handler.EndReceive(ar);
}
停止按钮里的:
try
{
if ((handler!=null)&&(handler.Connected))
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
if(socket != null)
{
socket.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
别生气了,对不起了,
就说:无法访问名为system.net.sockets.socket的已处置对象。
就是把第四个参数改了,也是一样
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;namespace WindowsApplication6
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private Socket socket;
private Socket commSocket;
public byte[] packet;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 56);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 32);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 9999);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind((EndPoint)ip);
socket.Listen(10);
socket.BeginAccept(new AsyncCallback(OnAccept),null);
}
private void Receive()
{
if(packet == null)
{
packet = new byte[4096];
}
commSocket.BeginReceive(packet,0,packet.Length,SocketFlags.None,new AsyncCallback(OnReceive),packet.Length);
}
private void OnAccept(IAsyncResult ar)
{
commSocket = socket.EndAccept(ar);
socket.BeginAccept(new AsyncCallback(OnAccept),null);
Receive();
}
private void OnReceive(IAsyncResult ar)
{
int r = commSocket.EndReceive(ar);
if((r==0) || (packet == null))
{
MessageBox.Show("接收数据时发生错误。。");
return;
}
else
{
string msg = System.Text.Encoding.Unicode.GetString(packet,0,packet.Length);
MessageBox.Show(msg);
}
if(commSocket.Poll(400,SelectMode.SelectRead))
{
Receive();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
CloseSocket();
}
public void CloseSocket()
{
if ((commSocket!=null)&&(commSocket.Connected))
{
commSocket.Shutdown(SocketShutdown.Both);
commSocket.Close();
}
if(socket != null)
{
socket.Close();
}
}
}
}
错误:未处理的“System.InvalidOperationException”类型的异常出现在 system.dll 中其他信息:AcceptCallback
我都快崩溃了,你那里没有这个信息吗?奇怪
错误:未处理的“System.InvalidOperationException”类型的异常出现在 system.dll 中其他信息:AcceptCallback
我都快崩溃了,你那里没有这个信息吗?奇怪
[email protected]
谢谢