Creating File Transfer Apps Using Infrared in .NET Compact Framework

Last Updated: Nov 02, 2025
5 min read
Legacy Archive

Connectivity between smart devices and personal computers is a necessity in this fast-moving world. This feature is possible using the classes provided in the .NET Compact Framework. These classes used for infrared communication are added to the System.Net.Sockets namespace.

The Infrared Data Association (IrDA) has set standards for socket programming, and infrared communication programming must implement these standards in the IrDA specifications. The Windows socket must be invoked using platform invoke on your personal computer since the full .NET Framework doesn't have the IrDA classes built-in.

Understanding IrDA Protocol and Architecture

This platform invoke on your personal computer is essential for communication between smart devices and the PC. The protocol defined by IrDA is for point-to-point communication between devices. This communication can be done over a short range only. Connecting multiple devices to the same host is also possible, where peer connectivity is established.

Functionalities such as client-server are implemented over infrared connectivity. The server is usually the personal computer, while the client is the smart device that initiates connectivity. The IrDA protocol stack has a layer called the Information Access Service (IAS). This IAS layer has both Server and Client components. The server has the services and applications available for IR connections that come to it, while the client discovers the connections available in the server.

Available IrDA Classes

The .NET Compact Framework provides several classes for working with infrared communication. Each class has its own specific purpose:

  • IrDACharacterSet: Determines the character set supported by IrDA devices
  • IrDAClient: Provides access to client functionality for sending and receiving data
  • IrDADeviceInfo: Finds connections available with the server for a particular device
  • IrDAEndPoint: Obtains infrared port information
  • IrDAHints: Provides enumeration list for device type and connection type
  • IrDAListener: Monitors available connections by placing a socket in listening state

The Start method of the IrDAListener class must be called to place the socket in a listening state. Otherwise, it won't be available for receiving connections.

Creating the Send Application

To create an application to transfer files using infrared communication, you'll need two devices (like Pocket PCs). One device will send data while the other receives it. You'll use the IrDAClient class and its DiscoverDevices method to find devices available within range. Information about devices within range is obtained using IrDADeviceInfo objects.

The tasks performed when you click the Send button include getting the file stream to be sent, creating an IrDAClient instance with a service name, and reading the file stream into the IrDAClient for sending.

SendFile.cs
using System.Net.Sockets;
using System.IO;

private void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        // Discover available IrDA devices
        IrDAClient client = new IrDAClient("FileTransfer");
        IrDADeviceInfo[] devices = client.DiscoverDevices(3);
        
        if (devices.Length > 0)
        {
            // Connect to first available device
            client.Connect(devices[0].DeviceID, "FileTransfer");
            
            // Open file to send
            FileStream fs = new FileStream("myfile.txt", FileMode.Open);
            
            // Get network stream
            Stream stream = client.GetStream();
            
            // Write file size first
            byte[] fileSize = BitConverter.GetBytes(fs.Length);
            stream.Write(fileSize, 0, fileSize.Length);
            
            // Create buffer and read file
            byte[] buffer = new byte[1024];
            int bytesRead;
            
            while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
            {
                stream.Write(buffer, 0, bytesRead);
            }
            
            fs.Close();
            client.Close();
            
            MessageBox.Show("File sent successfully!");
        }
        else
        {
            MessageBox.Show("No devices found");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

Creating the Receive Application

The tasks performed when you click the Receive button include creating a stream to write the received file, creating an IrDAEndPoint for the device that sends the file with the service name, starting the listening service with IrDAListener, creating an IrDAClient object using AcceptIrDAClient method, reading the stream that has the data, and writing the received stream into a text file.

ReceiveFile.cs
using System.Net.Sockets;
using System.IO;

private void btnReceive_Click(object sender, EventArgs e)
{
    try
    {
        // Create IrDA endpoint
        IrDAEndPoint endpoint = new IrDAEndPoint(
            new byte[] {0, 0, 0, 0}, 
            "FileTransfer"
        );
        
        // Create and start listener
        IrDAListener listener = new IrDAListener(endpoint);
        listener.Start();
        
        MessageBox.Show("Waiting for file...");
        
        // Accept incoming connection
        IrDAClient client = listener.AcceptIrDAClient();
        
        // Get network stream
        Stream stream = client.GetStream();
        
        // Read file size first
        byte[] fileSizeBytes = new byte[8];
        stream.Read(fileSizeBytes, 0, fileSizeBytes.Length);
        long fileSize = BitConverter.ToInt64(fileSizeBytes, 0);
        
        // Create file stream for writing
        FileStream fs = new FileStream("received_file.txt", 
                                       FileMode.Create);
        
        // Read and write data
        byte[] buffer = new byte[1024];
        int bytesRead;
        long totalRead = 0;
        
        while (totalRead < fileSize && 
               (bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            fs.Write(buffer, 0, bytesRead);
            totalRead += bytesRead;
        }
        
        fs.Close();
        client.Close();
        listener.Stop();
        
        MessageBox.Show("File received successfully!");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.Message);
    }
}

Implementation Steps

To create applications to send and receive files, follow these steps:

For the Send Application:

  1. Create a Pocket PC application project
  2. Add a Send button to the form
  3. Create a text file to send in a folder
  4. Add code to the click event of the Send button

For the Receive Application:

  1. Create a Pocket PC application project
  2. Add a Receive button to the form
  3. Add code to the click event of the Receive button

Testing Your Applications

To run the applications you've created, align the infrared ports of both devices. Before this, install the applications on the respective devices. Click the Receive button on the receiving device first to put it in listening mode. Then click the Send button on the device that will send the data or file.

The text file that's sent to the receiving device should be available in the folder you've assigned for that purpose. Check whether you've received the file successfully. A walkthrough code is available for this kind of application on the MSDN website. You can download that code and test it.

FAQ

What are the IrDA classes available in .NET Compact Framework?

The main classes include IrDAClient (for sending/receiving data), IrDAListener (for monitoring connections), IrDADeviceInfo (for discovering available devices), IrDAEndPoint (for infrared port information), IrDACharacterSet (for character set support), and IrDAHints (for device type enumeration).

How does the sending device initiate file transfer?

The sending device gets the file stream, creates an IrDAClient instance with a service name, discovers available devices using DiscoverDevices, and then reads the file stream into the IrDAClient for transmission.

What must the receiving device do before files can be sent to it?

The receiving device must be in listening mode before the sender transmits. It creates an IrDAEndPoint, starts the IrDAListener service, accepts the connection using AcceptIrDAClient, and then reads and writes the received stream to a file.

What's the range limitation for IrDA communication?

IrDA communication works over short ranges only, as defined by the Infrared Data Association specifications. Devices must be properly aligned with their IR ports facing each other for successful communication.

Back to Articles