home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / viii-31 / sendnote.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  7KB  |  271 lines

  1. ;/* sendnote.c - Execute to compile with SAS/C 6.56
  2. sc DATA=FAR NMINC STRMERGE STREQ NOSTKCHK SAVEDS IGNORE=73 sendnote.c
  3. slink FROM LIB:c.o sendnote.o TO sendnote LIBRARY LIB:sc.lib LIB:amiga.lib
  4. quit
  5. */
  6.  
  7. /* (c)  Copyright 1992-93 Commodore-Amiga, Inc.   All rights reserved. */
  8. /* The information contained herein is subject to change without    */
  9. /* notice, and is provided "as is" without warranty of any kind,    */
  10. /* either expressed or implied.  The entire risk as to the use of   */
  11. /* this information is assumed by the user.                         */
  12.  
  13. /*
  14. ** Our Application Prototypes (specific to notes.c file)
  15. */
  16.  
  17. void    main( int, char ** );
  18. void    FinalExit( int );
  19.  
  20. /*
  21. ** Application-specific defines and globals
  22. */
  23.  
  24. char Version[] = "\0$VER: SendNote 1.2 (1.12.91)";
  25.  
  26. #define TEMPLATE    "Host/A,Text,Button"
  27. #define OPT_HOST    0
  28. #define OPT_TEXT    1
  29. #define OPT_BUTTON  2
  30. #define OPT_COUNT   3
  31.  
  32. /*
  33. ** The library bases...we need em later...
  34. */
  35.  
  36. struct Library *IntuitionBase, *SockBase;
  37.  
  38. /*
  39. ** All other includes and protos are indexed off our catch-all file
  40. ** note.h which both the client (sendnote.c) and server (shownote.c)
  41. ** include.
  42. */
  43.  
  44. #include    "note.h"
  45.  
  46.  
  47. void    main(int argc, char **argv)
  48. {
  49.     struct RDArgs *rdargs;      /* ReadArgs() return information */
  50.  
  51.     struct sockaddr_in serv;    /* Server's Internet Address */
  52.  
  53.     struct hostent *host;       /* The located host info */
  54.  
  55.     struct NetNote out;         /* Message packet for send/recv */
  56.  
  57.     long opts[OPT_COUNT] =  {
  58.                             0L,
  59.                             (long)"== PING! ==",
  60.                             (long)"OK"
  61.                             };
  62.  
  63.     int sock;                   /* The working socket */
  64.  
  65.     char *hostnam,                  /* Arg of hostname */
  66.          *text,                 /* Arg of text to be sent */
  67.          *button;               /* Arg of button text */
  68.  
  69.     /*
  70.     ** Process arguments using new (2.0) dos calls.
  71.     */
  72.  
  73.     rdargs = (struct RDArgs *)ReadArgs( (UBYTE *)TEMPLATE, opts, NULL );
  74.     if(rdargs == NULL)
  75.     {
  76.         puts("Command line not accepted!");
  77.         FinalExit( RETURN_ERROR );
  78.     }
  79.  
  80.     hostnam = (char *)opts[OPT_HOST];
  81.     text = (char *)opts[OPT_TEXT];
  82.     button  = (char *)opts[OPT_BUTTON];
  83.  
  84.     /*
  85.     **  Open socket.library and intialize socket space
  86.     */
  87.  
  88.     if (SockBase = OpenLibrary("inet:libs/socket.library", 0L))
  89.     {
  90.         setup_sockets( 1, &errno );
  91.     }
  92.     else
  93.     {
  94.         puts("Can't open socket.library!");
  95.         FinalExit( RETURN_ERROR );
  96.     }
  97.  
  98.     /*
  99.     ** First we need to try and resolve the host machine as a
  100.     ** normal IP/Internet address.  If that fails, fall back to seaching
  101.     ** the hosts file for it.  Before anything, we need to clear out
  102.     ** the buffer (serv) where the information will be placed, using
  103.     ** the bzero() call (actually a macro in sys/types.h).
  104.     */
  105.  
  106.     bzero( &serv, sizeof(struct sockaddr_in) );
  107.     if ( (serv.sin_addr.s_addr = inet_addr(hostnam)) == INADDR_NONE )
  108.     {
  109.         /*
  110.         ** Okay, the program wasnt handed a dotted decimal address,
  111.         ** so we check and see if it was handed a machine name.
  112.         **
  113.         ** NOTE:  Grab the information you need before you use the
  114.         **        gethostbyname() call again.  Subsequent calls
  115.         **        will overwrite the buffer it hands back.
  116.         */
  117.  
  118.         if ( (host = gethostbyname(hostnam)) == NULL )
  119.         {
  120.             printf("Host not found: %s\n",host);
  121.             FinalExit( RETURN_ERROR );
  122.         }
  123.  
  124.         /*
  125.         ** It does indeed have a name, so copy the addr field from the
  126.         ** hostent structure into the sockaddr structure.
  127.         */
  128.  
  129.         bcopy( host->h_addr, (char *)&serv.sin_addr, host->h_length );
  130.     }
  131.  
  132. /*
  133. **  Following is commented out for ease of testing purposes!  Normally, apps
  134. **  should obtain server numbers using this type of code and a matching entry
  135. **  in the inet:db/services file.
  136. **
  137. **  {
  138. **      struct servent *servptr;
  139. **      char *servnam = APPNAME;
  140. **
  141. **      *
  142. **      ** Open the INET:DB/SERVICES file and locate the server info
  143. **      ** by matching the name and service.
  144. **      *
  145. **
  146. **      if ((servptr = getservbyname( servnam, "tcp" )) == NULL)
  147. **      {
  148. **          printf("%s not in inet:db/services list!",servnam);
  149. **          FinalExit( RETURN_ERROR );
  150. **      }
  151. **      serv.sin_port = servptr->s_port;
  152. **  }
  153. **
  154. */
  155.  
  156.     /*
  157.     ** If you used the above code, you would remove this line:
  158.     */
  159.  
  160.     serv.sin_port = 8769;
  161.  
  162.     /*
  163.     ** This tells the system the socket in question is an Internet socket
  164.     */
  165.  
  166.     serv.sin_family = AF_INET;
  167.  
  168.     /*
  169.     ** Initialize the socket
  170.     */
  171.  
  172.     if ( (sock = socket( AF_INET, SOCK_STREAM, 0 )) < 0 )
  173.     {
  174.         printf("socket gen: %s\n", strerror(errno));
  175.         FinalExit( RETURN_ERROR );
  176.     }
  177.  
  178.     /*
  179.     ** Connect the socket to the remote socket, which belongs to the
  180.     ** server, and which will "wake up" the server.
  181.     */
  182.  
  183.     if ( connect( sock,
  184.                   (struct sockaddr *)&serv,
  185.                   sizeof(struct sockaddr) ) < 0 )
  186.     {
  187.         printf("connect: %s\n", strerror(errno));
  188.         s_close( sock );
  189.         FinalExit( RETURN_ERROR );
  190.     }
  191.  
  192.     /*
  193.     ** Compose the message packet for transmission
  194.     */
  195.  
  196.     out.nn_Code = NN_MSG;
  197.     strcpy( (char *)&out.nn_Text, text );
  198.     strcpy( (char *)&out.nn_Button, button );
  199.  
  200.     /*
  201.     ** Send the packet to the remote system
  202.     */
  203.  
  204.     send( sock, (char *)&out, sizeof(struct NetNote), 0 );
  205.  
  206.     printf("\nMessage sent to %s...waiting for answer...\n", hostnam );
  207.  
  208.     /*
  209.     ** Wait for either acknowledge or error.  This is a potential hang
  210.     ** location if the server is mortally wounded.
  211.     */
  212.  
  213.     recv( sock, (char *)&out, sizeof(struct NetNote), 0 );
  214.  
  215.     /*
  216.     ** Evaluate the packet returned to us
  217.     */
  218.  
  219.     if (out.nn_Code == NN_ACK)
  220.     {
  221.         printf("Response:  Button %ld pressed.\n\n", out.nn_Retval );
  222.     }
  223.     else
  224.     {
  225.         puts("Error during message send...please try again later!");
  226.         FinalExit( RETURN_ERROR );
  227.     }
  228.  
  229.     /*
  230.     ** Since ReadArgs() was called inside the main() function, the pointer
  231.     ** to the buffer it created needs to be deallocated inside main().
  232.     */
  233.  
  234.     if (rdargs)
  235.     {
  236.         FreeArgs( rdargs );
  237.     }
  238.  
  239.     FinalExit( RETURN_OK );
  240.  
  241. }
  242.  
  243. /*
  244. **  FinalExit() - Non-returning routine which handles exits and cleanups.
  245. **
  246. **
  247. */
  248.  
  249. void    FinalExit( int retcode )
  250. {
  251.  
  252.     /*
  253.     ** If SockBase is non-null, it means that socket.library was opened,
  254.     ** and a socket environment was initialized.  Remove the environment
  255.     ** and close the library.
  256.     */
  257.  
  258.     if (SockBase)
  259.     {
  260.         cleanup_sockets();
  261.         CloseLibrary(SockBase);
  262.     }
  263.  
  264.     /*
  265.     ** Terminate program, handing return code out to the shell handler
  266.     */
  267.  
  268.     exit( retcode );
  269. }
  270.  
  271.