1*27700Smckusick .\" Copyright (c) 1986 Regents of the University of California. 2*27700Smckusick .\" All rights reserved. The Berkeley software License Agreement 3*27700Smckusick .\" specifies the terms and conditions for redistribution. 4*27700Smckusick .\" 5*27700Smckusick .\" @(#)dgramsend.c 6.1 (Berkeley) 05/04/86 6*27700Smckusick .\" 7*27700Smckusick #include <sys/types.h> 8*27700Smckusick #include <sys/socket.h> 9*27700Smckusick #include <netinet/in.h> 10*27700Smckusick #include <netdb.h> 11*27700Smckusick #include <stdio.h> 12*27700Smckusick 13*27700Smckusick #define DATA "The sea is calm tonight, the tide is full . . ." 14*27700Smckusick 15*27700Smckusick /* 16*27700Smckusick * Here I send a datagram to a receiver whose name I get from the command 17*27700Smckusick * line arguments. The form of the command line is dgramsend hostname 18*27700Smckusick * portnumber 19*27700Smckusick */ 20*27700Smckusick 21*27700Smckusick main(argc, argv) 22*27700Smckusick int argc; 23*27700Smckusick char *argv[]; 24*27700Smckusick { 25*27700Smckusick int sock; 26*27700Smckusick struct sockaddr_in name; 27*27700Smckusick struct hostent *hp, *gethostbyname(); 28*27700Smckusick 29*27700Smckusick /* Create socket on which to send. */ 30*27700Smckusick sock = socket(AF_INET, SOCK_DGRAM, 0); 31*27700Smckusick if (sock < 0) { 32*27700Smckusick perror("opening datagram socket"); 33*27700Smckusick exit(-1); 34*27700Smckusick } 35*27700Smckusick /* 36*27700Smckusick * Construct name, with no wildcards, of the socket to send to. 37*27700Smckusick * Getnostbyname() returns a structure including the network address 38*27700Smckusick * of the specified host. The port number is taken from the command 39*27700Smckusick * line. 40*27700Smckusick */ 41*27700Smckusick hp = gethostbyname(argv[1]); 42*27700Smckusick bcopy(hp->h_addr, &(name.sin_addr.s_addr), hp->h_length); 43*27700Smckusick name.sin_family = AF_INET; 44*27700Smckusick name.sin_port = htons(atoi(argv[2])); 45*27700Smckusick /* Send message. */ 46*27700Smckusick if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0) { 47*27700Smckusick perror("sending datagram message"); 48*27700Smckusick } 49*27700Smckusick close(sock); 50*27700Smckusick } 51