IrDAClientとは? わかりやすく解説

IrDAClient クラス

赤外線通信の接続サービスを提供します。

名前空間: System.Net.Sockets
アセンブリ: System.Net.IrDA (system.net.irda.dll 内)
構文構文

public class IrDAClient
public ref class IrDAClient
public class IrDAClient
public class IrDAClient
解説解説
使用例使用例

このコード例は、赤外線通信を使用して、デバイス間でファイルを送受信する方法を示しています。ここでは、2 台の Pocket PC (ファイルの送信側と受信側) が必要です。このサンプルを実行するには、次の手順に従います。

  1. この例をビルドします。

  2. ファイル送信側デバイスの [マイ ドキュメント] フォルダに、send.txt という名前のテキスト ファイルを作成します。

  3. デバイスの赤外線ポートの位置を調整します。

  4. 両デバイスで IrDADemo.exe アプリケーションを起動します。

  5. 両デバイスで [Discover] をクリックし、範囲内にある赤外線デバイスの一覧を表示します。

  6. ファイル受信側デバイスで、送信側デバイスを選択し、[Receive] をクリックします。このボタンは、送信側デバイスで [Send] をクリックする前にクリックする必要があります。

  7. ファイル送信側デバイスで、[Send] をクリックします。

  8. 受信側デバイスがファイルを受信します。受信したファイルは、[マイ ドキュメント] フォルダの receive.txt という名前のファイルとして保存されます。

