Sample Infrared Socket Client

This sample IrSock client opens a socket and makes five attempts to locate a server. If none is found, it displays a message box to inform the user of the failure. When a server is detected, the client queries the server for its device identifier and sends a greeting to the service named "My Server." It then waits for the server to respond, displays a message box with the response, and closes the socket.

#include <windows.h>
#include <af_irda.h>

#define    NumRetries    5

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  LPTSTR lpCmdLine, int nCmdShow )
{
   SOCKET         sock;
   SOCKADDR_IRDA  address = {AF_IRDA, 0,0,0,0, "MyServer"};
    EVICELIST     devList;
   int            devListLen = sizeof(devList),
                  cnt = 0,
                  idx = 0;
   char           helloClient[25];
   TCHAR         helloText[25];

   sock = socket(AF_IRDA, SOCK_STREAM, 0);
   devList.numDevice = 0;      // initialize number of devices to zero

   while ((devList.numDevice == 0) && (cnt <= NumRetries))
   {
         getsockopt(sock, SOL_IRLMP, IRLMP_ENUMDEVICES,
                  (char *)&devList, &devListLen);
         cnt++;
         Sleep(1000);          // Wait one second before retrying
   }
   if  (cnt > NumRetries)
   {
         MessageBox (NULL, TEXT("Server could not be located"),
                   TEXT("IR Client"), MB_OK);
   }
   else
    {
                               // Get socket address of server
         for (idx = 0; idx <= 3; idx++)
            address.irdaDeviceID[idx] =
            devList.Device[0].irdaDeviceID[idx];

         connect(sock, (struct sockaddr *)&address,
                     sizeof(SOCKADDR_IRDA));

        send(sock, "Hello Server!", strlen("Hello Server!")+1, 0);

        recv(sock, helloClient, sizeof(helloClient), 0);

        for (idx = 0; idx <= sizeof(helloClient); idx++)
             helloText[idx] = helloClient[idx];

        MessageBox (NULL, helloText, TEXT("IR Client"), MB_OK);

        closesocket(sock);
   }
return 0;
}