2016-07-05 53 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) ; 
      }