Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 (Tek) 9/26/83; site hammer.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!vax135!cornell!uw-beaver!tektronix!orca!hammer!steveh From: steveh@hammer.UUCP Newsgroups: net.unix-wizards Subject: Test for Socket Problems Message-ID: <905@hammer.UUCP> Date: Tue, 4-Sep-84 15:07:28 EDT Article-I.D.: hammer.905 Posted: Tue Sep 4 15:07:28 1984 Date-Received: Thu, 6-Sep-84 04:27:39 EDT Organization: Tektronix, Wilsonville OR Lines: 58 The following is sample program using Unix Domain sockets for all those people who still can't figure out how to use them. Some notes: o As distributed the include file /usr/include/sys/un.h is wrong. The max path length is 108 not 110 (because it gets padded to 4 byte boundary). o The from address, returned from accept, means nothing in Unix domain. o Casts to sockaddr are not strictly necessary, but keep lint happy. Here is the program: ----- #include #include #include #define SOCKET "/tmp/socktest" struct sockaddr_un sun = { AF_UNIX, SOCKET }; main () { int s, m; struct sockaddr from; int fromlen = sizeof (from); unlink (SOCKET); if (fork () == 0) { sleep (10); if( (s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) perror ("child socket"); if (connect (s, (struct sockaddr *) &sun, sizeof (sun)) < 0) perror ("connect"); printf ("child done\n"); } else { if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) perror ("socket"); if (bind (s, (struct sockaddr *) &sun, sizeof (sun)) < 0) perror ("bind"); if (listen (s, 1) < 0) perror ("listen"); m = 1 << s; if (select (20, &m, 0, 0, 0) < 0) perror ("select"); if (accept (s, &from, &fromlen) < 0) perror ("accept"); printf ("parent done\n"); close(s); unlink(SOCKET); } exit (0); }