如何将SocketIO与C#客户端连接
C#我正在为里程碑虚拟机开发一个MIP插件。 使用C#连接到SocketIO时出现问题。 我已尝试使用TcpClient、Socket、ClientWebSocket连接到SocketIO如何将SocketIO与C#客户端连接,c#,.net,socket.io,tcpclient,milestone,C#,.net,Socket.io,Tcpclient,Milestone,C#我正在为里程碑虚拟机开发一个MIP插件。 使用C#连接到SocketIO时出现问题。 我已尝试使用TcpClient、Socket、ClientWebSocket连接到SocketIO TcpClient tcpClient = new TcpClient(); tcpClient.Connect("127.0.0.1, 3001); 我也尝试过连接ClientWebSocket,但在服务器端同样没有反应 using (var client = new Cli
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("127.0.0.1, 3001);
我也尝试过连接ClientWebSocket,但在服务器端同样没有反应
using (var client = new ClientWebSocket())
{
// await client.ConnectAsync(new Uri("ws://192.168.100.25:8090/?token="),timeout.Token);
await client.ConnectAsync(new Uri(LOCAL_PATH), timeout.Token);
var buffer = new ArraySegment<byte>(new byte[1000]);
var result = await client.ReceiveAsync(buffer, timeout.Token);
}
使用(var client=new ClientWebSocket())
{
//wait client.ConnectAsync(新Uri(“ws://192.168.100.25:8090/?token=”),timeout.token);
wait client.ConnectAsync(新Uri(本地路径),timeout.Token);
var buffer=newarraysegment(新字节[1000]);
var result=await client.ReceiveAsync(缓冲区、超时、令牌);
}
谁能提供一些可以作为SocketIO客户机的库
URI有以下语法:这里是我使用的经过测试的代码,假设您的服务器正在运行,您需要传递IP或主机名和端口号,然后是要发送的负载或消息:
private bool ConnectAndSendMessage(String server, Int32 port, String message)
{
try
{
// Create a TcpClient.
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Buffer to store the response bytes receiver from the running server.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
// Close everything.
stream.Close();
client.Close(); return true;
}
catch (ArgumentNullException e)
{
_txtStyling.WriteCustomLine(string.Format("ArgumentNullException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
}
catch (SocketException e)
{
_txtStyling.WriteCustomLine(string.Format("SocketException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
}
}
下面是我使用的一个经过测试的代码,假设您的服务器正在运行,您需要传递IP或主机名和端口号,然后是要发送的负载或消息:
private bool ConnectAndSendMessage(String server, Int32 port, String message)
{
try
{
// Create a TcpClient.
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Buffer to store the response bytes receiver from the running server.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
// Close everything.
stream.Close();
client.Close(); return true;
}
catch (ArgumentNullException e)
{
_txtStyling.WriteCustomLine(string.Format("ArgumentNullException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
}
catch (SocketException e)
{
_txtStyling.WriteCustomLine(string.Format("SocketException: {0} \n\n", e.Message), 14, false, false, Brushes.Red); return false;
}
}
你的服务器正在运行吗?似乎您正在尝试连接到同一台机器127.0.0.1是的,我的服务器正在运行。是的,我正在尝试连接到一台正在我的计算机上运行的服务器。但我在另一台电脑上用服务器做了同样的尝试。结果是一样的。你的服务器在运行吗?似乎您正在尝试连接到同一台机器127.0.0.1是的,我的服务器正在运行。是的,我正在尝试连接到一台正在我的计算机上运行的服务器。但我在另一台PC上的服务器上尝试了同样的方法。结果是一样的。这是连接到SocketIO的吗?那个么我想通过Uri传递的查询呢?啊,好的,我知道你们在寻找什么,似乎你们有一个需要在你们的应用程序中使用的web API。。。为此,您可以使用RestSharp.dll,从nuget下载。在这里,您可以查看我尝试连接的一些基本示例,输入所有必要的库,导入,使用但我获得的运行时。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。异常类型:System.IO.FileNotFoundException异常消息:无法加载文件或程序集“RestSharp,版本=106.0.0.0,区域性=中性,PublicKeyToken=598062e77f915f75”或其依赖项之一。系统找不到指定的文件。restsharp的版本应与.net framwork的版本匹配,请检查.net版本是否为4.6,我无法理解哪个版本与此连接到SocketIO兼容?那个么我想通过Uri传递的查询呢?啊,好的,我知道你们在寻找什么,似乎你们有一个需要在你们的应用程序中使用的web API。。。为此,您可以使用RestSharp.dll,从nuget下载。在这里,您可以查看我尝试连接的一些基本示例,输入所有必要的库,导入,使用但我获得的运行时。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。异常类型:System.IO.FileNotFoundException异常消息:无法加载文件或程序集“RestSharp,版本=106.0.0.0,区域性=中性,PublicKeyToken=598062e77f915f75”或其依赖项之一。系统找不到指定的文件。restsharp的版本应与.net framwork的版本匹配,请检查.net版本是否为4.6,我无法理解哪个版本与它兼容