Поделиться через


IrSock Client Application (Windows Embedded CE 6.0)

1/6/2010

The basic procedure for using IrSock is similar to that for Winsock. IrSock uses only the stream socket connection-oriented communication method.

To create and use a socket with a client application

  1. Open a stream socket with socket, as with the server application. Use AF_IRDA for the address format parameter, SOCK_STREAM for the type and NULL for the protocol parameter

  2. Search for the available devices, and retrieve its address with getsockopt. For more information, see Discovering Devices Using IrSock.

  3. Connect to the server with the connect function using SOCKADDR_IRDA for the name parameter.

  4. Use send and recv to communicate with the server.

  5. Close the socket with closesocket.

For information about creating a server application, see IrSock Server Application

The following code sample shows how an IrSock client opens a socket and makes five attempts to locate a server. If no server is found, the client displays a dialog 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 NUMRETYR  5                   // Maximum number of retries

int WINAPI WinMain (
              HINSTANCE hInstance,    // Handle to the current instance
              HINSTANCE hPrevInstance,// Handle to the previous instance
              LPTSTR lpCmdLine,       // Pointer to the command line
              int nCmdShow)           // Show window state. 
{
  SOCKET sock;                    // Socket bound to the server
  DEVICELIST devList;             // Device list
  SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IRServer"};
                                  // Specifies the server socket address
  int iCount = 0,                 // Number of retries
      index = 0,                  // Integer index
      iReturn,                    // Return value of recv function
      iDevListLen = sizeof (devList);
                                  // Size of the device list
  char szClientA[100];            // ASCII string 
  TCHAR szClientW[100];           // Unicode string
  TCHAR szError[100];             // Error message string

  // Create a socket that is bound to the server.
  if ((sock = socket (AF_IRDA, SOCK_STREAM, 0)) == INVALID_SOCKET)
  {
    wsprintf (szError, TEXT("Allocating socket failed. Error: %d"), 
              WSAGetLastError ());
    MessageBox (NULL, szError, TEXT("Error"), MB_OK);
    return FALSE;
  }
  
  // Initialize the number of devices to zero.
  devList.numDevice = 0;      

  while ( (devList.numDevice == 0) && (iCount <= NUMRETYR))
  {
    // Retrieve the socket option.
    if (getsockopt (sock, SOL_IRLMP, IRLMP_ENUMDEVICES, 
                    (char *)&devList, &iDevListLen) == SOCKET_ERROR)
    {
      wsprintf (szError, TEXT("Server could not be located, getsockopt")
                TEXT(" failed. Error: %d"), WSAGetLastError ());
      MessageBox (NULL, szError, TEXT("Error"), MB_OK);
      closesocket (sock);
      return FALSE;
    }
    
    iCount++;

    // Wait one second before retrying.
    Sleep (1000);          
  }

  if (iCount > NUMRETYR)
  {
    MessageBox (NULL, TEXT ("Server could not be located!"), 
                TEXT ("Error"), MB_OK);
    closesocket (sock);
    return FALSE;
  }

  // Get the server socket address.
  for (index = 0; index <= 3; index++)
  {
    address.irdaDeviceID[index] = devList.Device[0].irdaDeviceID[index];
  }

  // Establish a connection to the socket.
  if (connect (sock, (struct sockaddr *)&address, 
               sizeof (SOCKADDR_IRDA)) == SOCKET_ERROR) 
  {
    wsprintf (szError, 
              TEXT("Connecting to the server failed. Error: %d"),
              WSAGetLastError ());
    MessageBox (NULL, szError, TEXT("Error"), MB_OK);
    closesocket (sock);
    return FALSE;
  }

  // Send a string from the client socket to the server socket.
  if (send (sock, "To Server.", strlen ("To Server.") + 1, 0)
           == SOCKET_ERROR) 
  {
    wsprintf (szError, 
              TEXT("Sending data to the server failed. Error: %d"),
              WSAGetLastError ());
    MessageBox (NULL, szError, TEXT("Error"), MB_OK);
  }

  // Receive data from the server socket.
  iReturn = recv (sock, szClientA, sizeof (szClientA), 0);

  // Check if there is any data received. If there is, display it.
  if (iReturn == SOCKET_ERROR)
  {
    wsprintf (szError, TEXT("No data is received, recv failed.")
              TEXT(" Error: %d"), WSAGetLastError ());
    MessageBox (NULL, szError, TEXT("Client"), MB_OK);
  }
  else if (iReturn == 0)
  {
    MessageBox (NULL, TEXT("Finished receiving data"), TEXT("Client"),
                MB_OK);
  }
  else
  {
    // Convert the ASCII string to a Unicode string
    szServerA[99] = 0;
    MultiByteToWideChar(CP_ACP, 0, szServerA, -1,
                                   szServerW, sizeof(szServerW)/sizeof(szServerW[0]));
    // Display the string received from the server.
    MessageBox (NULL, szClientW, TEXT("Received From Server"), MB_OK);
  }

  // Close the socket.
  closesocket (sock);

  return 0;
}

See Also

Concepts

Creating an Infrared Winsock Application
IrSock Server Application
IrDA Application Development