Annotation of 43BSDReno/share/doc/ps1/07.ipctut/socketpair.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1986 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that the above copyright notice and this paragraph are
                      7:  * duplicated in all such forms and that any documentation,
                      8:  * advertising materials, and other materials related to such
                      9:  * distribution and use acknowledge that the software was developed
                     10:  * by the University of California, Berkeley.  The name of the
                     11:  * University may not be used to endorse or promote products derived
                     12:  * from this software without specific prior written permission.
                     13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     15:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     16:  */
                     17: 
                     18: #ifndef lint
                     19: char copyright[] =
                     20: "@(#) Copyright (c) 1986 The Regents of the University of California.\n\
                     21:  All rights reserved.\n";
                     22: #endif /* not lint */
                     23: 
                     24: #ifndef lint
                     25: static char sccsid[] = "@(#)socketpair.c       6.3 (Berkeley) 3/7/89";
                     26: #endif /* not lint */
                     27: 
                     28: #include <sys/types.h>
                     29: #include <sys/socket.h>
                     30: #include <stdio.h>
                     31: 
                     32: #define DATA1 "In Xanadu, did Kublai Khan . . ."
                     33: #define DATA2 "A stately pleasure dome decree . . ."
                     34: 
                     35: /*
                     36:  * This program creates a pair of connected sockets then forks and
                     37:  * communicates over them.  This is very similar to communication with pipes,
                     38:  * however, socketpairs are two-way communications objects. Therefore I can
                     39:  * send messages in both directions. 
                     40:  */
                     41: 
                     42: main()
                     43: {
                     44:        int sockets[2], child;
                     45:        char buf[1024];
                     46: 
                     47:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
                     48:                perror("opening stream socket pair");
                     49:                exit(1);
                     50:        }
                     51: 
                     52:        if ((child = fork()) == -1)
                     53:                perror("fork");
                     54:        else if (child) {       /* This is the parent. */
                     55:                close(sockets[0]);
                     56:                if (read(sockets[1], buf, 1024, 0) < 0)
                     57:                        perror("reading stream message");
                     58:                printf("-->%s\en", buf);
                     59:                if (write(sockets[1], DATA2, sizeof(DATA2)) < 0)
                     60:                        perror("writing stream message");
                     61:                close(sockets[1]);
                     62:        } else {                /* This is the child. */
                     63:                close(sockets[1]);
                     64:                if (write(sockets[0], DATA1, sizeof(DATA1)) < 0)
                     65:                        perror("writing stream message");
                     66:                if (read(sockets[0], buf, 1024, 0) < 0)
                     67:                        perror("reading stream message");
                     68:                printf("-->%s\en", buf);
                     69:                close(sockets[0]);
                     70:        }
                     71: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.