2012-12-12 101 views
1

我在Windows 8上學習並嘗試通過WiFi Direct在2臺PC之間創建一個傳輸文件的應用程序。 現在我可以讓應用程序在2臺PC之間發送文本。Windows 8上的文件傳輸WiFi Direct

SendText:

private async void SendText() 
{ 
    string msg = SendMessageTextBox.Text; 

    if (msg.Length > 0) 
    { 
     var msgLength = dataWriter.MeasureString(msg); 
     dataWriter.WriteInt32(msg.Length); 
     dataWriter.WriteString(msg); 
     try 
     { 
      await dataWriter.StoreAsync(); 
      Debug.WriteLine("Send Message >>: " + msg + "\n"); 
     } 
     catch (Exception exc) 
     { 
      Debug.WriteLine("Send error: " + exc.Message + "\n"); 
     } 
    } 
} 

接收文字:

private async void ReceiveText(StreamSocket socket, DataReader reader) 
{   
    uint initialLength = 5; 

    try 
    { 
     await reader.LoadAsync(initialLength); 
     uint msgLength = (uint)reader.ReadInt32(); 

     try 
     { 
      await reader.LoadAsync(msgLength); 
      string message = reader.ReadString(msgLength); 
      Debug.WriteLine(currentTime + " <<: " + message + "\n"); 

      ReceiveText(socket, reader); 
     } 
     catch (Exception exc) 
     { 
      Debug.WriteLine("Error: " + exc.Message + "\n"); 
      socket.Dispose(); 
     } 
    } 
    catch (Exception exc) 
    { 
     Debug.WriteLine("Error: " + exc.Message + "\n"); 
     socket.Dispose(); 
    } 
} 

但是我真的搞不清如何轉換和傳輸文件,我花很多時間在上面......

我知道有是DataReader和DataWriter類中的一些方法:

... 
public IBuffer ReadBuffer(uint length); 
public void ReadBytes(byte[] value); 
public static DataReader FromBuffer(IBuffer buffer); 
... 
public void WriteBytes(byte[] value); 
public void WriteBuffer(IBuffer buffer, uint start, uint count); 
... 

但如何將文件轉換爲流和使用什麼方法發送和接收?

我盡我所能,但總是失敗,真讓我畫畫......

是否有人給我一些指導?還是一個小樣本?

謝謝。

回答