/************************************************************************/ /* */ /* This is a simple client that use UDP in order to transmit data */ /* */ /* */ /* */ /************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <sys/socket.h> #include <sys/wait.h> #define MYPORT 9999 /* the port users will be sending to */ int main(int argc, char *argv[]) { /************************/ /* Definitions */ /************************/ int sockfd; struct sockaddr_in their_addr; /*Server address information */ struct hostent *he; /*Struct for name resolving */ int numbytes; /*Number of bytes that were sent*/ int I; /*Index */ if (argc != 3) { fprintf(stderr,"usage: %s hostname message\n",argv[0]); exit(1); } /*--------------------------*/ /*Communication preparation */ /*--------------------------*/ if ((he=gethostbyname(argv[1])) == NULL) { /*Transform host name into address */ herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { /*Socket definition */ perror("socket"); exit(1); } their_addr.sin_family = AF_INET; /*Scoket address family */ their_addr.sin_port = htons(MYPORT); /*Port in network byte order */ their_addr.sin_addr = *((struct in_addr *)he->h_addr); /*Host address */ bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ /*----------------------*/ /*Communication process */ /*----------------------*/ for (I=1;I<100;I++){ printf("Now sending packet number %d \n",I); if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0, \ (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } } printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr)); close(sockfd); return 0; }