如何在 Socket.Stream 中向客户端发送消息?
How to send message to client in Socket.Stream?
在 C# 中有一个应用程序,我需要附加一个具有最短响应时间的 Web 界面(显示几个计时器),所以我想通过套接字连接 mozilla has a great example just for me. It already shows the connection, handshake and reading a message from the client. Bonus, the JS side is shown. It just doesn't show how to compose a response to the client. The following page 显示消息的字节数是从,但我还是不能把它弄好。
每当您尝试向客户端发送内容时,连接都会立即终止,即使您发送的是相同的字节数组。
请帮我实现发送消息的功能,我自己不会((
我找到了方法。如果有人需要它,我会把我的代码留在这里。它与 mozila 相同,但我添加了 SendEcho 函数
C# 上的代码服务器
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace TestSocket
{
class Program
{
public static void Main()
{
string ip = "127.0.0.1";
int port = 80;
var server = new TcpListener(IPAddress.Parse(ip), port);
string swkaSha1Base64 = "";
byte[] response = new byte[1];
server.Start();
Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
// enter to an infinite cycle to be able to handle every change in stream
while (true)
{
while (!stream.DataAvailable) ;
while (client.Available < 3) ; // match against "get"
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, client.Available);
string s = Encoding.UTF8.GetString(bytes);
if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
{
Console.WriteLine("=====Handshaking from client=====\n{0}", s);
// 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
// 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
// 3. Compute SHA-1 and Base64 hash of the new value
// 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
// HTTP/1.1 defines the sequence CR LF as the end-of-line marker
response = Encoding.UTF8.GetBytes(
"HTTP/1.1 101 Switching Protocols\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
"Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");
string x = Encoding.UTF8.GetString(response);
Console.WriteLine(x);
stream.Write(response, 0, response.Length);
}
else
{
bool fin = (bytes[0] & 0b10000000) != 0,
mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
if (bytes[0] == 0x88)
{
Console.WriteLine("соединение закрыто");
}
int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
msglen = bytes[1] - 128, // & 0111 1111
offset = 2;
if (msglen == 126)
{
// was ToUInt16(bytes, offset) but the result is incorrect
msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
offset = 4;
}
else if (msglen == 127)
{
Console.WriteLine("TODO: msglen == 127, needs qword to store msglen");
// i don't really know the byte order, please edit this
// msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
// offset = 10;
}
if (msglen == 0)
Console.WriteLine("msglen == 0");
else if (mask)
{
byte[] decoded = new byte[msglen];
byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
offset += 4;
for (int i = 0; i < msglen; ++i)
decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);
string text = Encoding.UTF8.GetString(decoded);
SendEcho(stream, text); //my update
Console.WriteLine("{0}", text);
}
else
Console.WriteLine("mask bit not set");
Console.WriteLine();
}
}
}
private static void SendEcho(NetworkStream stream, string inputText)
{
byte[] sendBytes = Encoding.UTF8.GetBytes(inputText);
byte lengthHeader = 0;
byte[] lengthCount = new byte[] { };
if (sendBytes.Length <= 125)
lengthHeader = (byte)sendBytes.Length;
if (125 < sendBytes.Length && sendBytes.Length < 65535) //System.UInt16
{
lengthHeader = 126;
lengthCount = new byte[] {
(byte)(sendBytes.Length >> 8),
(byte)(sendBytes.Length)
};
}
if (sendBytes.Length > 65535)//max 2_147_483_647 but .Length -> System.Int32
{ lengthHeader = 127;
lengthCount = new byte[] {
(byte)(sendBytes.Length >> 56),
(byte)(sendBytes.Length >> 48),
(byte)(sendBytes.Length >> 40),
(byte)(sendBytes.Length >> 32),
(byte)(sendBytes.Length >> 24),
(byte)(sendBytes.Length >> 16),
(byte)(sendBytes.Length >> 8),
(byte)sendBytes.Length,
};
}
List<byte> responseArray = new List<byte>() { 0b10000001 };
responseArray.Add(lengthHeader);
responseArray.AddRange(lengthCount);
responseArray.AddRange(sendBytes);
stream.Write(responseArray.ToArray(), 0, responseArray.Count);
}
}
}
HTML 和浏览器上的JS代码(我用chrome 98)
<!doctype html>
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<h2>WebSocket Test</h2>
<textarea cols=60 rows=6></textarea>
<button>send</button>
<div id=output></div>
<script>
// http://www.websocket.org/echo.html
var button = document.querySelector("button"),
output = document.querySelector("#output"),
textarea = document.querySelector("textarea"),
// wsUri = "ws://echo.websocket.org/",
wsUri = "ws://127.0.0.1/",
websocket = new WebSocket(wsUri);
button.addEventListener("click", onClickButton);
websocket.onopen = function (e) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
};
websocket.onclose = function (e) {
writeToScreen("DISCONNECTED");
};
websocket.onmessage = function (e) {
writeToScreen("<span>RESPONSE: " + e.data + "</span>");
};
websocket.onerror = function (e) {
writeToScreen("<span class=error>ERROR:</span> " + e.data);
};
function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message) {
output.insertAdjacentHTML("afterbegin", "<p>" + message + "</p>");
}
function onClickButton() {
var text = textarea.value;
text && doSend(text);
textarea.value = "";
textarea.focus();
}
</script>
在 C# 中有一个应用程序,我需要附加一个具有最短响应时间的 Web 界面(显示几个计时器),所以我想通过套接字连接 mozilla has a great example just for me. It already shows the connection, handshake and reading a message from the client. Bonus, the JS side is shown. It just doesn't show how to compose a response to the client. The following page 显示消息的字节数是从,但我还是不能把它弄好。 每当您尝试向客户端发送内容时,连接都会立即终止,即使您发送的是相同的字节数组。 请帮我实现发送消息的功能,我自己不会((
我找到了方法。如果有人需要它,我会把我的代码留在这里。它与 mozila 相同,但我添加了 SendEcho 函数
C# 上的代码服务器
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace TestSocket
{
class Program
{
public static void Main()
{
string ip = "127.0.0.1";
int port = 80;
var server = new TcpListener(IPAddress.Parse(ip), port);
string swkaSha1Base64 = "";
byte[] response = new byte[1];
server.Start();
Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("A client connected.");
NetworkStream stream = client.GetStream();
// enter to an infinite cycle to be able to handle every change in stream
while (true)
{
while (!stream.DataAvailable) ;
while (client.Available < 3) ; // match against "get"
byte[] bytes = new byte[client.Available];
stream.Read(bytes, 0, client.Available);
string s = Encoding.UTF8.GetString(bytes);
if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase))
{
Console.WriteLine("=====Handshaking from client=====\n{0}", s);
// 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
// 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
// 3. Compute SHA-1 and Base64 hash of the new value
// 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
// HTTP/1.1 defines the sequence CR LF as the end-of-line marker
response = Encoding.UTF8.GetBytes(
"HTTP/1.1 101 Switching Protocols\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
"Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");
string x = Encoding.UTF8.GetString(response);
Console.WriteLine(x);
stream.Write(response, 0, response.Length);
}
else
{
bool fin = (bytes[0] & 0b10000000) != 0,
mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
if (bytes[0] == 0x88)
{
Console.WriteLine("соединение закрыто");
}
int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
msglen = bytes[1] - 128, // & 0111 1111
offset = 2;
if (msglen == 126)
{
// was ToUInt16(bytes, offset) but the result is incorrect
msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
offset = 4;
}
else if (msglen == 127)
{
Console.WriteLine("TODO: msglen == 127, needs qword to store msglen");
// i don't really know the byte order, please edit this
// msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
// offset = 10;
}
if (msglen == 0)
Console.WriteLine("msglen == 0");
else if (mask)
{
byte[] decoded = new byte[msglen];
byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
offset += 4;
for (int i = 0; i < msglen; ++i)
decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);
string text = Encoding.UTF8.GetString(decoded);
SendEcho(stream, text); //my update
Console.WriteLine("{0}", text);
}
else
Console.WriteLine("mask bit not set");
Console.WriteLine();
}
}
}
private static void SendEcho(NetworkStream stream, string inputText)
{
byte[] sendBytes = Encoding.UTF8.GetBytes(inputText);
byte lengthHeader = 0;
byte[] lengthCount = new byte[] { };
if (sendBytes.Length <= 125)
lengthHeader = (byte)sendBytes.Length;
if (125 < sendBytes.Length && sendBytes.Length < 65535) //System.UInt16
{
lengthHeader = 126;
lengthCount = new byte[] {
(byte)(sendBytes.Length >> 8),
(byte)(sendBytes.Length)
};
}
if (sendBytes.Length > 65535)//max 2_147_483_647 but .Length -> System.Int32
{ lengthHeader = 127;
lengthCount = new byte[] {
(byte)(sendBytes.Length >> 56),
(byte)(sendBytes.Length >> 48),
(byte)(sendBytes.Length >> 40),
(byte)(sendBytes.Length >> 32),
(byte)(sendBytes.Length >> 24),
(byte)(sendBytes.Length >> 16),
(byte)(sendBytes.Length >> 8),
(byte)sendBytes.Length,
};
}
List<byte> responseArray = new List<byte>() { 0b10000001 };
responseArray.Add(lengthHeader);
responseArray.AddRange(lengthCount);
responseArray.AddRange(sendBytes);
stream.Write(responseArray.ToArray(), 0, responseArray.Count);
}
}
}
HTML 和浏览器上的JS代码(我用chrome 98)
<!doctype html>
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<h2>WebSocket Test</h2>
<textarea cols=60 rows=6></textarea>
<button>send</button>
<div id=output></div>
<script>
// http://www.websocket.org/echo.html
var button = document.querySelector("button"),
output = document.querySelector("#output"),
textarea = document.querySelector("textarea"),
// wsUri = "ws://echo.websocket.org/",
wsUri = "ws://127.0.0.1/",
websocket = new WebSocket(wsUri);
button.addEventListener("click", onClickButton);
websocket.onopen = function (e) {
writeToScreen("CONNECTED");
doSend("WebSocket rocks");
};
websocket.onclose = function (e) {
writeToScreen("DISCONNECTED");
};
websocket.onmessage = function (e) {
writeToScreen("<span>RESPONSE: " + e.data + "</span>");
};
websocket.onerror = function (e) {
writeToScreen("<span class=error>ERROR:</span> " + e.data);
};
function doSend(message) {
writeToScreen("SENT: " + message);
websocket.send(message);
}
function writeToScreen(message) {
output.insertAdjacentHTML("afterbegin", "<p>" + message + "</p>");
}
function onClickButton() {
var text = textarea.value;
text && doSend(text);
textarea.value = "";
textarea.focus();
}
</script>