0

我正在开发一个 SIP 客户端。为此,我必须侦听端口 5060 以获取传入的 SIP 服务器消息。为此,我编写了一些代码。(另外我在程序中拥有管理员权限。)

    WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
    if (hasAdministrativeRight == true)
    {
        TcpListener server;
        Int32 port = 5060;
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        server = new TcpListener(localAddr, port);
        server.Start();
        Byte[] bytes = new Byte[256];
        String data = null;
        while (true)
        {
            Console.Write("Waiting for a connection... ");
            TcpClient client = server.AcceptTcpClient();
            Console.WriteLine("Connected!");
            data = null;
            NetworkStream stream = client.GetStream();
            int i;
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                Console.WriteLine("Received: {0}", data);
                data = data.ToUpper();

                byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                stream.Write(msg, 0, msg.Length);
                Console.WriteLine("Sent: {0}", data);
            }

            client.Close();
        }
    }

我得到 SocketException:“试图以访问权限禁止的方式访问套接字”(本机错误代码:10013)...

你对此有什么建议吗?

4

1 回答 1

1

您似乎正在运行两个应用程序,并且它们正试图访问同一个套接字。

微软对您的问题的评价:

WSAEACCES (10013)

  • 翻译:权限被拒绝
  • 说明:试图以访问权限禁止的方式访问套接字。比如sendto使用广播地址,但是setsockopt(SO_BROADCAST)没有设置广播权限,就会出现这个错误。

    WSAEACCES 错误的另一个可能原因是当调用绑定 (Wsapiref_6vzm.asp) 函数时(在 Microsoft Windows NT 4 .0 Service Pack 4 [SP4] 或更高版本中),另一个程序、服务或内核模式驱动程序被绑定到具有独占访问权限的相同地址。这种独占访问是 Windows NT 4.0 SP4 及更高版本的新功能,它是通过使用 SO_EXCLUSIVEADDRUSE 选项实现的。

于 2012-05-08T22:00:43.277 回答