2016-07-05 49 views
1

我在Windows mobile和Android上创建一个应用程序,通过套接字流在它们之间建立数据传输。我完成了Android的编码。我把图像转换成android中的字节数组并发送它。使用datareader类通过套接字流读取图像

我不知道如何在Windows 10移动版的DataReader类中读取它。如果有很好的例子是有的,请让我知道

回答

0

这是链接有许多代码样本MSFT UWP样本,这将是有益的。 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DataReaderWriter

为DataReader的代码段我使用:

var reader = new DataReader(socket.InputStream); 
while (true) 
{ 
uint readLength = await reader.LoadAsync(sizeof(uint)); 
if (readLength < sizeof(uint)) 
{ 
break; 
} 
uint currentLength = reader.ReadUInt32(); 
readLength = await reader.LoadAsync(currentLength); 
if (readLength < currentLength) 
{ 
break; 
} 
string message = reader.ReadString(currentLength); 
} 

reader.DetachStream(); 

然而,做检查在机器人端,因为它比正常读和流的写入不同。数据阅读器预计消息前的消息长度。一定要检查endiannes。这里是我在C#中使用的代码片段,可能有用。

try 
{ 
    int len = data.Length; 
    byte[] lenByte = BitConverter.GetBytes(data.Length); 
       if (BitConverter.IsLittleEndian) 
       { 
        Array.Reverse(lenByte); 
       }   
       foreach (byte b in lenByte) 
       { outStream.WriteByte(b); } 

       foreach (byte b in data) outStream.WriteByte(b); 
       Console.WriteLine("Message sent"); 
       } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message) ; 
      }