我加上捕获异常之后,用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();
}
到底是哪里错了

解决方案 »

  1.   

    EndAccept之后 你还要继续 BeginAccept的。
      

  2.   

    加了还是那样,错误一样,走到ListenSoc.Shutdown(SocketShutdown.Receive);出错:
    无法访问名为system.net.sockets.socket的已处置对象。
      

  3.   

    同意  haiwangstar(南河三(来自于小犬星座)) 应该先绑定
      
     
      

  4.   

    不信你们打开以socket监听,然后再关上,肯定出错,我重新建了一个程序,一共就几行代码,根本关不上,你们试过了没有啊
      

  5.   

    呵呵。。不要ShutDown..直接Close。对你接收数据的套接字ShutSown。
      

  6.   

    不行
    出错:
    无法访问名为system.net.sockets.socket的已处置对象。:
      

  7.   

    你说的serverSocket应该是服务端开始监听的socket把,如果是的话,我已经试过了,不行
      

  8.   

    大家为什么不试试呢:
    创建一个项目,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);
    }
    先启动,在关闭,根本不行
      

  9.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    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();
    }
      

  10.   

    搞什么啊!根本没什么错的!
    你那个Close是什么???
      

  11.   

    我写错了,是socket.close(),
    你说没错,我说有错,是因为你的回掉函数是空,如果你换乘我的程序那样,写一个回掉函数,就会出错了
      

  12.   

    我感觉好像是我程序里建了一个socket,但是在回掉函数里又有一个socket,当执行到回掉函数socket时,我们却关掉主程序里的socket,才会出错,可我又说不上到底是怎么回事
      

  13.   

    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);这句是错的
     listener.BeginAccept();
    handler.BeginReceive(..);
      

  14.   

    不知你是怎么搞的。我又试了一下。。一切都正常。客户端private TcpClient client;
    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();
    }
    }
      

  15.   

    大哥,我是说:把服务器启动监听,我想不让服务器socket监听,就用shutdown和close(),现在只要服务端,刚启动,没客户端连接就立即关闭服务端socket,关不掉
      

  16.   

    为什么你的beginaccept(new,null)里是空呢
      

  17.   

    我从什么地方弄来一段程序?你贴出的代码中的错我早就指出了。你有没有看我的代码中是怎么处理的?“我想不让服务器socket监听,就用shutdown和close(),现在只要服务端,刚启动,没客户端连接就立即关闭服务端socket,关不掉”不让服务端监听?没有监听,你开服务端有什么用。”为什么你的beginaccept(new,null)里是空呢“第二个参数是传递一个对象给回调函数。我不想传什么参数,当然可以为空了。“如果你能指出我的程序哪里错了,并改过来,那才说明真正的明白了。”
    我明不明白也要向你证明吗?
      

  18.   

    生那么大气干嘛,不让服务端监听?没有监听,你开服务端有什么用。
    这句话你理解错误:我开始让服务器监听,然后不想让服务器监听了,所以才关闭。我代码里的错误,按着你说的改了,还是有错。你让我改的地方:1。加了个acceptCallback在accept回掉函数
    2。关闭的时候,
    if ((commSocket!=null)&&(commSocket.Connected))
    {
    commSocket.Shutdown(SocketShutdown.Both);
    commSocket.Close();
    }
    if(socket != null)
    {
    socket.Close();
    }
    也是按你说的改的。
    我是不是没改全,你说得好象就这些吧我没别的意思,看你气的,这个问题解决了,我送你50分赔礼怎样
      

  19.   

    呵。。一开始,我只写了一个服务端,同你所说的没客户端连接,只是建立一个套接字,然后绑定,监听的情况是一样的。然后我关闭套接字,没有发生什么异常。后来你说还有问题。我又写了一个客户端测试一下。客户端连接服务端,发送一句话,在服务端显示出来。然后关掉套接字,也都正常。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);说这段不对是因为:handler是返回的用于通信的套接字,是同尝试连接的客户端建立的新套接字,用于读写数据。应该让listener.BeginAccept(。。);这样做是让最初的那个套接字继续接受传入的连接,这样,你才可以继续接受新的传入的连接。不知这段你有没有改正。除这些之外,没看出你现在的代码段有什么错误。
      

  20.   

    看看这次的:
    启动按钮里的:
    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);
    }
    别生气了,对不起了,
      

  21.   

    呵。。没什么的handler.BeginReceive(bytes,0,bytes.Length,0,new AsyncCallback(OnReceive),null);这里第四个参数没有用枚举,编译通的过吗?不过,这个不是太重要。别的没有什么问题,还不行吗
      

  22.   

    编译通过了,错误还是一样,启动后就关闭:
    就说:无法访问名为system.net.sockets.socket的已处置对象。
    就是把第四个参数改了,也是一样
      

  23.   

    using System;
    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();
    }
    }
    }
    }
      

  24.   

    我可是按着你的程序一点都不差的写的:启动服务后,停止服务:
    错误:未处理的“System.InvalidOperationException”类型的异常出现在 system.dll 中其他信息:AcceptCallback
    我都快崩溃了,你那里没有这个信息吗?奇怪
      

  25.   

    我可是按着你的程序一点都不差的写的:启动服务后,停止服务:
    错误:未处理的“System.InvalidOperationException”类型的异常出现在 system.dll 中其他信息:AcceptCallback
    我都快崩溃了,你那里没有这个信息吗?奇怪
      

  26.   

    你能把你的文件发给我一份吗:
    [email protected]
    谢谢