C# ClientWebSocket-SocketException:无法建立连接
我正在尝试使用ClientWebSocket连接到WebSocket服务器(演示或本地主机)。 我得到一个例外:C# ClientWebSocket-SocketException:无法建立连接,c#,.net,asp.net-core,websocket,C#,.net,Asp.net Core,Websocket,我正在尝试使用ClientWebSocket连接到WebSocket服务器(演示或本地主机)。 我得到一个例外: System.Net.WebSockets.WebSocketException HResult=0x80004005 Message=Unable to connect to the remote server Source=System.Net.WebSockets.Client StackTrace: Inner Exception 1: HttpReques
System.Net.WebSockets.WebSocketException
HResult=0x80004005
Message=Unable to connect to the remote server
Source=System.Net.WebSockets.Client
StackTrace:
Inner Exception 1:
HttpRequestException: No connection could be made because the target machine actively refused it
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it
我还有JavaScript websocket客户端,它成功地连接了。为什么我无法使用ClientWebSocket连接
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using StreamJsonRpc;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
Console.WriteLine("Press Ctrl+C to end.");
//await ConnectAsync("ws://localhost:63762/ws");
await ConnectAsync("ws://demos.kaazing.com/echo");
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
}
static async Task ConnectAsync(string url)
{
using (var socket = new ClientWebSocket())
{
Console.WriteLine("---> JSON-RPC Client connecting to " + url);
await socket.ConnectAsync(new Uri(url), CancellationToken.None);
Console.WriteLine("-> Connected to web socket. Establishing JSON-RPC protocol...");
}
}
}
}
2007年7月24日更新
我在nodejs中编写简单的WebSocket服务器来测试连接 本地主机。我的websocket web客户端已成功连接,但未连接控制台 应用程序每次都无法连接 如果是这样,那就没有意义了。它对我有效。
在.net
控制台应用程序中有ws
客户端的代码
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
Console.WriteLine("Press Ctrl+C to end.");
await ConnectAsync("ws://localhost:8080");
//await ConnectAsync("ws://demos.kaazing.com/echo");
Console.ReadLine();
}
catch (OperationCanceledException)
{
// This is the normal way we close.
}
}
static async Task ConnectAsync(string url)
{
using (var socket = new ClientWebSocket())
{
Console.WriteLine("---> JSON-RPC Client connecting to " + url);
await socket.ConnectAsync(new Uri(url), CancellationToken.None);
Console.WriteLine("-> Connected to web socket. Establishing JSON-RPC protocol...");
await Send(socket, "Hello from client.");
await Receive(socket);
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "close", CancellationToken.None);
}
}
static async Task Send(ClientWebSocket socket, string data) =>
await socket.SendAsync(Encoding.UTF8.GetBytes(data), WebSocketMessageType.Text, true, CancellationToken.None);
static async Task Receive(ClientWebSocket socket)
{
var buffer = new ArraySegment<byte>(new byte[2048]);
do
{
WebSocketReceiveResult result;
using (var ms = new MemoryStream())
{
do
{
result = await socket.ReceiveAsync(buffer, CancellationToken.None);
ms.Write(buffer.Array, buffer.Offset, result.Count);
} while (!result.EndOfMessage);
if (result.MessageType == WebSocketMessageType.Close)
break;
ms.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(ms, Encoding.UTF8))
Console.WriteLine(await reader.ReadToEndAsync());
}
} while (true);
}
_______________________________________________________________
ClientWebSocket
无法直接连接到Python中的socket.io
。
这是吉姆·斯托特的一个解决方案。
上还有一个项目,它是socket.io
的C#
客户端
示例客户端样式:
socket.On("news", (data) => {
Console.WriteLine(data);
});
从下面的问题中,还有许多方法可以满足您的需求。
这是一篇关于Wait ConnectAsync(“ws://demos.kaazing.com/echo”)的文章;url是连接失败的主要原因。将其他ws-server更改为测试。当我使用其他ws-server测试您的代码时,它会工作。我在nodejs中编写简单的WebSocket服务器来测试本地主机上的连接。我的websocket web客户端成功连接,但控制台应用程序每次都无法连接。我已经更新了代码,它在我身上运行,你能检查差异吗?更新到.Net Core 3.1解决了我的问题。但我不想安装socket.io。我想使用System.Net.WebSockets连接到websocket服务器。对不起,我认为您以前是使用socket.io构建ws-server的。确定。我认为代码应该可以工作。谢谢你的努力。我将继续寻找解决方案。如果它与代码无关,您可以检查防火墙或操作系统版本(Win 8稍后)。希望你得到正确的答案。:)
socket.On("news", (data) => {
Console.WriteLine(data);
});