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 / connect / connect.c next >
C/C++ Source or Header  |  1997-10-05  |  9KB  |  396 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 using SPX connect.
  8. ****************************************************************************/
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <malloc.h>
  14. #include <wsipx.h>
  15. #include "../testlib/testlib.h"
  16.  
  17. SOCKADDR_IPX addr;
  18. SOCKADDR_IPX baddr;
  19. SOCKADDR_IPX caddr;
  20.  
  21. UCHAR ch = 0;
  22.  
  23. /*
  24. *   Function Prototypes 
  25. */
  26.  
  27. extern int main(int, char **);
  28. extern int net_init(SOCKET *);
  29. extern int make_connection(SOCKET);
  30. extern int do_send_receive(SOCKET);
  31.  
  32. /****************************************************************************
  33. *
  34. *    FUNCTION:  main( int argc, char **argv )
  35. *
  36. *    PURPOSE:   This is the main entry for the program
  37. *               
  38. *
  39. *    ARGUMENTS:    argc = Number of arguments
  40. *               argv = Array of ptrs to cmd line args
  41. *                
  42. *
  43. *     RETURNS:   Exit code for the program
  44. *                
  45. *\***************************************************************************/
  46. int main(int argc, char **argv)
  47. {
  48.     SOCKET s;
  49.  
  50.     /*
  51.     *   Set up our default values before checking command line 
  52.     */
  53.  
  54.     memcpy(Remote_Node_Number, "\x08\x00\x2B\x2E\x98\xA8", 6);
  55.     memcpy(Remote_Socket_Number, "\x05\x00", 2);
  56.     Socket_Type = SOCK_STREAM;
  57.     Protocol = NSPROTO_SPX;
  58.     Remote_Address_Family = AF_NS;
  59.     Sleep_Time = 250;
  60.  
  61.     /*
  62.     *   Get any command line options 
  63.     */
  64.  
  65.     parse_cmd_line(argc, argv);
  66.  
  67.     /*
  68.     *   Initialize the network and set up the socket 
  69.     */
  70.  
  71.     if (net_init(&s))
  72.         return 1;
  73.  
  74.     /*
  75.     *   Try to connect to our server 
  76.     */
  77.  
  78.     if (make_connection(s))
  79.         return 1;
  80.  
  81.     /*
  82.     *   Send/receive data to/from server 
  83.     */
  84.  
  85.     do_send_receive(s);
  86.  
  87.     /*
  88.     *   All done - close up 
  89.     */
  90.  
  91.     if (verbose)
  92.         printf("calling closesocket(socket = %d)\n", s);
  93.  
  94.     closesocket(s);
  95.     return 0;
  96. }
  97.  
  98. /****************************************************************************
  99. *
  100. *    FUNCTION:  net_init( SOCKET *skt )
  101. *
  102. *    PURPOSE:   Initializes the WinSock stuff and sets up our socket.
  103. *               
  104. *
  105. *    ARGUMENTS:    SOCKET * => struct to fill in with opened socket.
  106. *
  107. *     RETURNS:   0 if ok
  108. *                1 if error
  109. *
  110. *\***************************************************************************/
  111. int net_init(SOCKET *skt)
  112. {
  113.     SOCKET s;
  114.     int rc, addrlen;
  115.     WSADATA wsdata;
  116.     WORD    wVersionRequested;
  117.  
  118.     wVersionRequested = MAKEWORD(1,1);
  119.  
  120.  
  121.     /*
  122.     *   Register with the socket library 
  123.     */
  124.  
  125.     rc = WSAStartup(wVersionRequested, &wsdata);
  126.  
  127.     if (verbose)
  128.         printf("WSAStartup returned 0x%X\n", rc);
  129.  
  130.     if (rc) {
  131.         printf("WSAStartup failed: error = %d\n", rc);
  132.         return 1;
  133.     }
  134.  
  135.     if (verbose) {
  136.         printf("contents of wsdata structure: \n");
  137.         print_wsa(&wsdata);
  138.         printf("calling socket(address family = %d, socket type = %d, protocol = %d)\n", Local_Address_Family, Socket_Type, Protocol);
  139.     }
  140.  
  141.     /*
  142.     *   Open a STREAMING socket 
  143.     */
  144.  
  145.     s = socket(Local_Address_Family, Socket_Type, Protocol);
  146.  
  147.     if (verbose)
  148.         printf("socket() returned 0x%X (%d)\n", s, s);
  149.  
  150.     if (s == INVALID_SOCKET) {
  151.         dos_net_perror("socket call failed");
  152.         return 1;
  153.     }
  154.  
  155.     /*
  156.     *   Bind to any address 
  157.     */
  158.  
  159.     addr.sa_family = Local_Address_Family;
  160.     memcpy(&addr.sa_netnum, Local_Network_Number, 4);
  161.     memcpy(&addr.sa_nodenum, Local_Node_Number, 6);
  162.     memcpy(&addr.sa_socket, Local_Socket_Number, 2);
  163.  
  164.     rc = bind(s, (const struct sockaddr FAR *)&addr, 16);
  165.  
  166.     if (verbose)
  167.         printf("bind() returned 0x%X\n", rc);
  168.  
  169.     if (rc == SOCKET_ERROR) {
  170.         dos_net_perror("bind call failed");
  171.         return 1;
  172.     }
  173.  
  174.     if (verbose)
  175.         printf("calling getsockname(socket = %d)\n", s);
  176.  
  177.     addrlen = 16;
  178.     rc = getsockname(s, (struct sockaddr *) &addr, &addrlen);
  179.  
  180.     if (verbose)
  181.         printf("getsockname() returned 0x%X\n", rc);
  182.  
  183.     if (rc == SOCKET_ERROR) {
  184.         dos_net_perror("Error binding to socket");
  185.         closesocket(s);
  186.         return 1;
  187.     }
  188.  
  189.     if (verbose) {
  190.         printf("addrlen = %d\n", addrlen);
  191.         print_netaddr(addr.sa_netnum, "Bound address = ", "\n");
  192.     }
  193.  
  194.  
  195.     /*
  196.     *   Build the address of the node to connect to 
  197.     */
  198.  
  199.     memcpy(&caddr.sa_netnum, Remote_Network_Number, 4);
  200.     memcpy(&caddr.sa_nodenum, Remote_Node_Number, 6);
  201.     memcpy(&caddr.sa_socket, Remote_Socket_Number, 2);
  202.     caddr.sa_family = AF_NS;
  203.  
  204.     /*
  205.     *   Set up socket for return 
  206.     */
  207.  
  208.     *skt = s;
  209.  
  210.     return 0;
  211. }
  212.  
  213. /****************************************************************************
  214. *
  215. *    FUNCTION:  make_connection( SOCKET s )
  216. *
  217. *    PURPOSE:   Establishes a connection with our server.
  218. *
  219. *    ARGUMENTS:    SOCKET socket to use for connection
  220. *
  221. *     RETURNS:   0 if ok
  222. *                1 if error
  223. *
  224. *\***************************************************************************/
  225. int make_connection(SOCKET s)
  226. {
  227.     int rc, addrlen;
  228.  
  229.     /*
  230.     *   Connect 
  231.     */
  232.  
  233.     if (verbose)
  234.         printf("calling connect(socket = %d)\n", s);
  235.  
  236.     rc = connect(s, (const struct sockaddr FAR *)&caddr, 16);
  237.  
  238.     if (verbose)
  239.         printf("connect() returned 0x%X\n", rc);
  240.  
  241.     if (rc == SOCKET_ERROR) {
  242.         dos_net_perror("connect call failed");
  243.         return 1;
  244.     }
  245.  
  246.     printf("Connect OK\n");
  247.  
  248.     /*
  249.     *   Print out address we connected to 
  250.     */
  251.  
  252.     if (verbose)
  253.         printf("calling getpeername(socket = %d)\n", s);
  254.  
  255.     addrlen = 16;
  256.     rc = getpeername(s, (struct sockaddr *) &caddr, &addrlen);
  257.  
  258.     if (verbose)
  259.         printf("getpeername() returned 0x%X\n", rc);
  260.  
  261.     if (verbose) {
  262.         printf("addrlen = %d\n", addrlen);
  263.         print_netaddr(caddr.sa_netnum, "Remote Address = ", "\n");
  264.     }
  265.  
  266.     return 0;
  267. }
  268. /****************************************************************************
  269. *
  270. *    FUNCTION:  do_send_receive( SOCKET *s )
  271. *
  272. *    PURPOSE:   Alternately sends/receives data to/from the server.
  273. *
  274. *    ARGUMENTS:    SOCKET socket to transmit on
  275. *
  276. *     RETURNS:   0 if ok
  277. *                1 if error
  278. *
  279. *\***************************************************************************/
  280. int do_send_receive(SOCKET s)
  281. {
  282.     int rc, errflag = 0;
  283.     int sndpkts = 0, rcvpkts = 0;
  284.     LPSTR sendbuf;
  285.  
  286.     /*
  287.     *   Allocate a send buffer 
  288.     */
  289.  
  290.     if (verbose)
  291.         printf("Allocating %d bytes for send buffer \n");
  292.  
  293.     sendbuf = malloc(Send_Length);
  294.  
  295.     if (!sendbuf) {
  296.         printf("Error allocating %d bytes for send buffer\n");
  297.         return 1;
  298.     }
  299.  
  300.     /*
  301.     *   Send data and recv it back 
  302.     */
  303.  
  304.     while (1) {
  305.  
  306.         /*
  307.         *   Fill the buffer with our current character 
  308.         */
  309.  
  310.         memset(sendbuf, ch, Send_Length);
  311.  
  312.         /*
  313.         *   Send data 
  314.         */
  315.  
  316.         if (verbose)
  317.         printf("calling send(socket = %d, length = %d)\n", s, Send_Length);
  318.  
  319.         rc = send(s, sendbuf, Send_Length, 0);
  320.  
  321.         if (verbose)
  322.             printf("send() returned 0x%X\n", rc);
  323.  
  324.         if (rc == SOCKET_ERROR) {
  325.             dos_net_perror("\nsend() call failed");
  326.             errflag++;
  327.             break;
  328.         }
  329.  
  330.         if (verbose)
  331.             printf("Sent packet %d: sent %d bytes\n", sndpkts++, rc);
  332.         else
  333.             printf("\rSent packet %d: sent %d bytes... ", sndpkts++, rc);
  334.  
  335.         /*
  336.         *   Receive the data back 
  337.         */
  338.  
  339.         if (verbose)
  340.             printf("calling recv(socket = %d, length = %d)\n", s, Send_Length);
  341.  
  342.         rc = recv(s, sendbuf, Send_Length, 0);
  343.  
  344.         if (verbose)
  345.             printf("recv() returned %d\n", rc);
  346.  
  347.         if (rc == SOCKET_ERROR) {
  348.             dos_net_perror("\nrecv() call failed");
  349.             errflag++;
  350.             break;
  351.         }
  352.  
  353.         if (!rc) {
  354.             printf("connection has been lost\n");
  355.             break;
  356.         }
  357.  
  358.         printf("Received %d bytes", rc);
  359.  
  360.         if (verbose)
  361.             printf("\n");
  362.  
  363.         /*
  364.         *   See if our buffer has the same data in it 
  365.         */
  366.  
  367.         rc = mem_check(sendbuf, ch++, Send_Length);
  368.  
  369.         if (rc)
  370.             printf("Data compare error: packet = %d, offset = %d\n", (sndpkts-1), rc);
  371.         else if (verbose)
  372.             printf("Data compares okay\n");
  373.  
  374.         /*
  375.         *   If we are to send just 1, break out 
  376.         */
  377.  
  378.         if (No_Loop)
  379.             break;
  380.  
  381.         /*
  382.         *   Pause a little while (don't trash the network) 
  383.         */
  384.  
  385.         Sleep(Sleep_Time);
  386.     }
  387.  
  388.     if (verbose)
  389.         printf("\nFreeing send buffer\n");
  390.  
  391.     free(sendbuf);
  392.  
  393.     return errflag;
  394. }
  395.