-1

可能重复:
监听端口 5060

我正在开发一个 SIP 客户端。我有一个问题。我想监听 5060 端口以捕获 SIP 服务器消息。为此,我编写了一些东西。(我也获得了程序的管理员权限)

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

我的代码:

 private void ListenPort() {
        WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
        TcpListener server = null;
        Int32 port = 5060;
        IPAddress localAddr = IPAddress.Parse("192.168.1.33");
        server = new TcpListener(localAddr, port);

        Byte[] bytes = new Byte[1000];
        String data = null;
    while (hasAdministrativeRight == true)
    {
        server.Start();
        int i = 0;
        while (1==1)
        {
            TcpClient client = server.AcceptTcpClient();
            NetworkStream stream = client.GetStream();
            data = null;
            i = stream.Read(bytes, 0, bytes.Length);
            data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
            label3.Text += data;
            this.Refresh();
            Thread.Sleep(500);

        }
    }
}

你觉得问题出在哪里?

4

3 回答 3

1

您需要在 while 循环之外和第一个 AcceptTcpClient 调用之前调用 server.Start() 。还尝试使用 IPAddress.Any 而不是 IPAddress.Parse("192.168.1.33") 作为您的侦听器 ip

于 2012-05-10T15:37:59.047 回答
1

Have you checked that no other program is already using port 5060? That's what this error can mean.

See this discussion on the matter: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/d23d471c-002a-4958-829e-eb221c8a4b76/

于 2012-05-10T15:31:40.860 回答
0

确保任何其他SIP 服务器程序未安装为使用相应端口的 Windows 服务。

输入netstat -an,它会显示“监听”端口或尝试谷歌搜索端口检查软件。

并检查您的 SIP 服务器配置是否通过 TCP 或 UDP 运行。

于 2012-08-10T12:20:47.977 回答