2012-11-03 56 views
3

编译时出现此错误。这是什么意思,我该如何解决它?'System.Net.Sockets.Socket.Dispose(布尔)'由于其保护级别而无法访问

“System.Net.Sockets.Socket.Dispose(布尔)”不可访问由于其保护级别

这里是我的两个文件;

Listener.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
namespace Multi_con_Server 
{ 
class Listener 
{ 
    Socket s; 

    public bool Listening 
    { 
     get; 
     private set; 
    } 
    public int Port 
    { 
     get; 
     private set; 
    } 

    public Listener(int port) 
    { 
     Port = port; 
     s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
    } 
    public void Start() 
    { 
     if (Listening) 
      return; 
     s.Bind(new IPEndPoint(0, Port)); 
     s.Listen(0); 

     s.BeginAccept(callback, null); 
     Listening = true; 
    } 
    public void Stop() 
    { 
     if (!Listening) 
      return; 

     s.Close(); 
     s.Dispose(); 
     s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
    } 

    void callback(IAsyncResult ar) 
    { 
     try 
     { 
      Socket s = this.s.EndAccept(ar); 

      if (SocketAccepted != null) 
      { 
       SocketAccepted(s); 
      } 

      this.s.BeginAccept(callback, null); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 

    } 
    public delegate void SocketAccecptedHandler(Socket e); 
    public event SocketAccecptedHandler SocketAccepted; 

} 
} 

的Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
namespace Multi_Con_C 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,  
    ProtocolType.Tcp); 
     s.Connect("127.0.0.1", 8); 
     s.Close(); 
     s.Dispose(); 
    } 
} 
} 

回答

4

MSDNSocket.Close()自动 “释放所有相关的资源。”您可以安全地从您的代码中删除所有发生的Socket.Dispose()

+0

谢谢。那工作。 :D – Herzatyl

+0

没问题! :) –

相关问题