Imports System
Imports System.Windows.Forms
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private ListBox1 As System.Windows.Forms.ListBox
    Private StatusBar1 As System.Windows.Forms.StatusBar
    Private WithEvents Send As
 System.Windows.Forms.Button
    Private WithEvents Receive As
 System.Windows.Forms.Button
    Private WithEvents Discover As
 System.Windows.Forms.Button

    Dim irListen As IrDAListener
    Dim irClient As IrDAClient
    Dim irEndP As IrDAEndPoint
    Dim irDevices() As IrDADeviceInfo

    Private fileSend As String
    Private fileReceive As String
    Private irServiceName As String

    Private buffersize As Integer

    Public Sub New()
        InitializeComponent()
    End Sub 

    Private Sub Form1_Load(ByVal
 sender As System.Object, _
        ByVal e As System.EventArgs) Handles
 MyBase.Load

        ' Display Pocket PC OK button for closing.
        Me.MinimizeBox = False
    End Sub

    Protected Overrides Sub
 Dispose(disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub 

    Private Sub InitializeComponent()
        Me.Send = New System.Windows.Forms.Button
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Receive = New System.Windows.Forms.Button
        Me.StatusBar1 = New System.Windows.Forms.StatusBar
        Me.Discover = New System.Windows.Forms.Button
        '
        ' Send
        '
        Me.Send.Location = New System.Drawing.Point(16,
 120)
        Me.Send.Size = New System.Drawing.Size(80,
 20)
        Me.Send.Text = "Send File"
        '
        ' ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(16,
 48)
        Me.ListBox1.Size = New System.Drawing.Size(200,
 58)
        '
        ' Receive
        '
        Me.Receive.Location = New System.Drawing.Point(136,
 120)
        Me.Receive.Size = New System.Drawing.Size(80,
 20)
        Me.Receive.Text = "Receive File"
        '
        ' StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0,
 248)
        Me.StatusBar1.Size = New System.Drawing.Size(240,
 22)
        '
        ' Discover
        '
        Me.Discover.Location = New System.Drawing.Point(16,
 16)
        Me.Discover.Size = New System.Drawing.Size(112,
 20)
        Me.Discover.Text = "Discover Devies"
        '
        ' Form1
        '
        Me.Controls.Add(Discover)
        Me.Controls.Add(StatusBar1)
        Me.Controls.Add(Receive)
        Me.Controls.Add(ListBox1)
        Me.Controls.Add(Send)
        Me.Text = "IrDA Demo"
    End Sub 

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    ' Discover and list the available infrared devices.
    ' Infrared ports must be aligned.
    Private Sub Discover_Click(sender As
 Object, e As System.EventArgs) _
        Handles Discover.Click

        ' Create a collection of a maximum of three devices.
        irDevices = irClient.DiscoverDevices(2)

        ' Show a message if no devices are found.
        If irDevices.Length = 0 Then
            MsgBox("No remote infrared devices found!")
            Return
        End If

        ' Enumerate the IrDADeviceInfo
        ' array and list device information
        ' for each device in the list box.
        Dim device As String
        Dim ID As Integer
        ListBox1.Items.Clear()
        For Each irDevice As
 IrDADeviceInfo In  irDevices
            ID = BitConverter.ToInt32(irDevice.DeviceID, 0)
            device = ID.ToString() _
                & " " & irDevice.DeviceName
 _
                & " " & irDevice.CharacterSet
 _
                & " " & irDevice.Hints
            ListBox1.Items.Add(device)
        Next irDevice

        ListBox1.SelectedIndex = 0
        If irDevices.Length > 0 Then
            StatusBar1.Text = irDevices.Length.ToString() & "
 remote device(s)"
        End If 

        ' Enable the Send and Receive buttons.
        Send.Enabled = True
        Receive.Enabled = True
    End Sub 

    Private Sub IrDAConfig()

        irClient = New IrDAClient()

        ' Files for sending and receiving.
        fileSend = ".\My Documents\send.txt"
        fileReceive = ".\My Documents\receive.txt"

        ' Specify a name for the IrDA Service.
        ' It can be any name, provided all 
        ' participating devices use the same name.
        irServiceName = "IrDAFtp"

        ' Set maximum buffer size for file transfers.
        buffersize = 256

        ' Disable Send and Receive buttons until devices are discovered.
        Send.Enabled = False
        Receive.Enabled = False

    End Sub

    ' Sends a file.
    Private Sub Send_Click(sender As
 Object, e As System.EventArgs) _
        Handles Send.Click

        ' Open the file to send and get its stream.
        Dim fs As Stream
        Try
            fs = New FileStream(fileSend, FileMode.Open)
        Catch exFile As Exception
            MsgBox("Cannot open " & exFile.ToString())
            Return
        End Try

        ' Create an IrDA client with a service name that must
        ' match the service name of the receiving IrDA client.
        Try
            irClient = New IrDAClient(irServiceName)
        Catch exS As SocketException
            MsgBox("Create socket error: " & exS.Message
 & _
                " - Did you click Receive on the other device?")
            Return
        End Try

        ' Get the underlying stream of the client.
        Dim baseStream As Stream = irClient.GetStream()

        ' Get the size of the file to send
        ' and write its size to the stream.
        Dim length As Byte()
 = BitConverter.GetBytes(Fix(fs.Length))
        baseStream.Write(length, 0, length.Length)

        ' Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte

        ' Display the number of bytes being sent.
        Dim fileLength As Integer
 = CInt(fs.Length)
        StatusBar1.Text = "Sending " & fileLength
 & " bytes"

        ' Read the file stream into the base stream.
        While fileLength > 0
            Dim numRead As Integer
 = fs.Read(buffer, 0, buffer.Length)
            baseStream.Write(buffer, 0, numRead)
            fileLength -= numRead
        End While

        fs.Close()
        baseStream.Close()
        irClient.Close()
        StatusBar1.Text = "File sent"
    End Sub 

    ' Receives a file.
    Private Sub Receive_Click(sender As
 Object, e As System.EventArgs) _
        Handles Receive.Click

        ' Create a stream for writing the file.
        Dim writeStream As Stream
        Try
            writeStream = New FileStream(fileReceive, FileMode.OpenOrCreate)
        Catch 
            MsgBox("Couldn't open " & fileReceive &
 " for writing")
            Return
        End Try

        ' Create a connection, with the IrDAEndPoint class,
        ' for the selected device in the list box.
        ' Start listening for incoming requests from
        ' that device with an IrDAListener object.
        Try
            Dim i As Integer
 = ListBox1.SelectedIndex
            irEndP = New IrDAEndPoint(irDevices(i).DeviceID, irServiceName)
            irListen = New IrDAListener(irEndP)
            irListen.Start()
        Catch exSoc As SocketException
            MsgBox("Couldn't listen on service " & irServiceName
 & ": " _
                & exSoc.ErrorCode)
        End Try

        ' Show listening for selected device.
        StatusBar1.Text = "Listening for " & ListBox1.SelectedItem.ToString()

        ' Create a client connection for the
        ' service detected by the listener.
        Dim irClient As IrDAClient
        Try
            irClient = irListen.AcceptIrDAClient()
        Catch exp As SocketException
            MsgBox("Couldn't accept socket " & exp.ErrorCode)
            Return
        End Try

        ' Show whether a transfer from
        ' the remote device is pending.
        If irListen.Pending() = True Then
            StatusBar1.Text = "Pending from " &
 irClient.RemoteMachineName
        Else
            StatusBar1.Text = "Not pending from "
 & irClient.RemoteMachineName
        End If

        ' Get the underlying stream of the client.
        Dim baseStream As Stream = irClient.GetStream()

        Dim numToRead As Integer

        'Create a buffer for reading the file.
        Dim buffer(buffersize) As Byte

        ' Read the stream of data, which contains
        ' the data from the remote device, until
        ' there are no more bytes to read.
        numToRead = 4
        While numToRead > 0
            Dim numRead As Integer
 = baseStream.Read(buffer, 0, numToRead)
            numToRead -= numRead
        End While

        ' Get the size of the buffer to show
        ' the number of bytes to write to the file.
        numToRead = BitConverter.ToInt32(buffer, 0)
        StatusBar1.Text = "Going to write " &
 numToRead & " bytes"

        ' Write the stream to the file until
        ' there are no more bytes to read.
        While numToRead > 0
            Dim numRead As Integer
 = baseStream.Read(buffer, 0, buffer.Length)
            numToRead -= numRead
            writeStream.Write(buffer, 0, numRead)
        End While

        ' Show that the file was received.
        StatusBar1.Text = "File received"

        baseStream.Close()
        writeStream.Close()
        irListen.Stop()
        irClient.Close()
    End Sub 
