2016-07-05 26 views
1

私は、WindowsモバイルとAndroid上でソケット・ストリームを介してデータ転送を確立するためにアプリケーションを作成しています。私はAndroidでコーディングを終了しました。私はアンドロイドで画像をバイト配列に変換し、ストリームを送信します。ソケット・ストリームを通してデータレイド・クラスを使用してイメージを読み取る

私はWindows 10 mobileのDataReaderクラスでそれを読む方法を知らない。良い例があれば教えてください

答えて

0

これは参考になる多くのコードサンプルを持つMSFT UWPサンプルのリンクです。 DataReaderのため https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DataReaderWriter

コードスニペットは、私が使用:それは通常の読み出しやストリームの書き込みよりも異なるので

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(); 

しかし、Androidの最後にチェックします。データリーダーは、メッセージの前にメッセージの長さが必要です。エンジニアもチェックしてください。ここでは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) ; 
      } 
関連する問題