?? form1.cs
字號:
?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Receiver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IrDADeviceInfo[] irDevices;
IrDAClient irClient = new IrDAClient();
string irServiceName = "IrDATest";
int buffersize = 256;
// 首先嘗試檢索紅外網(wǎng)絡(luò)
irDevices = irClient.DiscoverDevices(2);
// 如果沒有找到紅外設(shè)備則退出
if ((irDevices.Length == 0))
{
MessageBox.Show("No remote infrared devices found.");
return;
}
// 連接到第一個設(shè)備上
IrDAEndPoint irEndP =
new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
irClient.Connect(irEndP);
// 建立一個流用于寫文件
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;
}
// 獲得IrDAClient的數(shù)據(jù)流
Stream baseStream = irClient.GetStream();
// 建立讀取文件的緩沖區(qū).
byte[] buffer = new byte[buffersize];
Int64 numToRead;
Int64 numRead;
// 首先讀取出并計算出文件的長度來.
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));
}
numToRead = BitConverter.ToInt64(buffer, 0);
try
{
// 然后將流中其他的數(shù)據(jù)寫入文件流
while ((numToRead > 0))
{
numRead = baseStream.Read(buffer, 0, buffer.Length);
numToRead = (numToRead - numRead);
writeStream.Write(buffer, 0, Convert.ToInt32(numRead));
}
//寫完數(shù)據(jù)后關(guān)閉文件流
writeStream.Close();
MessageBox.Show("File received.");
}
catch (Exception exWriteOut)
{
MessageBox.Show(("Write out: " + exWriteOut.Message));
}
//關(guān)閉IrDAClient對象的數(shù)據(jù)流和連接
baseStream.Close();
irClient.Close();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -