Windows 8 WiFi Direct上的文件传输

时间:2012-12-12 09:29:26

标签: windows-8

我在Windows 8上学习并尝试通过WiFi Direct在2台PC之间创建一个用于传输文件的应用程序。 现在我可以让应用程序在2台PC之间发送文本。

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

但是如何将文件转换为流并使用什么方法发送和接收?

我尽我所能,但总是失败,真的让我画画......

有人给我一些指导吗?还是一小部分样品?

感谢。

1 个答案:

答案 0 :(得分:2)

这篇文章(包括你可以重用的lib)可以帮助你在2个Windows 8对等体之间使用Wifi-Direct传输文件: http://blogs.msdn.com/b/stephe/archive/2013/12/11/win8-1-transfering-a-file-between-2-peers-using-wifi-direct-and-proximity-api.aspx

希望有所帮助