1

我创建了一个使用 websocket 接受客户端的 Socket 服务器,我正在尝试提供聊天服务,但我遇到了两件事:

1.我能够将味精从浏览器发送到服务器,但反之则不行。

2.一旦我在控制台中写入并点击输入,连接已关闭,我收到断开连接的消息

这是我的代码:

服务器

 using System;
 using System.Net.Sockets;
 using System.Net;
 using System.Security.Cryptography;
 using System.Threading;
 using System.Text;
 using System.IO;

  namespace ConsoleApplication1
  {
class Program
{
    static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

    static void Main(string[] args)
    {
        serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080));
        serverSocket.Listen(128);
        serverSocket.BeginAccept(null, 0, OnAccept, null);
        Console.ReadLine();
    }

    private static void OnAccept(IAsyncResult result)
    {

        byte[] buffer = new byte[1024];
        try
        {
            Socket client = null;
            string headerResponse = "";
            if (serverSocket != null && serverSocket.IsBound)
            {

                client = serverSocket.EndAccept(result);
                int i = client.Receive(buffer);
                headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i);
                // write received data to the console
                Console.WriteLine(headerResponse);

            }
            if (client != null)
            {

                //Console.WriteLine("Connected and waiting for a client msg");
                /* Handshaking and managing ClientSocket */

                var key = headerResponse.Replace("ey:", "`")
                          .Split('`')[1]                     // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
                          .Replace("\r", "").Split('\n')[0]  // dGhlIHNhbXBsZSBub25jZQ==
                          .Trim();

                // key should now equal dGhlIHNhbXBsZSBub25jZQ==
                var test1 = AcceptKey(ref key);

                var newLine = "\r\n";

                var response = "HTTP/1.1 101 Switching Protocols" + newLine
                     + "Upgrade: websocket" + newLine
                     + "Connection: Upgrade" + newLine
                     + "Sec-WebSocket-Accept: " + test1 + newLine + newLine
                    //+ "Sec-WebSocket-Protocol: chat, superchat" + newLine
                    //+ "Sec-WebSocket-Version: 13" + newLine
                     ;


                client.Send(System.Text.Encoding.UTF8.GetBytes(response));


                int received = ReceiveMsg(client);

                // once the message is received decode it in different formats
                Console.WriteLine(Convert.ToString(buffer).Substring(0, received));

                Console.WriteLine("Type any word to send to the client");
                Console.ReadLine();
                int sent = client.Send(buffer, buffer.Length, SocketFlags.None);
                Console.WriteLine("Sent {0} bytes.", sent); //the sent buffer is not displaying on browser
                Console.ReadLine(); //here the connection is closed 
            }
        }
        catch (SocketException exception)
        {
            throw exception;
        }
        finally
        {
            if (serverSocket != null && serverSocket.IsBound)
            {
                serverSocket.BeginAccept(null, 0, OnAccept, null);
            }
        }
    }


    public static int ReceiveMsg(Socket server )
    {
        byte[] msg = Encoding.UTF8.GetBytes("This is a test");
        byte[] bytes = new byte[256];
        try
        {
            // Blocks until send returns. 
            int i = server.Send(msg);
            Console.WriteLine("Sent {0} bytes.", i);

            // Get reply from the server.
            i = server.Receive(bytes);
            ASCIIEncoding encoder = new ASCIIEncoding();
            Console.WriteLine(encoder.GetString(bytes, 0, bytes.Length));
        }
        catch (SocketException e)
        {
            Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);
            return (e.ErrorCode);
        }
        return 0;
    }

    public static T[] SubArray<T>(T[] data, int index, int length)
    {
        T[] result = new T[length];
        Array.Copy(data, index, result, 0, length);
        return result;
    }

    private static string AcceptKey(ref string key)
    {
        string longKey = key + guid;
        byte[] hashBytes = ComputeHash(longKey);
        return Convert.ToBase64String(hashBytes);
    }

    static SHA1 sha1 = SHA1CryptoServiceProvider.Create();
    private static byte[] ComputeHash(string str)
    {
        return sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(str));
    }
   }
 }

客户

<!DOCTYPE HTML>
 <html lang = "en">
 <head>
  <title>WebSocket Test</title>
   <style type = "text/css">
    h1 {
     text-align: center;
   }
     .error {
       color: red;
   } 
    .response {
      color: blue;
   }
   fieldset {
     width: 80%;
      margin: auto;
  text-align: center;
  -moz-box-shadow: 10px 10px 10px #000000;
   -webkit-box-shadow: 10px 10px 10px #000000;
 }
  #output {
font-family: monospace;
width: 80%;
margin-left: auto;
margin-right: auto;
margin-top: 1em;
background-color: #eeeeee;
padding: 1em;
border: 5px groove #cccccc;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
-moz-box-shadow: 10px 10px 10px #000000;
-webkit-box-shadow: 10px 10px 10px #000000;
   } 
  </style>
   <script language="javascript" type="text/javascript">
  var output;
  var websocket;
  function init() {
      output = document.getElementById("output");
  } // end init
  function connect() {
      //open socket
      if ("WebSocket" in window) {
          websocket = new WebSocket("ws://localhost:8080/server2");
          //note this server does nothing but echo what was passed
          //use a more elaborate server for more interesting behavior

          output.innerHTML = "connecting...";

          //attach event handlers
          websocket.onopen = onOpen;
          websocket.onclose = onClose;
          websocket.onmessage = onMessage;

          websocket.onerror = onError;
      } else {
          alert("WebSockets not supported on your browser.");
      } // end if
   } // end connect
   function onOpen(evt) {
      //called as soon as a connection is opened
      output.innerHTML = "<p>CONNECTED TO SERVER</p>";
  } // end onOpen
  function onClose(evt) {
      //called when connection is severed
      output.innerHTML += "<p>DISCONNECTED</p>"+evt.data;
  } // end onClose;
  function onMessage(evt) {
      //called on receipt of message
      alert("msg received");
      output.innerHTML += "<p class = 'response'>RESPONSE: "
  + evt.data + "</p>";
  } // end onMessage
  function onError(evt) {
      //called on error
      output.innerHTML += "<p class = 'error'>ERROR: "
  + evt.data + "</p>";
  } // end onError
  function sendMessage() {
      //get message from text field
      txtMessage = document.getElementById("txtMessage");
      message = txtMessage.value;
      //pass message to server
      websocket.send(message);
      output.innerHTML += "<p>MESSAGE SENT: " + message + "</p>";
  } // end sendMessage

   </script>
</head>
    <body onload = "init()">
   <h1>Web Socket Echo Chamber</h1>
   <form action = "">
    <fieldset>
  <button type = "button" onclick = "connect()">connect to server</button>
  <label for = "txtMessage">
    <input type = "text"  id = "txtMessage" value = "HTML5 Quick Reference For Dummies" />
  </label>
  <button type = "button" onclick = "sendMessage()">send message</button>
  <button type = "button" onclick = "websocket.close()">disconnect</button>
</fieldset>
   </form>
    <div id="output">Click 'connect' button to connect</div>
 </body>
   </html>

任何想法 ??

4

0 回答 0