0

So im trying to connect to a server using C# socket but I ran into this error:

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

(!! Sorry don't know how to put visual studio in English)

Here's my code

    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));
    }

I know that you need admin right to use raw socket but I am using stream socket so I am a bit stuck. (Btw I'm using Visual studio 2017) What I've tried:

  • Disable firewall completly: not working
  • Disable anti-virus completly: not working
  • To use the TcpClient client = new TcpClient(); class : not working
AleXelton
  • 767
  • 5
  • 27
  • Not related with your problem, but just an issue of software engineering: Please use `System.Threading.Thread.Sleep(millis);` instead of your busy wait `sleep(millis)` method! – Roland Bär Jun 25 '18 at 15:03

3 Answers3

0

According to Google Translate the Error Message says: "An attempt to access a socket prohibited by its access permissions was attempted 192.168.1.17:9999"

So it looks like there is still an permission issue. Maybe you have a firewall running on your system that only allows some known outgoing connections?

Roland Bär
  • 1,720
  • 3
  • 22
  • 33
0

So the solution was to put that line into Package.appxmanifest

<Capability Name="privateNetworkClientServer" />

In the <Capabilities> tag

-1

Try to bind your socket to the endpoint instead of connect, and then call Listen. Hope it helps

C.M.P
  • 1
  • 1
  • You don't understand I want to connect to my server not be ther server – TurtleForGaming Apps Jun 24 '18 at 19:29
  • Sorry, my bad. I didn't read the question properly. Try looking at https://stackoverflow.com/questions/10461257/an-attempt-was-made-to-access-a-socket-in-a-way-forbbiden-by-its-access-permiss . Hope that helps you. Haven't seen that exception before. – C.M.P Jun 24 '18 at 19:36
  • Okay. Hope you figure it out. Sorry i couldn't be of more help – C.M.P Jun 24 '18 at 19:48
  • Reading the question properly first is an absolute requirement for posting a good answer. – Björn Zurmaar Jun 24 '18 at 19:48
  • Maybe a dumb quistion, but are you running visual studio as an administrator? – C.M.P Jun 24 '18 at 19:49