#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
Include dependency graph for udpClient.c:
Go to the source code of this file.
Defines | |
#define | REMOTE_SERVER_PORT 8 |
#define | MAX_MSG 100 |
Functions | |
int | main (int argc, char *argv[]) |
|
Definition at line 22 of file udpClient.c. |
|
Definition at line 21 of file udpClient.c. Referenced by main(). |
|
Definition at line 25 of file udpClient.c. References AF_INET, gethostbyname(), htonl, htons, INADDR_ANY, inet_ntoa(), NULL, REMOTE_SERVER_PORT, in_addr::s_addr, sockaddr_in::sin_addr, sockaddr_in::sin_family, sockaddr_in::sin_port, and SOCK_DGRAM.
00025 { 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 } |
Here is the call graph for this function: