dgMain.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:6k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* dgMain.c - UNIX main for datagram test sender/receiver demo */
  2. /*
  3. modification history
  4. --------------------
  5. 01d,31oct91,rrr  passed through the ansification filter
  6.   -changed includes to have absolute path from h/
  7.   -fixed #else and #endif
  8.   -changed VOID to void
  9.   -changed copyright notice
  10. 01c,15oct91,ajm  made mips look to bsd includes
  11. 01b,25oct90,lpf  #ifdef <sys/socket.h>.
  12.  put back bzero() for HP.
  13. 01a,10jun88,gae  written based on rdc's test programs.
  14. */
  15. /*
  16. DESCRIPTION
  17. This demonstration module shows how to send and receive UDP packets
  18. from VxWorks to UNIX and any combination thereof.
  19. Firstly, a receiver, or receivers, are initiated on VxWorks and/or
  20. UNIX.  It will wait for packets on an agreed port number.
  21. Then a sender, again on either UNIX or VxWorks, begins sending packets.
  22. The receiver will terminate after an agreed number of packets are sent.
  23. EXAMPLE
  24.     % dgReciver -v
  25.     % dgSender -h localhost -v
  26.     ... output from dgReceiver
  27. OPTIONS
  28.     -v verbose flag causes extra diagnostics to be printed.
  29.     -b broadcast flag causes dgSender to broadcast the packets.
  30.     -h <host> host name flag indicates the network to send on.
  31.     -p <port> selects the port number (default is 1101).
  32.     -n <#> is the number of packets to send (default 5).
  33.     -help shows the options.
  34. */
  35. #if 0
  36. #include "vxWorks.h"
  37. #include "sys/types.h"
  38. #include "netdb.h"
  39. #include "inetLib.h"
  40. #include "in.h"
  41. #else
  42. #include <sys/types.h>
  43. #include <netdb.h>
  44. #include <netinet/in.h>
  45. #define IMPORT extern
  46. #define LOCAL static
  47. #define BOOL int
  48. #define ERROR -1
  49. #define NULL 0
  50. #endif
  51. #if !defined(HOST_HP)
  52. #include <sys/socket.h> /* Do not include this file for HP/UX 7.0 */
  53. #endif
  54. #if !defined(HOST_MIPS)
  55. #include <sys/ioctl.h>
  56. #else
  57. #include <bsd43/sys/ioctl.h>
  58. #endif
  59. #define TORF(x) ((x) ? "True" : "False")
  60. #define PACKET_NUM 500 /* maximum number of packets */
  61. IMPORT BOOL broadcast; /* when TRUE dgSender will broadcast */
  62. IMPORT BOOL verbose; /* when TRUE extra diagnostics are displayed */
  63. IMPORT int dgPort;
  64. IMPORT int dgPackets;
  65. IMPORT BOOL is42; /* SUNOS 3.4 is not completely 4.3 compatible */
  66. #ifdef POLL
  67. IMPORT int fionread; /* FIONREAD value (different from VxWorks) */
  68. #endif
  69. IMPORT int errno;
  70. char sysBootHost [40]; /* VxWorks boot host name / our host name */
  71. /* UNIX program must be called either: */
  72. #define SENDNAME "dgSender"
  73. #define RECVNAME "dgReceiver"
  74. LOCAL char *toolname;
  75. LOCAL char *dgSendUsage =
  76.     "usage: %s [-help] [-b] [-h host] [-p port] [-n packets] [-v]n";
  77. LOCAL char *dgRecvUsage =
  78.     "usage: %s [-help] [-p port] [-n packets] [-v]n";
  79. LOCAL char *dgSendHelp =
  80. "show defaultsn
  81. -b    broadcast         = %sn
  82. -h    host              = %sn
  83. -p    port number       = %dn
  84. -n    number of packets = %dn
  85. -v    verbose           = %sn";
  86. LOCAL char *dgRecvHelp =
  87. "show defaultsn
  88. -p    port number       = %dn
  89. -n    number of packets = %dn
  90. -v    verbose           = %sn";
  91. #if     defined(HOST_HP)
  92. void bzero (s, n)
  93.     char *s;
  94.     int n;
  95.     {
  96.     memset (s, '', n);
  97.     }
  98. #endif
  99. /*******************************************************************************
  100. *
  101. * main - send/recieve UDP datagrams
  102. *
  103. * -help show defaults
  104. * -b  (broadcast [send only])
  105. * -h host (broadcast [send only])
  106. * -n port (1101)
  107. * -p packets (5)
  108. * -v            (verbose)
  109. */
  110. void main (argc, argv)
  111.     int argc;
  112.     char *argv [];
  113.     {
  114.     char **argp = argv;
  115.     BOOL send;
  116.     toolname  = *argp;
  117.     send = strcmp (SENDNAME, toolname) == 0;
  118.     gethostname (sysBootHost, sizeof (sysBootHost));
  119.     while (--argc > 0)
  120. {
  121. argp++;
  122. switch (argp [0][0])
  123.     {
  124.     case '-':
  125. if (strcmp (*argp, "-help") == 0)
  126.     {
  127.     if (send)
  128. printf (dgSendHelp, TORF(broadcast), sysBootHost,
  129. dgPort, dgPackets, TORF(verbose));
  130.     else
  131. printf (dgRecvHelp,
  132. dgPort, dgPackets, TORF(verbose));
  133.     exit (0);
  134.     }
  135. switch (argp [0][1])
  136.     {
  137.     case 'b':
  138. if (!send)
  139.     goto l_usage;
  140. broadcast = !broadcast;
  141. break;
  142.     case 'h':
  143. if (!send)
  144.     goto l_usage;
  145. if (argc-- > 0)
  146.     strcpy (sysBootHost, *++argp);
  147. else
  148.     {
  149.     printf ("%s: missing <%s>n", toolname, "host");
  150.     goto l_usage;
  151.     }
  152. break;
  153.     case 'n':
  154. if (argc-- > 0)
  155.     {
  156.     dgPackets = atoi (*++argp);
  157.     if (dgPackets > PACKET_NUM)
  158. {
  159. printf ("%s: too many packets <%d>n",
  160. toolname, dgPackets);
  161. goto l_usage;
  162. }
  163.     }
  164. else
  165.     {
  166.     printf ("%s: missing <%s>n", toolname, "packet");
  167.     goto l_usage;
  168.     }
  169. break;
  170.     case 'p':
  171. if (argc-- > 0)
  172.     dgPort = atoi (*++argp);
  173. else
  174.     {
  175.     printf ("%s: missing <%s>n", toolname, "port");
  176.     goto l_usage;
  177.     }
  178. break;
  179.     case 'v':
  180. verbose = !verbose;
  181. break;
  182.     default:
  183. printf ("%s: bad flag <%s>n", toolname, *argp);
  184. goto l_usage;
  185.     }
  186. break;
  187.     default:
  188. printf ("%s: no such option <%s>n", toolname, *argp);
  189. goto l_usage;
  190.     }
  191. }
  192.     if (argc > 0)
  193. {
  194. printf ("%s: no such option <%s> ...n", toolname, *argp);
  195. goto l_usage;
  196. }
  197. #ifdef POLL
  198.     fionread = FIONREAD; /* VxWorks differs */
  199. #endif
  200. #ifndef SO_BROADCAST
  201.     /* BSD 4.2 doesn't have or require turning on of broadcasting */
  202.     is42 = TRUE;
  203. #endif /* SO_BROADCAST */
  204.     if (send)
  205. dgSender (sysBootHost, dgPort, dgPackets);
  206.     else
  207. dgReceiver (dgPort, dgPackets);
  208.     exit (0);
  209. l_usage:
  210.     printf ((send ? dgSendUsage : dgRecvUsage), toolname);
  211.     exit (0);
  212.     }
  213. /*******************************************************************************
  214. *
  215. * hostGetByName - VxWorks compatible function to return inet address as long
  216. */
  217. long hostGetByName (hostname)
  218.     char *hostname;
  219.     {
  220.     struct hostent *destHost;
  221.     /* get the internet address for the given host */
  222.     if ((destHost = (struct hostent *) gethostbyname (hostname)) == NULL)
  223. {
  224. printf ("%s:non existant host <%s>n", toolname, hostname);
  225. return ((u_long)ERROR);
  226. }
  227.     return (*(u_long *)destHost->h_addr);
  228.     }
  229. /*******************************************************************************
  230. *
  231. * errnoGet - VxWorks compatible function to return errno
  232. */
  233. int errnoGet ()
  234.     {
  235.     return (errno);
  236.     }