1

C# I am developing a MIP plugin for Milestone VMS. I have a problem while connecting to SocketIO with C#. I have tried to connect to SocketIO with TcpClient, Socket, ClientWebSocket


    TcpClient tcpClient = new TcpClient();

    tcpClient.Connect("127.0.0.1, 3001);

I also have tried to connect with ClientWebSocket but again no reaction in server side.

    using (var client = new ClientWebSocket())
                {
                    // await client.ConnectAsync(new Uri("ws://192.168.100.25:8090/?token="),timeout.Token);
                    await client.ConnectAsync(new Uri(LOCAL_PATH), timeout.Token);
                    var buffer = new ArraySegment<byte>(new byte[1000]);

                    var result = await client.ReceiveAsync(buffer, timeout.Token);
                }

Can anyone provide some libraries that may serve as clients to SocketIO?

URI has this syntax: http://127.0.0.1:3001?token=xxx

  • is your server running ? seems you are trying to connect to the same machine 127.0.0.1 – Erwin Draconis Jan 14 '20 at 12:43
  • Yes, my server is running. And yes I am trying to connect to a server which is running in my computer. But the same thing I have tried with a server in another PC. The result is same. – Sofi Harutyunyan Jan 14 '20 at 13:19

1 Answers1

0

here is a tested code that i use , supposing your server is running , you need to pass the IP or HostName and port number , then come the payload or message that you want to send :

private bool ConnectAndSendMessage(String server, Int32 port, String message)
    {
        try
        {
            // Create a TcpClient.
            TcpClient client = new TcpClient(server, port);

            // Translate the passed message into ASCII and store it as a Byte array.
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // Get a client stream for reading and writing.
            NetworkStream stream = client.GetStream(); 

            // Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length);


            // Buffer to store the response bytes receiver from the running server.
            data = new Byte[256];

            // String to store the response ASCII representation.
            String responseData = String.Empty;

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = stream.Read(data, 0, data.Length);
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

            // Close everything.
            stream.Close();
            client.Close(); return true;
        }
        catch (ArgumentNullException e)
        {
            _txtStyling.WriteCustomLine(string.Format("ArgumentNullException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
        }
        catch (SocketException e)
        {
            _txtStyling.WriteCustomLine(string.Format("SocketException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
        }
    }
Erwin Draconis
  • 764
  • 8
  • 20
  • Is this connecting to SocketIO? And what about query I want to pass via Uri? – Sofi Harutyunyan Jan 14 '20 at 13:20
  • ah ok i see what you are looking for, seems that you have a web API that you need to consum in your app... for this you can use RestSharp.dll , download it from nuget here you can take a look at some basic examples http://restsharp.org/getting-started/#basic-usage – Erwin Draconis Jan 14 '20 at 13:39
  • I try to connect, intsall the necessary library, import, use but runtime I get ......................................................................................................................................... Exception type:System.IO.FileNotFoundException Exception message:Could not load file or assembly 'RestSharp, Version=106.0.0.0, Culture=neutral, PublicKeyToken=598062e77f915f75' or one of its dependencies. The system cannot find the file specified. – Sofi Harutyunyan Jan 15 '20 at 06:36
  • version of restsharp should match the version of you .net framwork,please check that – Erwin Draconis Jan 15 '20 at 08:11
  • .NET version is 4.6 and I cannot understand which is compatible version for it – Sofi Harutyunyan Jan 15 '20 at 14:03
  • @SofiHarutyunyan restsharp version 106.0.0.0 is only compatible with .net version 4.5.2 and below, in your case you should install a higher version of RestSharp or downgrade your .net framwork project to 4.5.2 , both are straightforward steps – Erwin Draconis Jan 16 '20 at 07:49