127700Smckusick .\" Copyright (c) 1986 Regents of the University of California. 227700Smckusick .\" All rights reserved. The Berkeley software License Agreement 327700Smckusick .\" specifies the terms and conditions for redistribution. 427700Smckusick .\" 5*27883Skarels .\" @(#)dgramsend.c 6.2 (Berkeley) 05/08/86 627700Smckusick .\" 727700Smckusick #include <sys/types.h> 827700Smckusick #include <sys/socket.h> 927700Smckusick #include <netinet/in.h> 1027700Smckusick #include <netdb.h> 1127700Smckusick #include <stdio.h> 1227700Smckusick 1327700Smckusick #define DATA "The sea is calm tonight, the tide is full . . ." 1427700Smckusick 1527700Smckusick /* 1627700Smckusick * Here I send a datagram to a receiver whose name I get from the command 1727700Smckusick * line arguments. The form of the command line is dgramsend hostname 1827700Smckusick * portnumber 1927700Smckusick */ 2027700Smckusick 2127700Smckusick main(argc, argv) 22*27883Skarels int argc; 23*27883Skarels char *argv[]; 2427700Smckusick { 25*27883Skarels int sock; 2627700Smckusick struct sockaddr_in name; 2727700Smckusick struct hostent *hp, *gethostbyname(); 2827700Smckusick 2927700Smckusick /* Create socket on which to send. */ 3027700Smckusick sock = socket(AF_INET, SOCK_DGRAM, 0); 3127700Smckusick if (sock < 0) { 3227700Smckusick perror("opening datagram socket"); 33*27883Skarels exit(1); 3427700Smckusick } 3527700Smckusick /* 3627700Smckusick * Construct name, with no wildcards, of the socket to send to. 3727700Smckusick * Getnostbyname() returns a structure including the network address 3827700Smckusick * of the specified host. The port number is taken from the command 3927700Smckusick * line. 4027700Smckusick */ 4127700Smckusick hp = gethostbyname(argv[1]); 42*27883Skarels if (hp == 0) { 43*27883Skarels fprintf(stderr, "%s: unknown host\n", argv[1]); 44*27883Skarels exit(2); 45*27883Skarels } 46*27883Skarels bcopy(hp->h_addr, &name.sin_addr, hp->h_length); 4727700Smckusick name.sin_family = AF_INET; 4827700Smckusick name.sin_port = htons(atoi(argv[2])); 4927700Smckusick /* Send message. */ 50*27883Skarels if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0) 5127700Smckusick perror("sending datagram message"); 5227700Smckusick close(sock); 5327700Smckusick } 54