首页 > 解决方案 > data format in connection betwen desktop app and UWP application

问题描述

im creating a client-server service betwen desktop App (c#) and HoloLens (UWP). The server runs on HoloLens, while PC App is the client.

I meet some problems with transfering data and here is my question to you.

While UWP server uses a Windows.Netowrking and Windows.Networking.Sockets, it seems to receive and send data as strings. PC App uses System.Net and System.Net.Sockets and before sending the string it requires converting string into byte format.

Can this be an issue? I could not find to much documentation for Windows.Networking and i dont know if the data before sending is converted to byte anyway.

标签: c#.netnetworkinguwphololens

解决方案


我不知道发送前的数据是否无论如何都转换为字节

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

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

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

您可能想要研究的一个常见问题是字节序,或者检查您如何将字符串更改为字节 []。即你在字符串之间改变的问题,或者不同的字节[]等不正确的问题。您可以阅读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);

推荐阅读