26.7 Creating an IrDA Server

 <  Day Day Up  >  

You want to create a server application that listens for incoming connection from an infrared port.


The last recipe created the send portion of an IR chat application. This recipe shows how to add the second piece of the equation, receiving data from an IR port. Receiving data uses an IrDAListener object that blocks until a connection is made, receives data, and then goes back into its blocking listening state. The blocking method is named AcceptIrDAClient , which, as its name suggests, returns an IrDAClient object that it has successfully connected to. Because this method is a blocking call, you want to use it within a secondary thread if your application uses a Windows Form. If you are continuing with the application created in the previous recipe, add two fields to the form class: an IrDAListener and a Thread that will be used for receiving data. Additionally, within the form constructor, create an instance of both objects. The Thread constructor accepts a ThreadStart delegate, which is shown following this recipe. At this point, your class variables and new constructor should appear as shown in Listing 26.6.

Listing 26.6 Creating a Listener Thread
 using System; using System.Threading; using System.Net.Sockets; using System.IO; using System.Text; namespace _7_IRChatClient {     public class Form1 : System.Windows.Forms.Form     {         private System.Windows.Forms.TextBox tbTranscript;         private System.Windows.Forms.TextBox tbMessage;         private System.Windows.Forms.Button btnSend;         private System.Windows.Forms.MainMenu mainMenu1;         private System.Windows.Forms.MenuItem menuItem1;         private System.Windows.Forms.MenuItem mnuExit;         private Thread listenerThread;         private string serviceType = "IRDA_CHAT";         IrDAListener listener;         public Form1()         {             InitializeComponent();             // create the IR listener             listener = new IrDAListener( serviceType );             // create the thread and start it             listenerThread = new Thread( new ThreadStart(                 ListenerThreadMethod ));             listenerThread.Start();         }         void ListenerThreadMethod()         {         }         // continued... } 

The code passed a string value into the constructor of the IrDAListener constructor. This value is the service name that the listener will use when listening for incoming connections. In the previous recipe, a client wanting to send data creates an IrDAClient object passing this same string for the service name. As you can see, this method is quite similar to the methods used in sockets, but instead of integers being used for ports, strings are used in IR communication for service names .

The next step is to put the IrDAListener object in a blocked state while it listens for incoming connections. You call the AcceptIrDAClient method, which, as mentioned earlier, returns an IrDAClient object once a connection is made. At this point, the application is ready to receive data. To receive the incoming data from the connected client, call the GetStream method, which returns a Stream object. Next, call the Read method from the Stream object passing a byte array to fill; the offset into the array to start, which is normally 0; and the maximum bytes to read. Once this step is finished, you can close the Stream and IrDAClient objects and start listening for incoming connections again. Listing 26.7 demonstrates the techniques just described contained within the Thread delegate.

Listing 26.7 Connecting and Communicating with Infrared Clients
 void ListenerThreadMethod() {     int bytesRead = 0;     IrDAClient client = null;   // the connecting client     Stream byteStream = null;     byte[] buffer = new byte[1024];     try     {         do         {             listener.Start();             client = listener.AcceptIrDAClient();             byteStream = client.GetStream();             bytesRead = byteStream.Read( buffer, 0, 1024 );             if( byteStream != null )                 byteStream.Close();             if( client != null )                 client.Close();             AddMessage( client.RemoteMachineName,                 Encoding.ASCII.GetString( buffer, 0, bytesRead ));         } while (true);     }     finally     {         if( byteStream != null )             byteStream.Close();         if( client != null )             client.Close();         listener.Stop();     } } 


This recipe and the last introduced the IrDAListener and IrDAClient classes. These two classes perform the majority of IR communications within the .NET Compact Framework. However, you can use another IR class to obtain more information about a connected client. The IrDADeviceInfo class allows you to get information about a device, such as its DeviceID , DeviceName , supported CharacterSet , and a value from the IrDAHints enumerate data type through the IrDADeviceInfo property Hints . These Hints indicate whether the device is a fax machine, PDA, printer, or computer, to name a few. However, devices aren't required to return that information, so you shouldn't use it as an indicator for what type of device you are discovering. You use the IrDADeviceInfo class in conjunction with an IrDAClient object. The IrDAClient class contains a method named DiscoverDevices , which returns an array of IrDADeviceInfo methods for any devices discovered in its IR range. Listing 26.8 uses an IrDAClient to discover any devices within range, and if it finds any, it places the information in the transcript TextBox for the application created in this recipe.

Listing 26.8 Device Discovery Using Infrared
 private void mnuDevInfo_Click(object sender, System.EventArgs e) {     IrDAClient client = new IrDAClient();     IrDADeviceInfo[] devices = client.DiscoverDevices(10);     foreach( IrDADeviceInfo device in devices )     {         AddMessage( "Found", "DeviceID = " + FormatDeviceID(device.DeviceID) +             " DeviceName: " + device.DeviceName +             " Hints: " +  device.Hints.ToString() );     } } private string FormatDeviceID( byte[] deviceID ) {     string ret = "0x";     for( int i = 0; i < 4; i++ )     {         ret += Convert.ToInt32(deviceID[i]).ToString("X");     }     return ret; } 
 <  Day Day Up  >  

Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net