Form1.cs
上传用户:linger1010
上传日期:2008-12-08
资源大小:561k
文件大小:3k
源码类别:

Windows Mobile

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.IO;
  11. namespace Sender
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             IrDAClient irClient = new IrDAClient();
  22.             //定制服务的名称
  23.             string irServiceName = "IrDATest";
  24.             IrDADeviceInfo[] irDevices;
  25.             int buffersize = 256;
  26.             // 首先查找红外网络里面的设备
  27.             irDevices = irClient.DiscoverDevices(2);
  28.             // 显示找到的第一个设备
  29.             if ((irDevices.Length == 0))
  30.             {
  31.                 MessageBox.Show("No remote infrared devices found.");
  32.                 return;
  33.             }
  34.             try
  35.             {
  36.                 IrDAEndPoint irEndP =
  37.                     new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
  38.                 IrDAListener irListen = new IrDAListener(irEndP);
  39.                 irListen.Start();
  40.                 irClient = irListen.AcceptIrDAClient();
  41.                 MessageBox.Show("Connected!");
  42.             }
  43.             catch (SocketException exSoc)
  44.             {
  45.                 MessageBox.Show(("Couldn't listen on service "
  46.                                 + (irServiceName + (": " + exSoc.ErrorCode))));
  47.             }
  48.             // 打开一个文件用于发送
  49.             Stream fs;
  50.             try
  51.             {
  52.                 //请首先在设备的My Documents文件夹下面放置一个send.txt文件用于发送
  53.                 fs = new FileStream(".\My Documents\send.txt", FileMode.Open);
  54.             }
  55.             catch (Exception exFile)
  56.             {
  57.                 MessageBox.Show(("Cannot open " + exFile.ToString()));
  58.                 return;
  59.             }
  60.             // 获得IrDAClient对象的数据流
  61.             Stream baseStream = irClient.GetStream();
  62.             // 首先获得这个文件的长度
  63.             byte[] length = BitConverter.GetBytes(fs.Length);
  64.             //把这个文件的长度写到流中去
  65.             baseStream.Write(length, 0, length.Length);
  66.             // 建立缓冲区用于读取文件
  67.             byte[] buffer = new byte[buffersize];
  68.             int fileLength = (int)fs.Length;
  69.             try
  70.             {
  71.                 // 将文件流读入IrDAClient的数据流
  72.                 while ((fileLength > 0))
  73.                 {
  74.                     Int64 numRead = fs.Read(buffer, 0, buffer.Length);
  75.                     baseStream.Write(buffer, 0, Convert.ToInt32(numRead));
  76.                     fileLength = (fileLength - Convert.ToInt32(numRead));
  77.                 }
  78.                 MessageBox.Show("File sent");
  79.             }
  80.             catch (Exception exSend)
  81.             {
  82.                 MessageBox.Show(exSend.Message);
  83.             }
  84.             //关闭文件流,以及IrDAClient的数据流。最后关闭IrDAClient对象。
  85.             fs.Close();
  86.             baseStream.Close();
  87.             irClient.Close();
  88.         }
  89.     }
  90. }