/* sender for internet datagram demo */ /* modification 9/20/93 with Louis Sanchez's bug fix */ #include #include #include #include #include /************************************************************ printsockaddr prints the contents of a sockaddr_in structure when called with a pointer to such a structure. ************************************************************/ void printsockaddr(struct sockaddr_in *arg) { struct hostent *hp; printf(" sockaddr_in: "); printf("Domain=%d, ", arg->sin_family); hp=gethostbyaddr(&(arg->sin_addr), sizeof(arg->sin_addr), AF_INET); printf("Hostname=%s, ", hp->h_name); printf("Port=%d, ", ntohs(arg->sin_port)); printf("Address=%s,\n", inet_ntoa(arg->sin_addr)); } help_msg() { printf("format: idgs3 -[dn] hostname portno\n"); printf(" option: n set the number of messages sent\n"); printf(" d set debug mode\n"); } main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in to; struct sockaddr_in from; struct hostent *hp, *gethostbyname(); int i; int n; int noOfMsgs; char toBuf[1024]; char fromBuf[1024]; char hostname[64]; extern char *optarg; extern int optind; int option_exist = 0; int c; int debug = 0; char *rcv_name; /* receiver's socket name */ int toPort_no; int length; int flag; /* process command line args */ while ((c=getopt(argc,argv,"dn:")) != EOF) { option_exist = 1; switch(c) { case 'd': debug = 1; if (debug) printf("turn on debug mode.\n"); break; case 'n': if ((noOfMsgs = atoi(optarg)) < 1) noOfMsgs = 1; if (debug) printf("The number of messags to be sent = %d.\n", noOfMsgs); break; case '?': help_msg(); exit (1); default: fprintf (stderr, "unrecognized arg >%s<\n", optarg); help_msg(); exit (1); } } rcv_name = argv[optind++]; if ((toPort_no = atoi(argv[optind])) < 1025) toPort_no = 1025; if (debug) printf("The receiver's host name is %s, port_no=%d\n", rcv_name, toPort_no); /* create socket from which to read */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* create name structure with wildcard using INADDR_ANY */ from.sin_family = AF_INET; from.sin_addr.s_addr = INADDR_ANY; from.sin_port = 0; /* port no. < 1024 IPPORT_RESERVED is for privillege process */ /* here just initialized it, it will be assigned value after call */ /* getsockname() */ if (bind(sock, &from, sizeof(struct sockaddr_in))) { perror("binding name to datagram socket"); exit(1); } length = sizeof(from); if (getsockname(sock, &from, &length)) { perror("getting socket name"); exit(1); } /* Find assigned port value and print out. */ printf("socket has port #%d\n", ntohs(from.sin_port)); /* construct name of the socket to send to * gethostbyname() returns a structure including the network address * of the specified host from /etc/hosts. The port number is taken from * the cmd line * create name structure for the UNIX domain socket * use the name from cmd argument */ hp = gethostbyname(rcv_name); if (hp == 0) { fprintf(stderr, "%s: unknown host",rcv_name); exit(2); } bcopy(hp->h_addr, &to.sin_addr, hp->h_length); to.sin_family = AF_INET; to.sin_port = htons(toPort_no); /*send message */ for (i=1; i<=noOfMsgs; i++) { sprintf(toBuf, "packet %d\n", i); if (sendto(sock, toBuf, strlen(toBuf), 0, &to, sizeof(to)) < 0) perror("sending datagram message"); n = recv(sock, fromBuf, 1024, flag); if (n < 0) perror("receiving datagram message"); fromBuf[n] = 0; /* null terminate */ printf("received %d bytes, msg=%s\n", n, fromBuf); } close(sock); }