我在 Windows 8 上学习并尝试通过 WiFi Direct 创建一个用于在 2 台 PC 之间传输文件的应用程序。现在我可以让应用在两台电脑之间发送文本。
发短讯:
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);
...
但是如何将文件转换为流以及使用什么方法来发送和接收呢?
我尽力了,但总是失败,真的让我画画……
有人给我一些指导吗?还是小样本?
谢谢。