.\" Copyright (c
) 1986 Regents of the University of California
.
.\" All rights reserved
. The Berkeley software License Agreement
.\" specifies the terms
and conditions
for redistribution
.
.\" @
(#)dgramread.c 6.2 (Berkeley) %G%
* In the included file <netinet/in.h> a sockaddr_in is defined as follows:
* struct in_addr sin_addr;
* This program creates a datagram socket, binds a name to it, then reads
/* Create socket from which to read. */
sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
perror("opening datagram socket");
/* Create name with wildcards. */
name
.sin_family
= AF_INET
;
name
.sin_addr
.s_addr
= INADDR_ANY
;
if (bind(sock
, &name
, sizeof(name
))) {
perror("binding datagram socket");
/* Find assigned port value and print it out. */
if (getsockname(sock
, &name
, &length
)) {
perror("getting socket name");
printf("Socket has port #%d\en", ntohs(name
.sin_port
));
/* Read from the socket */
if (read(sock
, buf
, 1024, 0) < 0)
perror("receiving datagram packet");