home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / tcpiptk / sendfile / recv.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  5KB  |  186 lines

  1. /********************************************************copyrite.xic********/
  2. /*                                                                          */
  3. /*   Licensed Materials - Property of IBM                                   */
  4. /*   IBM TCP/IP for OS/2                                                    */
  5. /*   (C) Copyright IBM Corporation. 1990,1996.                              */
  6. /*                                                                          */
  7. /*   All rights reserved.                                                   */
  8. /*                                                                          */
  9. /*   US Government Users Restricted Rights -                                */
  10. /*   Use, duplication or disclosure restricted by GSA ADP Schedule          */
  11. /*   Contract with IBM Corp.                                                */
  12. /*                                                                          */
  13. /*--------------------------------------------------------------------------*/
  14. /*  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is             */
  15. /*  sample code created by IBM Corporation. This sample code is not         */
  16. /*  part of any standard or IBM product and is provided to you solely       */
  17. /*  for  the purpose of assisting you in the development of your            */
  18. /*  applications.  The code is provided "AS IS", without                    */
  19. /*  warranty of any kind.  IBM shall not be liable for any damages          */
  20. /*  arising out of your use of the sample code, even if they have been      */
  21. /*  advised of the possibility of such damages.                             */
  22. /*--------------------------------------------------------------------------*/
  23. #define INCL_32
  24.  
  25. #include <io.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <types.h>
  30. #include <sys\socket.h>
  31. #include <netinet\in.h>
  32. #include <arpa\inet.h>
  33. #include <netinet\tcp.h>
  34. #include <fcntl.h>
  35.  
  36.  
  37. int getfile ();
  38.  
  39.  
  40. #define FILENAMESIZE    128
  41.  
  42. char    serveraddress[FILENAMESIZE];
  43. char    clientaddress[FILENAMESIZE];
  44. char    filename[256];
  45. int     myport ; /* port to fecv init info on */
  46. int  rc;
  47.  
  48.  
  49. struct _FileHeader
  50. {
  51.   unsigned long Offset;
  52.   unsigned long Size;
  53.   char  FileName[256];
  54. };
  55.  
  56.  
  57. struct fileattrib {
  58.                     char filename[125];
  59.                     long filesize;
  60.                   };
  61.  
  62. struct fileattrib fa;
  63.  char *temp;
  64.  char message[1000];
  65.  int s1, s2;
  66. FILE *fd;
  67.  
  68. void main( int argc, char *argv[])
  69. {
  70.  struct sockaddr_in clientname;
  71.  int                clientnamelength;
  72.  struct in_addr     clientaddr;
  73.  struct sockaddr_in server;
  74.    if(argc<3)
  75.     {
  76.      printf(" usage : <ip_addr> <port>");
  77.      exit(0);
  78.     }
  79.  
  80.    strcpy (serveraddress,argv[1]);
  81.    myport = atoi(argv[2]);
  82.  
  83.           printf ("Waiting for TestInit message from client\n");
  84.  
  85.           rc = getfile();
  86.  
  87.           if(rc)
  88.                printf("file received\n");
  89.           else
  90.                printf(" error");
  91.  
  92. }
  93.  
  94.  
  95. int getfile ()
  96. {
  97.     int rc,i;
  98.     struct sockaddr_in clientname;
  99.     int                clientnamelength;
  100.     struct in_addr     clientaddr;
  101.     struct sockaddr_in servername;
  102.     char *filename;
  103.  
  104.  
  105.  
  106.     s1 = socket (AF_INET, SOCK_STREAM, 0);
  107.  
  108.     if (s1 != -1)
  109.     {
  110.         printf ("s_send:socket %d opened\n", s1);
  111.  
  112.       memset (&servername, 0, sizeof(servername));
  113.       servername.sin_len         = sizeof(servername);
  114.       servername.sin_family      = AF_INET;
  115.       servername.sin_addr.s_addr = inet_addr(serveraddress);
  116.       servername.sin_port        = myport;
  117.  
  118.  
  119.       /*
  120.        * bind out address.
  121.        */
  122.  
  123.       rc = bind (s1,
  124.                  (struct sockaddr *) &servername,
  125.                  sizeof (servername));
  126.  
  127.       clientnamelength = sizeof(clientname);
  128.  
  129.       if (!rc)
  130.       {
  131.  
  132.         printf ("s_send:server bound to port %d on %s\n",
  133.                 servername.sin_port,
  134.                 serveraddress);
  135.  
  136.         /*
  137.          * set socket to listen for connection requests.
  138.          */
  139.         rc = listen (s1, 5);
  140.  
  141.       if (!rc)
  142.         {
  143.           s2 = accept (s1,
  144.                        (struct sockaddr *)&clientname,
  145.                        &clientnamelength);
  146.  
  147.  
  148.           if (rc != -1)
  149.           {
  150.  
  151.             printf ("s_send:connection accepted on socket %d from %d:%s\n",
  152.                     s2,
  153.                     clientnamelength,
  154.                     inet_ntoa(clientname.sin_addr));
  155.  
  156.             rc = recv(s2,message,sizeof(message),MSG_WAITALL);
  157.                 printf("\n*************** no of bytes recvd =%d \n",rc);
  158.  
  159.               puts(message);
  160.  
  161.             fclose(fd);
  162.             return(rc);
  163.           }
  164.           else
  165.           {
  166.               printf ("s_send:accept() failed rc = %d sock_errno = %d \n", rc, sock_errno());
  167.           }
  168.         }
  169.         else
  170.         {
  171.             printf ("s_send:listen() failed rc = %d sock_errno = %d \n", rc, sock_errno());
  172.         }
  173.       }
  174.       else
  175.       {
  176.           printf ("s_send:bind() failed rc = %d sock_errno = %d \n", rc, sock_errno());
  177.       }
  178.     }
  179.     else
  180.     {
  181.         printf ("s_send:socket() failed rc = %d\n", sock_errno());
  182.     }
  183.  
  184.     return(rc);
  185. }
  186.