【问题标题】:C# Socket creation errorC# 套接字创建错误
【发布时间】:2018-06-26 00:24:10
【问题描述】:

所以我尝试使用 C# 套接字连接到服务器,但我遇到了这个错误:

System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Une tentative d’accès à un socket de manière interdite par ses autorisations d'accès a été tentée 192.168.1.17:9999

(!!抱歉不知道visual studio怎么用英文写)

这是我的代码

    Socket sock;

    public MainPage()
    {
        this.InitializeComponent();
        // Initialize MyRecognizer and Load Grammar

        connectToYanaForAll("192.168.1.17", 9999);
        sendInfoToYanaSocket(sock);
        //createGrammarFromYana(sock);
        disconnectFromYana(sock);
    }

    public void sleep(long millis) { Stopwatch stopwatch = Stopwatch.StartNew();  while (true)  { if (stopwatch.ElapsedMilliseconds >= millis) { break; } }  }

    public void connectToYanaForAll(String ip, int port)
    {
        IPAddress ipAddress = IPAddress.Parse(ip); //ipHostInfo.AddressList[0];
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
        // Create a TCP/IP  socket.  
        sock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); <-- Fail at this line
        sock.Connect(remoteEP);
    }


    public void disconnectFromYana(Socket sock)
    {
        sock.Shutdown(SocketShutdown.Both);
        sock = null;
    }

    public void sendInfoToYanaSocket(Socket sock)
    {
        String infos = "{\"action\":\"client_infos\",\"type\":\"listen\",\"version\":\"2\",\"location\":\"bureau\",\"token\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"}<EOF>";
        byte[] msg = Encoding.ASCII.GetBytes(infos);
        int bytesSent = sock.Send(msg);
        Debug.WriteLine(String.Join(bytesSent.ToString()," bytes sent!"));
    }

    public void createGrammarFromYana(Socket sock)
    {
        String infos = "{\"action\":\"GET_SPEECH_COMMANDS\"}<EOF>";
        byte[] msg = Encoding.ASCII.GetBytes(infos);
        int bytesSent = sock.Send(msg);
        Debug.WriteLine(String.Join(bytesSent.ToString(), " bytes sent!"));

        sleep(200);

        byte[] bytes = new byte[4096];
        int bytesRec = sock.Receive(bytes);
        Debug.WriteLine(String.Join("Received: ", bytesRec.ToString(), " bytes!"));
        Debug.WriteLine("Text = {0}",Encoding.ASCII.GetString(bytes, 0, bytesRec));
    }

我知道您需要管理员权限才能使用原始套接字,但我使用的是stream socket,所以我有点卡住了。 (顺便说一句,我正在使用 Visual Studio 2017) 我试过的:

  • 完全禁用防火墙:不起作用
  • 完全禁用防病​​毒:不起作用
  • 使用TcpClient client = new TcpClient(); 类:不工作

【问题讨论】:

  • 与您的问题无关,只是软件工程的问题:请使用System.Threading.Thread.Sleep(millis);而不是您的忙等待sleep(millis)方法!

标签: c# sockets visual-studio-2017 stream-socket-client


【解决方案1】:

根据谷歌翻译错误消息说: "尝试访问被其访问权限禁止的套接字的尝试 192.168.1.17:9999"

所以看起来仍然存在权限问题。也许您的系统上运行了防火墙,只允许一些已知的传出连接?

【讨论】:

    【解决方案2】:

    所以解决方案是将该行放入 Package.appxmanifest

    <Capability Name="privateNetworkClientServer" />
    

    &lt;Capabilities&gt;标签中

    【讨论】:

      【解决方案3】:

      尝试将您的套接字绑定到端点而不是连接,然后调用 Listen。希望对你有帮助

      【讨论】:

      • 你不明白我想连接到我的服务器而不是那里的服务器
      • 对不起,我的错。我没有正确阅读问题。尝试查看 stackoverflow.com/questions/10461257/… 。希望对您有所帮助。以前没见过这种异常。
      • 好的。希望你弄清楚。抱歉,我无法提供更多帮助
      • 首先正确阅读问题是发布好答案的绝对要求。
      • 可能是个愚蠢的问题,但您是以管理员身份运行 Visual Studio 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2011-07-31
      • 1970-01-01
      • 2020-01-11
      • 2011-08-01
      • 1970-01-01
      相关资源
      最近更新 更多