home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / sockauth / comm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-09  |  2.9 KB  |  162 lines

  1. /*++
  2.  
  3. Copyright 1996-1997 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     comm.c
  8.  
  9. Abstract:
  10.  
  11.     Implements a set of common operations for socket communication
  12.  
  13. Revision History:
  14.  
  15. --*/
  16.  
  17. #include <windows.h>
  18. #include <winsock.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "comm.h"
  22.  
  23. BOOL InitWinsock ()
  24. {
  25.     int nRes;
  26.     WSADATA wsaData;
  27.     WORD wVerRequested = 0x0101; // ver 1.1
  28.  
  29.     // Init the sockets interface
  30.     nRes = WSAStartup (wVerRequested, &wsaData);
  31.     if (nRes)  {
  32.         fprintf (stderr, "Couldn't init winsock: %d\n", nRes);
  33.         return(FALSE);
  34.     }
  35.  
  36.     return(TRUE);
  37. }
  38.  
  39. BOOL TermWinsock ()
  40. {
  41.     if (SOCKET_ERROR == WSACleanup ())
  42.         return(FALSE);
  43.     else
  44.         return(TRUE);
  45. }
  46.  
  47. BOOL SendMsg (SOCKET s, PBYTE pBuf, DWORD cbBuf)
  48. /*++
  49.  
  50.  Routine Description:
  51.  
  52.     Sends a message over the socket by first sending a DWORD that
  53.     represents the size of the message followed by the message itself.
  54.  
  55.  Return Value:
  56.  
  57.     Returns TRUE is successful; otherwise FALSE is returned.
  58.  
  59. --*/
  60. {
  61.     if (0 == cbBuf)
  62.         return(TRUE);
  63.  
  64.     // send the size of the message
  65.     //
  66.     if (!SendBytes (s, (PBYTE)&cbBuf, sizeof (cbBuf)))
  67.         return(FALSE);
  68.     
  69.     // send the body of the message
  70.     //
  71.     if (!SendBytes (s, pBuf, cbBuf))
  72.         return(FALSE);
  73.  
  74.     return(TRUE);
  75. }    
  76.  
  77. BOOL ReceiveMsg (SOCKET s, PBYTE pBuf, DWORD cbBuf, DWORD *pcbRead)
  78. /*++
  79.  
  80.  Routine Description:
  81.  
  82.     Receives a message over the socket.  The first DWORD in the message
  83.     will be the message size.  The remainder of the bytes will be the
  84.     actual message.
  85.  
  86.  Return Value:
  87.  
  88.     Returns TRUE is successful; otherwise FALSE is returned.
  89.  
  90. --*/
  91. {
  92.     DWORD cbRead;
  93.     DWORD cbData;
  94.  
  95.     // find out how much data is in the message
  96.     //    
  97.     if (!ReceiveBytes (s, (PBYTE)&cbData, sizeof (cbData), &cbRead))
  98.         return(FALSE);
  99.     
  100.     if (sizeof (cbData) != cbRead)
  101.         return(FALSE);
  102.     
  103.     // Read the full message
  104.     //
  105.     if (!ReceiveBytes (s, pBuf, cbData, &cbRead))
  106.         return(FALSE);
  107.     
  108.     if (cbRead != cbData)
  109.         return(FALSE);
  110.  
  111.     *pcbRead = cbRead;
  112.  
  113.     return(TRUE);
  114. }    
  115.  
  116. BOOL SendBytes (SOCKET s, PBYTE pBuf, DWORD cbBuf)
  117. {
  118.     PBYTE pTemp = pBuf;
  119.     int cbSent, cbRemaining = cbBuf;
  120.  
  121.     if (0 == cbBuf)
  122.         return(TRUE);
  123.         
  124.     while (cbRemaining) {
  125.         cbSent = send (s, pTemp, cbRemaining, 0);
  126.         if (SOCKET_ERROR == cbSent) {
  127.             fprintf (stderr, "send failed: %u\n", GetLastError ());
  128.             return FALSE;
  129.         }
  130.  
  131.         pTemp += cbSent;
  132.         cbRemaining -= cbSent;
  133.     }
  134.  
  135.     return TRUE;
  136. }
  137.  
  138. BOOL ReceiveBytes (SOCKET s, PBYTE pBuf, DWORD cbBuf, DWORD *pcbRead)
  139. {
  140.     PBYTE pTemp = pBuf;
  141.     int cbRead, cbRemaining = cbBuf;
  142.  
  143.     while (cbRemaining) {
  144.         cbRead = recv (s, pTemp, cbRemaining, 0);
  145.         if (0 == cbRead)
  146.             break;
  147.  
  148.         if (SOCKET_ERROR == cbRead) {
  149.             fprintf (stderr, "recv failed: %u\n", GetLastError ());
  150.             return FALSE;
  151.         }
  152.  
  153.         cbRemaining -= cbRead;
  154.         pTemp += cbRead;
  155.     }
  156.  
  157.     *pcbRead = cbBuf - cbRemaining;
  158.  
  159.     return TRUE;
  160. }
  161.  
  162.