zoukankan      html  css  js  c++  java
  • Windows Mobile使用红外线传输文件

    在接收文件方面请仔细查看,IrDAClient().GetStream 返回Stream对象,他和TcpLister、TcpClient 的返回不一样,要特别注意。

    .NET Compact Framework 为设备之间的红外线通信提供类。此示例演示了如何使用红外线通信在设备之间发送和接收文件。您需要两台 Pocket PC,分别用于发送和接收文件。

    特别注意:在Widows Mobile 6 VS2005)中添加 System.net.IrDA.dll 的引用,并添加命名空间 using System.Net.Sockets

    此示例创建一个 IrDAClient 实例,并使用其 DiscoverDevices 方法来搜索范围内的红外线设备。此方法返回一个 IrDADeviceInfo 对象数组,用于提供每台设备的信息。

    此示例提供了文件发送和接收代码,这些代码可以通过“发送”和“接收”按钮进行演示。您至少应为一台设备创建“发送”应用程序,为另一台设备创建“接收”应用程序。

    “发送”按钮只会将文件发送到侦听文件发送请求的设备。因此,在发送设备上点击“发送”按钮之前,必须先在接收设备上点击“接收”按钮。

     

    发送端执行以下任务:

    u       获取要发送的文件流。

    u       使用为此应用程序确定的服务名创建一个 IrDAClient 实例。通过指定服务名来建立红外线连接,只要参与设备引用相同的名称,则服务名可以为任何值。在此示例中,服务名为“IrDATest”。

    u       将文件流读入发送该文件的 IrDAClient 流。

    u       “接收”按钮将创建一个 IrDAListener 实例,以侦听服务名与发送设备中的 IrDAClient 实例相同的设备。

    接收端执行以下任务:

    u       创建将传输内容写入“我的文档”文件夹中的接收文件的流。

    u       使用发送设备的设备 ID 和服务名创建 IrDAEndPoint 实例。

    u       基于 IrDAEndPoint 实例创建一个 IrDAListener 实例并启动侦听服务。

    u       使用 AcceptIrDAClient 方法基于 IrDAListener 实例创建一个 IrDAClient 实例。

    u       读取 IrDAClient 实例的基础流,其中包含传输文件的数据。

    u       将该数据流写入 Receive.txt 文件的流。

    创建发送应用程序

    1.    为发送设备创建 Pocket PC 应用程序,并向窗体添加按钮。将该按钮命名为“发送”。

    2.    在“我的文档”文件夹中创建名为 Send.txt 的文件。

    在“发送”按钮的 Click 事件中添加以下代码。

    // Align the infrared ports of the devices. Click the Receive button first, then click Send.

    private void SendButton_Click(object sender, System.EventArgs e)

    {

        IrDAClient irClient = new IrDAClient();

        string irServiceName = "IrDATest";

        IrDADeviceInfo[] irDevices;

        int buffersize = 256;

        // Create a collection of devices to discover.

        irDevices = irClient.DiscoverDevices(2);

       

    // Show the name of the first device found.

        if ((irDevices.Length == 0))

        {

            MessageBox.Show("No remote infrared devices found.");

            return;

        }

        try

        {

            IrDAEndPoint irEndP = new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);

            IrDAListener irListen = new IrDAListener(irEndP);

            irListen.Start();

            irClient = irListen.AcceptIrDAClient();

            MessageBox.Show("Connected!");

        }

        catch (SocketException exSoc)

    {

        MessageBox.Show(("Couldn"'t listen on service "+ (irServiceName + (": " + exSoc.ErrorCode))));

        }

            // Open a file to send and get its stream.

        Stream fs;

        try

        {

            fs = new FileStream(".""My Documents""send.txt", FileMode.Open);

        }

        catch (Exception exFile)

        {

            MessageBox.Show(("Cannot open " + exFile.ToString()));

            return;

        }

        

        // Get the underlying stream of the client.

        Stream baseStream = irClient.GetStream();

      // Get the size of the file to send and write its size to the stream

    //得到文件的大小并将其写入发送数据流 8个字节

    byte[] length = BitConverter.GetBytes(fs.Length);

        baseStream.Write(length, 0, length.Length);

        // Create a buffer for reading the file.

        byte[] buffer = new byte[buffersize];

        int fileLength = (int) fs.Length;

        try

        {

            // Read the file stream into the base stream.

            while ((fileLength > 0))

            {

                Int64 numRead = fs.Read(buffer, 0, buffer.Length);

                baseStream.Write(buffer, 0, Convert.ToInt32(numRead));

                fileLength = (fileLength - Convert.ToInt32(numRead));

            }

            MessageBox.Show("File sent");

        }

        catch (Exception exSend)

        {

            MessageBox.Show(exSend.Message);

        }

        fs.Close();

        baseStream.Close();

        irClient.Close();

    }

    创建接收应用程序

    4 为接收设备创建 Pocket PC 应用程序,并向窗体添加按钮。将该按钮命名为“接收”。

    5 在“接收”按钮的 Click 事件中添加以下代码。

    // Align the infrared ports of the devices. Click the Receive button first, then click Send.

    private void ReceiveButton_Click(object sender, System.EventArgs e)

    {

        IrDADeviceInfo[] irDevices;

        IrDAClient irClient = new IrDAClient();

        string irServiceName = "IrDATest";

        int buffersize = 256;

        // Create a collection for discovering up to three devices, although only one is needed.

        irDevices = irClient.DiscoverDevices(2);

        // Cancel if no devices are found.

        if ((irDevices.Length == 0))

        {

            MessageBox.Show("No remote infrared devices found.");

            return;

        }

        // Connect to the first IrDA device

        IrDAEndPoint irEndP = new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);

        irClient.Connect(irEndP);

        // Create a stream for writing a Pocket Word file.

        Stream writeStream;

        try

        {

            writeStream =new FileStream(".""My Documents""receive.txt", FileMode.OpenOrCreate);

        }

        catch (Exception Ex)

        {

            MessageBox.Show("Cannot open file for writing. " + Ex.Message);

            return;

        }

        // Get the underlying stream of the client.

        Stream baseStream = irClient.GetStream();

        //此处得到文件长度

    byte[] len = new byte[8];
     baseStream.Read(len, 0, 8);
     int64 fileSize = BitConverter.ToInt64(len, 0);

     

    // Create a buffer for reading the file.

       byte[] buffer = new byte[buffersize];

        Int64 numToRead;

        Int64 numRead;

        // Read the file into a stream 8 bytes at a time. Because the stream does not support seek operations, its length cannot be determined.

        numToRead = 8;

        try

        {

            while ((numToRead > 0))

            {

                numRead = baseStream.Read(buffer, 0, Convert.ToInt32(numToRead));

                numToRead = (numToRead - numRead);

            }

        }

    catch (Exception exReadIn)

    {

            MessageBox.Show(("Read in: " + exReadIn.Message));

        }

        // Get the size of the buffer to show the number of bytes to write to the file.

        numToRead = BitConverter.ToInt64(buffer, 0);

        try

        {

            // Write the stream to the file until there are no more bytes to read.

            while ((numToRead > 0)) {

                numRead = baseStream.Read(buffer, 0, buffer.Length);

                numToRead = (numToRead - numRead);

                writeStream.Write(buffer, 0, Convert.ToInt32(numRead));

            }

            writeStream.Close();

            MessageBox.Show("File received.");

        }

        catch (Exception exWriteOut)

        {

            MessageBox.Show(("Write out: " + exWriteOut.Message));

        }

        baseStream.Close();

        irClient.Close();

    }

    运行应用程序

    1.    将应用程序部署到设备并启动这些应用程序。

    2.    对齐设备的红外端口。

    3.    点击接收设备上的“接收”按钮。

    4.    点击发送设备上的“发送”按钮。

    5.    检查是否已在“我的文档”文件夹中创建 Receive.txt 文件。

    编译代码

    此示例需要引用下面的命名空间:

         System

         System.Net

         System.Net.Sockets

         System.IO

         System.Windows.Forms

  • 相关阅读:
    windows 下使用cmake指定visual studio 版本
    python This application failed to stat could not find or load the Qt platform plugin "windows"
    WEP无线网络密码破解
    使用PsExec tool在Session 0下运行程序
    关于远程桌面出现:“由于数据加密错误,这个会话将结束。请重新连接到远程计算机。”
    VB命令行参数分隔, 类似C语言中的main(int argc, char* argv[])
    VS2010/MFC编程入门之四十四:定时器Timer
    MFC之进度条CProgressCtrl
    VC版DoEvents
    文件夹选择对话框
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/1584037.html
Copyright © 2011-2022 走看看