dgramsend.c revision 1639
1151497Sru.\" Copyright (c) 1986, 1993 2151497Sru.\" The Regents of the University of California. All rights reserved. 3151497Sru.\" 4151497Sru.\" Redistribution and use in source and binary forms, with or without 5151497Sru.\" modification, are permitted provided that the following conditions 6151497Sru.\" are met: 7151497Sru.\" 1. Redistributions of source code must retain the above copyright 8151497Sru.\" notice, this list of conditions and the following disclaimer. 9151497Sru.\" 2. Redistributions in binary form must reproduce the above copyright 10151497Sru.\" notice, this list of conditions and the following disclaimer in the 11151497Sru.\" documentation and/or other materials provided with the distribution. 12151497Sru.\" 3. All advertising materials mentioning features or use of this software 13151497Sru.\" must display the following acknowledgement: 14151497Sru.\" This product includes software developed by the University of 15151497Sru.\" California, Berkeley and its contributors. 16151497Sru.\" 4. Neither the name of the University nor the names of its contributors 17151497Sru.\" may be used to endorse or promote products derived from this software 18151497Sru.\" without specific prior written permission. 19151497Sru.\" 20151497Sru.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21151497Sru.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22151497Sru.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23151497Sru.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24151497Sru.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25151497Sru.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26151497Sru.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27151497Sru.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28151497Sru.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29151497Sru.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30151497Sru.\" SUCH DAMAGE. 31151497Sru.\" 32151497Sru.\" @(#)dgramsend.c 8.1 (Berkeley) 6/8/93 33151497Sru.\" 34151497Sru#include <sys/types.h> 35151497Sru#include <sys/socket.h> 36151497Sru#include <netinet/in.h> 37151497Sru#include <netdb.h> 38151497Sru#include <stdio.h> 39151497Sru 40151497Sru#define DATA "The sea is calm tonight, the tide is full . . ." 41151497Sru 42151497Sru/* 43151497Sru * Here I send a datagram to a receiver whose name I get from the command 44151497Sru * line arguments. The form of the command line is dgramsend hostname 45151497Sru * portnumber 46151497Sru */ 47151497Sru 48151497Srumain(argc, argv) 49151497Sru int argc; 50151497Sru char *argv[]; 51151497Sru{ 52151497Sru int sock; 53151497Sru struct sockaddr_in name; 54151497Sru struct hostent *hp, *gethostbyname(); 55151497Sru 56151497Sru /* Create socket on which to send. */ 57151497Sru sock = socket(AF_INET, SOCK_DGRAM, 0); 58151497Sru if (sock < 0) { 59151497Sru perror("opening datagram socket"); 60151497Sru exit(1); 61151497Sru } 62151497Sru /* 63151497Sru * Construct name, with no wildcards, of the socket to send to. 64151497Sru * Getnostbyname() returns a structure including the network address 65151497Sru * of the specified host. The port number is taken from the command 66151497Sru * line. 67151497Sru */ 68151497Sru hp = gethostbyname(argv[1]); 69151497Sru if (hp == 0) { 70151497Sru fprintf(stderr, "%s: unknown host\n", argv[1]); 71151497Sru exit(2); 72151497Sru } 73151497Sru bcopy(hp->h_addr, &name.sin_addr, hp->h_length); 74151497Sru name.sin_family = AF_INET; 75151497Sru name.sin_port = htons(atoi(argv[2])); 76151497Sru /* Send message. */ 77151497Sru if (sendto(sock, DATA, sizeof(DATA), 0, &name, sizeof(name)) < 0) 78151497Sru perror("sending datagram message"); 79151497Sru close(sock); 80151497Sru} 81151497Sru