Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与我的c#服务器的TCP Javascript客户端连接_Javascript_C#_Tcp - Fatal编程技术网

与我的c#服务器的TCP Javascript客户端连接

与我的c#服务器的TCP Javascript客户端连接,javascript,c#,tcp,Javascript,C#,Tcp,我试图与我的Javascript浏览器客户端和TCP C#服务器建立一个简单的连接,我能够将数据从Javascript发送到C#,但是我在服务器上得到奇怪的字符响应,而不是消息本身。我对javascript没有任何经验,我的TCP/Sockets知识几乎为零,但我正在努力学习 问题是: 当我发送数据时,我可以在c#服务器上获取数据,但作为一个奇怪的角色(滚动到末尾查看),我如何进行适当的握手?我已经在谷歌上搜索并尝试过了,但我仍然没有得到任何结果 这是JS <html> <

我试图与我的Javascript浏览器客户端和TCP C#服务器建立一个简单的连接,我能够将数据从Javascript发送到C#,但是我在服务器上得到奇怪的字符响应,而不是消息本身。我对javascript没有任何经验,我的TCP/Sockets知识几乎为零,但我正在努力学习

问题是: 当我发送数据时,我可以在c#服务器上获取数据,但作为一个奇怪的角色(滚动到末尾查看),我如何进行适当的握手?我已经在谷歌上搜索并尝试过了,但我仍然没有得到任何结果

这是JS

<html>
   <head>
      
      <script type = "text/javascript">
         function WebSocketTest() {
            
            if ("WebSocket" in window) {
               alert("WebSocket is supported by your Browser!");
               
               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:26950/echo");
                
               ws.onopen = function() {
                  
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };
                
               ws.onmessage = function (evt) { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };
                
               ws.onclose = function() { 
                  
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };
            } else {
              
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }
      </script>
        
   </head>
   
   <body>
      <div id = "sse">
         <a href = "javascript:WebSocketTest()">Run WebSocket</a>
      </div>
      
   </body>
</html>
浏览器连接只是连接的一种方式,我也使用Unity作为客户端和其他软件,一切都在那里工作,我只是觉得这个浏览器有问题。我们将非常感谢您提供的任何帮助和指导。多谢各位

  public static void RecieveData(object _obj)
     {
        TcpClient client = (TcpClient)_obj;
        NetworkStream stream = client.GetStream();
        StreamReader sr = new StreamReader(client.GetStream());
        StreamWriter sw = new StreamWriter(client.GetStream());
        string username = "";

        while (true)
        {

            if(client.Connected)
            {

                if (client.Client.Poll(0, SelectMode.SelectRead))
                {
                    byte[] buff = new byte[1];
                    if (client.Client.Receive(buff, SocketFlags.Peek) == 0)
                    {
                        connectedClients.Remove(username);
                        // Client disconnected
                        break;
                    }
                }

                try
                {
                   
                    byte[] buffer = new byte[1024];
                    stream.Read(buffer, 0, buffer.Length);
                    int recv = 0;
                    foreach (byte b in buffer)
                    {
                        if (b != 0)
                        {
                            recv++;
                        }
                    }
                    string request = Encoding.UTF8.GetString(buffer, 0, recv);
                    Console.WriteLine("request received" + request);

                    if (new Regex("^GET").IsMatch(request)) // Handshaking protocol
                    {
                        Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
                            + "Connection: Upgrade" + Environment.NewLine
                            + "Upgrade: websocket" + Environment.NewLine
                            + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
                                SHA1.Create().ComputeHash(
                                    Encoding.UTF8.GetBytes(
                                        new Regex("Sec-WebSocket-Key: (.*)").Match(request).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                                    )
                                )
                            ) + Environment.NewLine
                            + Environment.NewLine);

                        stream.Write(response, 0, response.Length);

                    }
                    else
                    {
                        string msg = DecodeMessage(buffer);
                        Console.WriteLine(msg);

                        stream.Flush();
                    }
          
                    

                }
                catch (Exception e)
                {
                    Console.WriteLine("Something went wrong."  + e.Message);

                    sw.WriteLine(e.ToString());
                }

            }

                  
        }
    }

 
    public static string DecodeMessage(byte[] bytes)
    {
        string incomingData = string.Empty;
        byte secondByte = bytes[1];
        int dataLength = secondByte & 127;
        int indexFirstMask = 2;
        if (dataLength == 126)
            indexFirstMask = 4;
        else if (dataLength == 127)
            indexFirstMask = 10;

        IEnumerable<byte> keys = bytes.Skip(indexFirstMask).Take(4);
        int indexFirstDataByte = indexFirstMask + 4;

        byte[] decoded = new byte[bytes.Length - indexFirstDataByte];
        for (int i = indexFirstDataByte, j = 0; i < bytes.Length; i++, j++)
        {
            decoded[j] = (byte)(bytes[i] ^ keys.ElementAt(j % 4));
        }

        return incomingData = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
    }
request received???L?J?)?9?+?j?#?9?"?
Message to sendJ?L?J?L?J?L?J?L?J?L?J?L?J?L?