End Class
using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace IrDADemo
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.StatusBar statusBar1;
        private System.Windows.Forms.Button Send;
        private System.Windows.Forms.Button Receive;
        private System.Windows.Forms.Button Discover;

        private IrDAListener irListen;
        private IrDAClient irClient;
        private IrDAEndPoint irEndP;
        private IrDADeviceInfo[] irDevices;

        string fileSend;
        string fileReceive;
        string irServiceName;

        int buffersize;

        public Form1()
        {
            InitializeComponent();

            irClient = new IrDAClient();

            // Files for sending and receiving.
            fileSend = ".\\My Documents\\send.txt";
            fileReceive = ".\\My Documents\\receive.txt";

            // Specify a name for the IrDA Service.
            // It can be any name, provided all
            // participating devices use the same name.
            irServiceName = "IrDAFtp";

            // Set maximum buffer size for file transfers.
            buffersize = 256;

            // Display Pocket PC OK button for closing.
            this.MinimizeBox = false;

            // Disable Send and Receive buttons until devices are discovered.
            Send.Enabled = false;
            Receive.Enabled = false;
        }

        protected override void Dispose(bool
 disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            this.Send = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.Receive = new System.Windows.Forms.Button();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.Discover = new System.Windows.Forms.Button();
            //
            // Send Button
            //
            this.Send.Location = new System.Drawing.Point(16,
 120);
            this.Send.Size = new System.Drawing.Size(80,
 20);
            this.Send.Text = "Send File";
            this.Send.Click += new System.EventHandler(this.Send_Click);
            //
            // listBox1
            //
            this.listBox1.Location = new System.Drawing.Point(16,
 48);
            this.listBox1.Size = new System.Drawing.Size(200,
 58);
            //
            // Receive Button
            //
            this.Receive.Location = new System.Drawing.Point(136,
 120);
            this.Receive.Size = new System.Drawing.Size(80,
 20);
            this.Receive.Text = "Receive File";
            this.Receive.Click += new System.EventHandler(this.Receive_Click);
            //
            // statusBar1
            //
            this.statusBar1.Location = new
 System.Drawing.Point(0, 248);
            this.statusBar1.Size = new System.Drawing.Size(240,
 22);
            //
            // Discover Button
            //
            this.Discover.Location = new System.Drawing.Point(16,
 16);
            this.Discover.Size = new System.Drawing.Size(112,
 20);
            this.Discover.Text = "Discover Devies";
            this.Discover.Click += new System.EventHandler(this.Discover_Click);
            //
            // Form1
            //
            this.Controls.Add(this.Discover);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.Receive);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.Send);
            this.Text = "IrDA Demo";
        }
        #endregion

        static void Main()
        {
            Application.Run(new Form1());
        }

        // Discover and list the available infrared devices.
        // Infrared ports must be aligned.
        private void Discover_Click(object
 sender, System.EventArgs e)
        {
            // Create a collection of a maximum of three devices.
            irDevices = irClient.DiscoverDevices(2);

            // Show a message if no devices are found.
            if (irDevices.Length == 0)
            {
                MessageBox.Show("No remote infrared devices found!");
                return;
            }

            // Enumerate the IrDADeviceInfo
            // array and list device information
            // for each device in the list box.
            string device;
            int ID;
            listBox1.Items.Clear();
            foreach(IrDADeviceInfo irDevice in
 irDevices)
            {
                ID = BitConverter.ToInt32(irDevice.DeviceID, 0);
                device = ID.ToString() + " " + irDevice.DeviceName + "
 "
                    +  irDevice.CharacterSet + " " + irDevice.Hints;
                listBox1.Items.Add(device);
            }

            listBox1.SelectedIndex = 0;
            if(irDevices.Length > 0)
                statusBar1.Text = 
                    irDevices.Length.ToString() + " remote device(s)";


            // Enable the Send and Receive buttons.
            Send.Enabled = true;
            Receive.Enabled = true;

        }


        // Sends a file.
        private void Send_Click(object sender,
 System.EventArgs e)
        {
            // Open the file to send and get its stream.
            Stream fileStream;
            try
            {
                fileStream = new FileStream(fileSend, FileMode.Open);
            }
            catch(Exception exFile)
            {
                MessageBox.Show("Cannot open " + exFile.ToString());
                return;
            }

            // Create an IrDA client with a service name that must
            // match the service name of the receiving IrDA client.
            try
            {
                irClient = new IrDAClient(irServiceName);
            }
            catch(SocketException exS)
            {
                MessageBox.Show("Create socket error: " + exS.Message +
 
                    " - Did you click Receive on the other device?");
                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.
            byte[] length = BitConverter.GetBytes((int)fileStream.Length);
            baseStream.Write(length, 0, length.Length);

            // Create a buffer for reading the file.
            byte[] buffer = new byte[buffersize];

            // Display the number of bytes being sent.
            int fileLength = (int)fileStream.Length;
            statusBar1.Text = "Sending " + fileLength + " bytes";

            // Read the file stream into the base stream.
            while(fileLength > 0)
            {
                int numRead = fileStream.Read(buffer, 0, buffer.Length);
                baseStream.Write(buffer, 0, numRead);
                fileLength -= numRead;
            }

            fileStream.Close();
            baseStream.Close();
            irClient.Close();
            statusBar1.Text = "File sent";


        }

        // Receives a file.
        private void Receive_Click(object sender,
 System.EventArgs e)
        {
            // Create a stream for writing the file.
            Stream writeStream;
            try
            {
                writeStream = new FileStream(fileReceive,
                    FileMode.OpenOrCreate);
            }
            catch(Exception)
            {
                MessageBox.Show("Couldn't open "
                    + fileReceive + " for writing");
                return;
            }

            // Create a connection, with the IrDAEndPoint class,
            // for the selected device in the list box.
            // Start listening for incoming requests from
            // that device with an IrDAListener object.
            try
            {
                int i = listBox1.SelectedIndex;
                irEndP = new IrDAEndPoint(irDevices[i].DeviceID
,
                    irServiceName);
                irListen = new IrDAListener(irEndP);
                irListen.Start();
            }
            catch(SocketException exSoc)
            {
                MessageBox.Show("Couldn't listen on service "
                    + irServiceName + ": "
                    + exSoc.ErrorCode);
            }

            // Show listening for selected device.
            statusBar1.Text = "Listening for "
                + listBox1.SelectedItem.ToString();

            // Create a client connection for the
            // service detected by the listener.
            IrDAClient irClient;
            try
            {
                irClient = irListen.AcceptIrDAClient();
            }
            catch(SocketException exp)
            {
                MessageBox.Show("Couldn't accept socket "
                    + exp.ErrorCode);
                return;
            }

            // Show whether a transfer from
            // the remote device is pending.
            if (irListen.Pending() == true)
                statusBar1.Text = "Pending from "
                    + irClient.RemoteMachineName;
            else
                statusBar1.Text = "Not pending from "
                    + irClient.RemoteMachineName;

            // Get the underlying stream of the client.
            Stream baseStream = irClient.GetStream();

            int numToRead;

            // Create a buffer for reading the file.
            byte[] buffer = new byte[buffersize];

            // Read the stream of data, which contains
            // the data from the remote device, until
            // there are no more bytes to read.
            numToRead = 4;
            while(numToRead > 0)
            {
                int numRead = baseStream.Read(buffer, 0, numToRead);
                numToRead -= numRead;
            }

            // Get the size of the buffer to show
            // the number of bytes to write to the file.
            numToRead = BitConverter.ToInt32(buffer, 0);
            statusBar1.Text = "Going to write "
                + numToRead + " bytes";

            // Write the stream to the file until
            // there are no more bytes to read.
            while(numToRead > 0)
            {
                int numRead = baseStream.Read(buffer, 0 , buffer.Length);
                numToRead -= numRead;
                writeStream.Write(buffer, 0, numRead);
            }

            // Show that the file was received.
            statusBar1.Text = "File received";

            baseStream.Close();
            writeStream.Close();
            irListen.Stop();
            irClient.Close();
        }
    }
}
継承階層継承階層
System.Object
   System.Net.EndPoint
    System.Net.Sockets.IrDAClient
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IrDAClient コンストラクタ ()


