home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / winsock / nwlink / dgrecv / dgrecv.c next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  313 lines

  1. /****************************************************************************\
  2. *  dgrecv.c -- sample program demonstrating NWLink.
  3. *
  4. *       Microsoft Developer Support
  5. *       Copyright (c) 1992-1997 Microsoft Corporation
  6. *
  7. *  This program is a simple example of opening a socket,
  8. *  binding to the socket, and waiting for a datagram packet.
  9. ****************************************************************************/
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <malloc.h>
  15. #include <wsipx.h>
  16. #include <wsnwlink.h>
  17. #include "../testlib/testlib.h"
  18.  
  19. /*
  20. *   Sockaddr structures 
  21. */
  22.  
  23. SOCKADDR_IPX addr;
  24. SOCKADDR_IPX baddr;
  25. SOCKADDR_IPX raddr;
  26.  
  27. /*
  28. *   Function Prototypes 
  29. */
  30.  
  31. extern int main(int, char **);
  32. extern int net_init(SOCKET *);
  33. extern int dg_recv(SOCKET);
  34.  
  35. /****************************************************************************
  36. *
  37. *    FUNCTION:  main( int argc, char **argv )
  38. *
  39. *    PURPOSE:   This is the main entry for the program
  40. *               
  41. *
  42. *    ARGUMENTS:    argc = Number of arguments
  43. *               argv = Array of ptrs to cmd line args
  44. *                
  45. *
  46. *     RETURNS:   Exit code for the program
  47. *                
  48. *\***************************************************************************/
  49. int main(int argc, char **argv)
  50. {
  51.     SOCKET s;
  52.  
  53.     /*
  54.     *   Initialize our default values before checking the command line 
  55.     */
  56.  
  57.     *Local_Socket_Number = 0x06;
  58.     *(Local_Socket_Number+1) = 0x00;
  59.  
  60.     /*
  61.     *   Parse the command line to set up any command line options 
  62.     */
  63.  
  64.     parse_cmd_line(argc, argv);
  65.  
  66.     /*
  67.     *   Initialize the network and set up our socket 
  68.     */
  69.  
  70.     if (net_init(&s))
  71.         return 1;
  72.  
  73.     /*
  74.     *   Do the receive thing 
  75.     */
  76.  
  77.     if (dg_recv(s))
  78.         return 1;
  79.  
  80.     /*
  81.     *   All Done - Close up the socket and exit 
  82.     */
  83.  
  84.     if (verbose)
  85.     printf("Calling closesocket()\n");
  86.  
  87.     closesocket(s);
  88.  
  89.     return 0;
  90. }
  91.  
  92. /****************************************************************************
  93. *
  94. *    FUNCTION:  net_init( SOCKET *skt )
  95. *
  96. *    PURPOSE:   Initializes the WinSock stuff and sets up our socket.
  97. *               
  98. *
  99. *    ARGUMENTS:    SOCKET * => struct to receive our socket info    
  100. *
  101. *     RETURNS:   0 if ok
  102. *                1 if error
  103. *
  104. *\***************************************************************************/
  105. int net_init(SOCKET *skt)
  106. {
  107.     SOCKET s;
  108.     int rc, addrlen = 16;
  109.     WSADATA wsdata;
  110.     WORD    wVersionRequested;
  111.  
  112.     if (verbose)
  113.         printf("Calling WSAStartup(), ");
  114.  
  115.     /*
  116.     *   Initialize with the WINSOCK library 
  117.     */
  118.  
  119.     wVersionRequested = MAKEWORD(1,1);
  120.     rc = WSAStartup(wVersionRequested, &wsdata);
  121.  
  122.     if (verbose)
  123.         printf("return = 0x%X (%d)\n", rc, rc);
  124.  
  125.     if (rc) {
  126.         printf("WSAStartup failed: error code = %d\n", rc);
  127.         return 1;
  128.     }
  129.  
  130.     if (verbose) {
  131.         printf("Contents of wsadata struct:\n");
  132.         print_wsa(&wsdata);
  133.     }
  134.  
  135.     /*
  136.     *   Open a DATAGRAM socket with IPX 
  137.     */
  138.  
  139.     if (verbose)
  140.         printf("Calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol);
  141.  
  142.     s = socket(Local_Address_Family, Socket_Type, Protocol);
  143.  
  144.     if (verbose)
  145.     printf("socket() returned 0x%X (%d)\n", s, s);
  146.  
  147.     if (s == INVALID_SOCKET) {
  148.         dos_net_perror("Socket call failed");
  149.         exit(1);
  150.     }
  151.  
  152.     /*
  153.     *    Bind to a socket.  We dont care what socket we bind to,
  154.     *    so we will send down all 0's
  155.     */
  156.  
  157.     addr.sa_family = Local_Address_Family;
  158.  
  159.     memcpy(&addr.sa_netnum, Local_Network_Number, 4);
  160.     memcpy(&addr.sa_nodenum, Local_Node_Number, 6);
  161.     memcpy(&addr.sa_socket, Local_Socket_Number, 2);
  162.  
  163.     if (verbose) {
  164.         printf("calling bind(), local address =\n  ");
  165.         print_saddr(&addr);
  166.     }
  167.  
  168.     rc = bind(s, (struct sockaddr *) &addr, 16);
  169.  
  170.     if (verbose)
  171.         printf("\nbind() returned 0x%X (%d)\n", rc, rc);
  172.  
  173.     if (rc == SOCKET_ERROR) {
  174.         dos_net_perror("Error binding to socket");
  175.         closesocket(s);
  176.         return 1;
  177.     }
  178.  
  179.     /*
  180.     *   Get the address we bound to and print it out 
  181.     */
  182.  
  183.     if (verbose)
  184.         printf("calling getsockname(socket = %d), ", s);
  185.  
  186.     rc = getsockname(s, (struct sockaddr *) &baddr, &addrlen);
  187.  
  188.     if (verbose)
  189.         printf("return = 0x%lX (%d)\n", rc, rc);
  190.  
  191.     if (rc == SOCKET_ERROR) {
  192.         dos_net_perror("Error getting socket name");
  193.         closesocket(s);
  194.         return 1;
  195.     }
  196.  
  197.     /*
  198.     *   Set the packet type to send for this socket 
  199.     */
  200.  
  201.     if (verbose)
  202.         printf("Calling setsockopt for packet type %d\n", Local_Packet_Type);
  203.  
  204.     rc = setsockopt(s, NSPROTO_IPX, IPX_PTYPE, (const char *) &Local_Packet_Type, 4);
  205.  
  206.     if (rc == SOCKET_ERROR)
  207.         dos_net_perror("setsockopt() call failed");
  208.  
  209.  
  210.     if (Filter_Packet_Type) {
  211.  
  212.         /*
  213.         *   Set the packet type for this socket 
  214.         */
  215.  
  216.         if (verbose)
  217.             printf("Calling setsockopt to filter packet type %d\n", Filter_Packet_Type);
  218.  
  219.     rc = setsockopt(s, NSPROTO_IPX, IPX_FILTERPTYPE, (const char *) &Filter_Packet_Type, sizeof(int));
  220.     printf("RC from FILTER SOCKOPT = %d\n", rc);
  221.  
  222.         if (rc == SOCKET_ERROR)
  223.             dos_net_perror("setsockopt() call failed setting filter packet type");
  224.     }
  225.     /*
  226.     *   Print out the network address 
  227.     */
  228.  
  229.     if (verbose) {
  230.         printf("addrlen = %d\n", addrlen);
  231.         print_netaddr(baddr.sa_netnum, "  Bound to address ", "\n");
  232.     }
  233.  
  234.     *skt = s;
  235.  
  236.     return 0;
  237. }
  238.  
  239. /****************************************************************************
  240. *
  241. *    FUNCTION:  dg_recv( SOCKET s )
  242. *
  243. *    PURPOSE:   Receives datagrams.
  244. *
  245. *    ARGUMENTS:    SOCKET   socket to receive on
  246. *
  247. *     RETURNS:   0 if ok
  248. *               1 if error
  249. *
  250. *\***************************************************************************/
  251. int dg_recv(SOCKET s)
  252. {
  253.     int rc, errflag = 0;
  254.     int addrlen = 16;
  255.     UINT dgrams = 0;
  256.     LPSTR recvbuf;
  257.  
  258.  
  259.     if (verbose)
  260.         printf("allocating %d bytes for receive buffer\n", Receive_Length);
  261.  
  262.     /*
  263.     *   Set up the data buffer to send 
  264.     */
  265.  
  266.     recvbuf = (LPSTR)malloc(Send_Length);
  267.  
  268.     if (!recvbuf) {
  269.         printf("Error allocating %d bytes for receive buffer\n", Receive_Length);
  270.         return 1;
  271.     }
  272.  
  273.     if (verbose) {
  274.         printf("calling recvfrom(socket = %d, length = %d),\n", s, Receive_Length);
  275.     }
  276.     else
  277.         printf("Waiting for call...\n");
  278.  
  279.     while (1) {
  280.         /*
  281.         *   Receive a packet from anyone 
  282.         */
  283.  
  284.         rc = recvfrom(s, recvbuf, Receive_Length, 0, (struct sockaddr *)&raddr, &addrlen);
  285.  
  286.         if (rc == SOCKET_ERROR) {
  287.             dos_net_perror("recvfrom() failed");
  288.             errflag++;
  289.             break;
  290.         }
  291.  
  292.         if (verbose) {
  293.             printf("Received %d bytes from \n  ", rc);
  294.             print_saddr(&raddr);
  295.             printf("\n");
  296.         }
  297.         else
  298.             printf("\rReceived packet %d, length = %d bytes", ++dgrams, rc);
  299.  
  300.  
  301.         if (No_Loop)
  302.             break;
  303.     }
  304.  
  305.     if (verbose)
  306.         printf("Freeing receive buffer\n");
  307.  
  308.     free(recvbuf);
  309.  
  310.     return errflag;
  311. }
  312.