11638Srgrimes.\" Copyright (c) 1986, 1993 21638Srgrimes.\" The Regents of the University of California. All rights reserved. 31638Srgrimes.\" 41638Srgrimes.\" Redistribution and use in source and binary forms, with or without 51638Srgrimes.\" modification, are permitted provided that the following conditions 61638Srgrimes.\" are met: 71638Srgrimes.\" 1. Redistributions of source code must retain the above copyright 81638Srgrimes.\" notice, this list of conditions and the following disclaimer. 91638Srgrimes.\" 2. Redistributions in binary form must reproduce the above copyright 101638Srgrimes.\" notice, this list of conditions and the following disclaimer in the 111638Srgrimes.\" documentation and/or other materials provided with the distribution. 121638Srgrimes.\" 3. All advertising materials mentioning features or use of this software 131638Srgrimes.\" must display the following acknowledgement: 141638Srgrimes.\" This product includes software developed by the University of 151638Srgrimes.\" California, Berkeley and its contributors. 161638Srgrimes.\" 4. Neither the name of the University nor the names of its contributors 171638Srgrimes.\" may be used to endorse or promote products derived from this software 181638Srgrimes.\" without specific prior written permission. 191638Srgrimes.\" 201638Srgrimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211638Srgrimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221638Srgrimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231638Srgrimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241638Srgrimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251638Srgrimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261638Srgrimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271638Srgrimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281638Srgrimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291638Srgrimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301638Srgrimes.\" SUCH DAMAGE. 311638Srgrimes.\" 321638Srgrimes.\" @(#)dgramsend.c 8.1 (Berkeley) 6/8/93 331638Srgrimes.\" 341638Srgrimes#include <sys/types.h> 351638Srgrimes#include <sys/socket.h> 361638Srgrimes#include <netinet/in.h> 371638Srgrimes#include <netdb.h> 381638Srgrimes#include <stdio.h> 391638Srgrimes 401638Srgrimes#define DATA "The sea is calm tonight, the tide is full . . ." 411638Srgrimes 421638Srgrimes/* 431638Srgrimes * Here I send a datagram to a receiver whose name I get from the command 441638Srgrimes * line arguments. The form of the command line is dgramsend hostname 458875Srgrimes * portnumber 461638Srgrimes */ 471638Srgrimes 481638Srgrimesmain(argc, argv) 491638Srgrimes int argc; 501638Srgrimes char *argv[]; 511638Srgrimes{ 521638Srgrimes int sock; 531638Srgrimes struct sockaddr_in name; 541638Srgrimes struct hostent *hp, *gethostbyname(); 551638Srgrimes 561638Srgrimes /* Create socket on which to send. */ 571638Srgrimes sock = socket(AF_INET, SOCK_DGRAM, 0); 581638Srgrimes if (sock < 0) { 591638Srgrimes perror("opening datagram socket"); 601638Srgrimes exit(1); 611638Srgrimes } 621638Srgrimes /* 631638Srgrimes * Construct name, with no wildcards, of the socket to send to. 6430849Sjraynard * Gethostbyname() returns a structure including the network address 651638Srgrimes * of the specified host. The port number is taken from the command 668875Srgrimes * line. 671638Srgrimes */ 681638Srgrimes hp = gethostbyname(argv[1]); 691638Srgrimes if (hp == 0) { 7034211Sjraynard fprintf(stderr, "%s: unknown host\en", argv[1]); 711638Srgrimes exit(2); 721638Srgrimes } 731638Srgrimes bcopy(hp->h_addr, &name.sin_addr, hp->h_length); 741638Srgrimes name.sin_family = AF_INET; 751638Srgrimes name.sin_port = htons(atoi(argv[2])); 761638Srgrimes /* Send message. */ 771638Srgrimes if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0) 781638Srgrimes perror("sending datagram message"); 791638Srgrimes close(sock); 801638Srgrimes} 81