【问题标题】:data format in connection betwen desktop app and UWP application桌面应用程序和 UWP 应用程序之间连接的数据格式
【发布时间】:2019-01-29 23:44:10
【问题描述】:

我正在桌面应用程序 (c#) 和 HoloLens (UWP) 之间创建客户端-服务器服务。 服务器在 HoloLens 上运行,而 PC 应用程序是客户端。

我在传输数据时遇到了一些问题,这是我的问题。

虽然 UWP 服务器使用 Windows.Netowrking 和 Windows.Networking.Sockets,但它似乎以字符串的形式接收和发送数据。 PC App 使用 System.Net 和 System.Net.Sockets,在发送字符串之前需要将字符串转换为字节格式。

这可能是个问题吗?我找不到太多有关 Windows.Networking 的文档,而且我不知道发送前的数据是否已转换为字节。

【问题讨论】:

  • 是什么让您认为数据是作为字符串传输的?套接字应该是纯二进制的。任何非二进制数据都需要序列化成二进制才能传输。

标签: c# .net networking uwp hololens


【解决方案1】:

不知道发送前的数据到底是不是转字节

当您使用网络 API 调用 Write() 或 Send() 时,您通常可以选择提供多种数据类型,无论是字符串、字节 [] 还是 int - 但这不会影响数据是物理传输的。

在内部,传输将以有效的方式“序列化”(作为二进制流),但您无需关心这些细节。

因为另一端将能够将该数据处理为 byte[]、int[] 或 string。

您可能想要研究的一个常见问题是endianness,或者检查您如何将字符串更改为字节[]。即你在字符串之间改变的问题,或者不同的字节[]等不正确的问题。您可以阅读System.Text.Encoding 以检查您是否在 byte[] 和 string 之间正确移动。

//Turn a string into a byte[], for your PC app to send
byte[] byteData = System.Text.Encoding.ASCII.GetBytes("Message");
//On the other end read that byte[] into ASCII characters
char[] charsData = System.Text.Encoding.ASCII.GetChars(byteData);
//Check you read something, and turn those chars into a string.
if (charsData != null && charsData.Length > 0)
    string stringData = new string(chars);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多