Sample Infrared Socket Server

This sample IrSock server allocates a socket and binds it to the IAS name, "MyServer." It then allocates a single connection object and prepares the server to listen for incoming connections. When the client contacts the server, the server accepts the connection. It then receives a string from the client, passes one back, and closes the connection.

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                  LPTSTR lpCmdLine, int nCmdShow )
{
   SOCKET         ServerSock,
                  ClientSock;

   SOCKADDR_IRDA  address = {AF_IRDA, 0, 0, 0, 0, "MyServer"};

   char           helloServer[25];               // ASCII String 
   TCHAR          helloText[25];                 // UNICODE String
   int            idx = 0;

   ServerSock = socket(AF_IRDA, SOCK_STREAM, 0);

   bind(ServerSock, (struct sockaddr *)&address, sizeof(address));

   listen(ServerSock, 1);

   ClientSock = accept(ServerSock, 0, 0);

   recv(ClientSock, helloServer, sizeof(helloServer), 0);

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

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

   send(ClientSock, "Hello Client!", strlen("Hello Client!")+1, 0);

   closesocket(ClientSock);
   closesocket(ServerSock);

return 0;
}