由于其权限而无法访问套接字

发布于 2024-12-21 22:44:10 字数 1870 浏览 0 评论 0原文

我在 Windows 7 上使用 C# 和 Visual Studio 2010
只是尝试一个简单的服务器/客户端示例来学习套接字编程大约两周,但是昨天当我开始调试时,我得到了这个异常(尝试以访问权限禁止的方式访问套接字)。即使我已经关闭了防火墙和防病毒软件。再加上检查安全选项卡并设置完全信任。

我读过很多文章说我们不能在 Windows 7 上使用 TCP,但它工作正常!它仍然有效,我只需要重新启动计算机即可。但每次需要运行时重新启动并不是一个解决方案。

有什么想法吗?

更新:

static void Main(string [] args)
    {
        Program progDomain = new Program();
        progDomain.clients = new List<ClientManager>();

        if ( args.Length == 0 )
        {
            progDomain.serverPort = 8000;
            progDomain.serverIP = IPAddress.Any;
        }
        if ( args.Length == 1 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = 8000;
        }
        if ( args.Length == 2 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = int.Parse(args [1]);
        }

        progDomain.bwListener = new BackgroundWorker();
        progDomain.bwListener.WorkerSupportsCancellation = true;
        progDomain.bwListener.DoWork += new DoWorkEventHandler(progDomain.StartToListen);
        progDomain.bwListener.RunWorkerAsync();

        Console.WriteLine("*** Listening on port {0}{1}{2} started.Press ENTER to shutdown server. ***\n",progDomain.serverIP.ToString(),":",progDomain.serverPort.ToString());

        Console.ReadLine();

        progDomain.DisconnectServer();
    }

    private void StartToListen(object sender , DoWorkEventArgs e)
    {
        this.listenerSocket = new Socket(AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
        this.listenerSocket.Bind(new IPEndPoint(this.serverIP , this.serverPort));
        this.listenerSocket.Listen(200);
        while ( true )
            this.CreateNewClientManager(this.listenerSocket.Accept());
    }

这是引发异常的代码段。没什么,因为它从一开始就开火。

I'm using C# with visual studio 2010 on windows 7
just trying a simple server/ client example to learn about socket programming for like 2 weeks now but then yesterday when I started debugging I got this exception (An attempt was made to access a socket in a way forbidden by its access permission). even though I've turned off the firewall and anti virus. plus checked the security tab and set full trust .

I read many articles saying we can't use TCP on windows 7 but it was working alright ! and it still works , I just need to restart the computer . but restarting every time I need to run is not a solution .

any ideas??

update:

static void Main(string [] args)
    {
        Program progDomain = new Program();
        progDomain.clients = new List<ClientManager>();

        if ( args.Length == 0 )
        {
            progDomain.serverPort = 8000;
            progDomain.serverIP = IPAddress.Any;
        }
        if ( args.Length == 1 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = 8000;
        }
        if ( args.Length == 2 )
        {
            progDomain.serverIP = IPAddress.Parse(args [0]);
            progDomain.serverPort = int.Parse(args [1]);
        }

        progDomain.bwListener = new BackgroundWorker();
        progDomain.bwListener.WorkerSupportsCancellation = true;
        progDomain.bwListener.DoWork += new DoWorkEventHandler(progDomain.StartToListen);
        progDomain.bwListener.RunWorkerAsync();

        Console.WriteLine("*** Listening on port {0}{1}{2} started.Press ENTER to shutdown server. ***\n",progDomain.serverIP.ToString(),":",progDomain.serverPort.ToString());

        Console.ReadLine();

        progDomain.DisconnectServer();
    }

    private void StartToListen(object sender , DoWorkEventArgs e)
    {
        this.listenerSocket = new Socket(AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
        this.listenerSocket.Bind(new IPEndPoint(this.serverIP , this.serverPort));
        this.listenerSocket.Listen(200);
        while ( true )
            this.CreateNewClientManager(this.listenerSocket.Accept());
    }

here's the piece of code where the exception fires. nothing much since it fires right from the start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2024-12-28 22:44:10

就像评论中写的那样检查 TcpListener 类。该课程让您的生活变得更加轻松。

Like written in the comments check the TcpListener class. That class makes your life a littel easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文