3

I got this error while compiling. what does it mean and how do i resolve it?

'System.Net.Sockets.Socket.Dispose(bool)' is inaccessible due to its protection level

Here are both of my files;

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

1 回答 1

4

From MSDN, Socket.Close() automatically "releases all associated resources". You can safely remove all occurances of Socket.Dispose() from your code.

于 2012-11-03T06:29:16.053 回答