IrDAClient コンストラクタ


IrDAClient コンストラクタ (String)

指定したサービスに接続するための IrDAClient クラスの新しいインスタンスを初期化します。

名前空間: System.Net.Sockets
アセンブリ: System.Net.IrDA (system.net.irda.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

引数が null 参照です。

InvalidOperationException

サービス名が無効か、デバイスの検出時に見つかりませんでした。

.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IrDAClient クラス
IrDAClient メンバ
System.Net.Sockets 名前空間

IrDAClient コンストラクタ (IrDAEndPoint)

指定したエンドポイントに接続するための IrDAClient クラスの新しいインスタンスを初期化します。

名前空間: System.Net.Sockets
アセンブリ: System.Net.IrDA (system.net.irda.dll 内)
構文構文

Public Sub New ( _
    remoteEP As IrDAEndPoint _
)
Dim remoteEP As IrDAEndPoint

Dim instance As New IrDAClient(remoteEP)
public IrDAClient (
    IrDAEndPoint remoteEP
)
public:
IrDAClient (
    IrDAEndPoint^ remoteEP
)
public IrDAClient (
    IrDAEndPoint remoteEP
)
public function IrDAClient (
    remoteEP : IrDAEndPoint
)

パラメータ

remoteEP

IrDAEndPoint。

例外例外
例外の種類条件

ArgumentNullException

引数が null 参照です。

.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IrDAClient クラス
IrDAClient メンバ
System.Net.Sockets 名前空間

IrDAClient プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ RemoteMachineName 通信に参加しているデバイスの名前を取得します。
参照参照

関連項目

IrDAClient クラス
System.Net.Sockets 名前空間

その他の技術情報

方法 : 赤外線ファイル転送を行う

IrDAClient メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

IrDAClient クラス
System.Net.Sockets 名前空間

その他の技術情報

方法 : 赤外線ファイル転送を行う

IrDAClient メンバ

赤外線通信の接続サービスを提供します。

IrDAClient データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ RemoteMachineName 通信に参加しているデバイスの名前を取得します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

IrDAClient クラス
System.Net.Sockets 名前空間

その他の技術情報

方法 : 赤外線ファイル転送を行う



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「IrDAClient」の関連用語

IrDAClientのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IrDAClientのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS