Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

udpClient.c

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /* This file has been written by Sergio Perez Alcañiz <serpeal@disca.upv.es>  */
00003 /*            Departamento de Informática de Sistemas y Computadores          */
00004 /*            Universidad Politécnica de Valencia                             */
00005 /*            Valencia (Spain)                                                */
00006 /*                                                                            */
00007 /* The RTL-lwIP project has been supported by the Spanish Government Research */
00008 /* Office (CICYT) under grant TIC2002-04123-C03-03                            */
00009 /******************************************************************************/
00010 
00011 #include <sys/types.h>
00012 #include <sys/socket.h>
00013 #include <netinet/in.h>
00014 #include <arpa/inet.h>
00015 #include <netdb.h>
00016 #include <stdio.h>
00017 #include <unistd.h>
00018 #include <string.h> /* memset() */
00019 #include <sys/time.h> /* select() */ 
00020 
00021 #define REMOTE_SERVER_PORT 8
00022 #define MAX_MSG 100
00023 
00024 
00025 int main(int argc, char *argv[]) {
00026   
00027   int sd, rc, i;
00028   struct sockaddr_in cliAddr, remoteServAddr;
00029   struct hostent *h;
00030 
00031   /* check command line args */
00032   if(argc<3) {
00033     printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
00034     exit(1);
00035   }
00036 
00037   /* get server IP address (no check if input is IP address or DNS name */
00038   h = gethostbyname(argv[1]);
00039   if(h==NULL) {
00040     printf("%s: unknown host '%s' \n", argv[0], argv[1]);
00041     exit(1);
00042   }
00043 
00044   printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
00045          inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
00046 
00047   remoteServAddr.sin_family = h->h_addrtype;
00048   memcpy((char *) &remoteServAddr.sin_addr.s_addr, 
00049          h->h_addr_list[0], h->h_length);
00050   remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
00051 
00052   /* socket creation */
00053   sd = socket(AF_INET,SOCK_DGRAM,0);
00054   if(sd<0) {
00055     printf("%s: cannot open socket \n",argv[0]);
00056     exit(1);
00057   }
00058   
00059   /* bind any port */
00060   cliAddr.sin_family = AF_INET;
00061   cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
00062   cliAddr.sin_port = htons(0);
00063   
00064   rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
00065   if(rc<0) {
00066     printf("%s: cannot bind port\n", argv[0]);
00067     exit(1);
00068   }
00069 
00070 
00071   /* send data */
00072   for(i=2;i<argc;i++) {
00073     rc = sendto(sd, argv[i], strlen(argv[i])+1, 0, 
00074                 (struct sockaddr *) &remoteServAddr, 
00075                 sizeof(remoteServAddr));
00076 
00077     if(rc<0) {
00078       printf("%s: cannot send data %d \n",argv[0],i-1);
00079       close(sd);
00080       exit(1);
00081     }
00082 
00083   }
00084   
00085   return 1;
00086 
00087 }

Generated on Wed Jan 14 12:58:57 2004 for RTL-lwIP-0.4 by doxygen 1.3.4