gpt4 book ai didi

c# - C# 中的简单套接字服务器在尝试将数据写入客户端时意外关闭连接

转载 作者:行者123 更新时间:2023-11-30 21:58:59 25 4
gpt4 key购买 nike

我想弄清楚为什么这个 C# 代码:

int port = 8080;
var localAddr = IPAddress.Parse("127.0.0.1");

var server = new TcpListener(localAddr, port);
server.Start();

Console.WriteLine("Waiting for a connection... ");
var client = server.AcceptTcpClient();

Console.WriteLine("Connected!"); // Writes to console when "open" button
// is pushed in browser.

NetworkStream stream = client.GetStream();

while (client.Connected) // successfully loops twice.
{
string message = DateTime.Now.ToString();
byte[] msg = Encoding.ASCII.GetBytes(message);

stream.Write(msg, 0, msg.Length); // Exception is thrown here; data
// is never received in browser.

Console.WriteLine("Sent: {0}", message);
Thread.Sleep(1000);
}

client.Close();

抛出以下异常(在上面的代码注释中引用):

An unhandled exception of type 'System.IO.IOException' occurred in System.dll

Additional information: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.

我在浏览器上使用的代码如下所示:

<!DOCTYPE html>

<html>
<head>
<title>Socket Test</title>
</head>
<body>

<div>
<input id=message size=50 />
<button type="button" onclick="sendMessage();" >Send</button> <br />
<button type="button" onclick="openSocket();" >Open</button>
<button type="button" onclick="closeSocket();" >Close</button>
</div>

<div id="messages"></div>

<script type="text/javascript">

var webSocket;
var messages = document.getElementById("messages");

function openSocket(){

if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
writeResponse("WebSocket is already opened.");
return;
}

webSocket = new WebSocket("ws://127.0.0.1:8080");

webSocket.onopen = function(event){
writeResponse("Connection opened.");
};

webSocket.onmessage = function(event){
writeResponse(event.data);
};

webSocket.onclose = function(event){
writeResponse("Connection closed with event code " + event.code);
};

webSocket.onerror = function(event){
writeResponse(event.data);
};
}

function closeSocket(){
webSocket.close();
}

function sendMessage(){
webSocket.send(message.value);
}

function writeResponse(text){
messages.innerHTML += "<br/>" + text;
}

</script>
</body>
</html>

...我知道效果很好,因为我可以更改行

webSocket = new WebSocket("ws://127.0.0.1:8080");

webSocket = new WebSocket("ws://echo.websocket.org");

并获得良好的连接和来自 http://websocket.org 的回显回复.

注意:与 websocket.org 测试不同,浏览器从不报告与 C# 服务器的连接已打开,即使 C# 服务器认为它已成功建立连接。

最佳答案

您不能使用 TCP 套接字直接与 WebSocket 通信。 WebSockets 建立在 HTTP 之上,初始请求是一个 HTTP 请求,用于握手和从 HTTP 连接升级到 WebSocket 连接,因此您需要先实现一个小型 http 服务器来处理 HTTP 连接升级请求。

升级 header 如下所示

    GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

因此,您要么使用现有库,要么阅读 https://www.rfc-editor.org/rfc/rfc6455 的相关部分

关于c# - C# 中的简单套接字服务器在尝试将数据写入客户端时意外关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29689680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com