1112758Ssam.\"	$NetBSD: udgramread.c,v 1.2 1998/01/09 06:55:06 perry Exp $
2112758Ssam.\"
3112758Ssam.\" Copyright (c) 1986, 1993
4112758Ssam.\"	The Regents of the University of California.  All rights reserved.
5112758Ssam.\"
6112758Ssam.\" Redistribution and use in source and binary forms, with or without
7112758Ssam.\" modification, are permitted provided that the following conditions
8112758Ssam.\" are met:
9112758Ssam.\" 1. Redistributions of source code must retain the above copyright
10112758Ssam.\"    notice, this list of conditions and the following disclaimer.
11112758Ssam.\" 2. Redistributions in binary form must reproduce the above copyright
12112758Ssam.\"    notice, this list of conditions and the following disclaimer in the
13112758Ssam.\"    documentation and/or other materials provided with the distribution.
14112758Ssam.\" 3. Neither the name of the University nor the names of its contributors
15112758Ssam.\"    may be used to endorse or promote products derived from this software
16112758Ssam.\"    without specific prior written permission.
17112758Ssam.\"
18112758Ssam.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19112758Ssam.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20112758Ssam.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21112758Ssam.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22112758Ssam.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23112758Ssam.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24112758Ssam.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25112758Ssam.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26112758Ssam.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27112758Ssam.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28105197Ssam.\" SUCH DAMAGE.
29105197Ssam.\"
30105197Ssam.\"	@(#)udgramread.c	8.1 (Berkeley) 6/8/93
31105197Ssam.\"
32105197Ssam#include <sys/types.h>
33105197Ssam#include <sys/socket.h>
34105197Ssam#include <sys/un.h>
35159965Sthompsa
36105197Ssam/*
37105197Ssam * In the included file <sys/un.h> a sockaddr_un is defined as follows
38105197Ssam * struct sockaddr_un {
39105197Ssam *	short	sun_family;
40105197Ssam *	char	sun_path[108];
41105197Ssam * };
42105197Ssam */
43105197Ssam
44105197Ssam#include <stdio.h>
45105197Ssam
46105197Ssam#define NAME "socket"
47171497Sbz
48105197Ssam/*
49105197Ssam * This program creates a UNIX domain datagram socket, binds a name to it,
50105197Ssam * then reads from the socket.
51105197Ssam */
52105197Ssammain()
53105197Ssam{
54105197Ssam	int sock, length;
55105197Ssam	struct sockaddr_un name;
56105197Ssam	char buf[1024];
57105197Ssam
58105197Ssam	/* Create socket from which to read. */
59105197Ssam	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
60105197Ssam	if (sock < 0) {
61105197Ssam		perror("opening datagram socket");
62105197Ssam		exit(1);
63105197Ssam	}
64105197Ssam	/* Create name. */
65105197Ssam	name.sun_family = AF_UNIX;
66105197Ssam	strcpy(name.sun_path, NAME);
67105197Ssam	if (bind(sock, &name, sizeof(struct sockaddr_un))) {
68105197Ssam		perror("binding name to datagram socket");
69105197Ssam		exit(1);
70105197Ssam	}
71105197Ssam	printf("socket -->%s\en", NAME);
72105197Ssam	/* Read from the socket */
73105197Ssam	if (read(sock, buf, 1024) < 0)
74105197Ssam		perror("receiving datagram packet");
75105197Ssam	printf("-->%s\en", buf);
76105197Ssam	close(sock);
77105197Ssam	unlink(NAME);
78105197Ssam}
79105197Ssam