⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 af_unix2.c

📁 目前已经有很多介绍计算机网络的书籍
💻 C
字号:
/* af_unix2.c: * * AF_UNIX Socket Example : */#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/socket.h>#include <sys/un.h>/* * This function reports the error and * exits back to the shell : */static voidbail(const char *on_what) {    perror(on_what);    exit(1);}intmain(int argc,char **argv,char **envp) {    int z;           /* Status return code */    int sck_unix;                /* Socket */    struct sockaddr_un adr_unix;/* AF_UNIX */    int len_unix;                /* length */    const char pth_unix[]     /* Abs. Name */        = "Z*MY-SOCKET*";    /*     * Create a AF_UNIX (aka AF_LOCAL) socket:     */    sck_unix = socket(AF_UNIX,SOCK_STREAM,0);    if ( sck_unix == -1 )        bail("socket()");    /*     * Form an AF_UNIX Address :     */    memset(&adr_unix,0,sizeof adr_unix);    adr_unix.sun_family = AF_UNIX;    strncpy(adr_unix.sun_path,pth_unix,        sizeof adr_unix.sun_path-1)        [sizeof adr_unix.sun_path-1] = 0;    len_unix = SUN_LEN(&adr_unix);    /* Now make 1st byte null */    adr_unix.sun_path[0] = 0;    /*     * Now bind the address to the socket :     */    z = bind(sck_unix,        (struct sockaddr *)&adr_unix,        len_unix);    if ( z == -1 )        bail("bind()");    /*     * Display all of our bound sockets :     */    system("netstat -pa --unix 2>/dev/null| "        "sed -n '/^Active UNIX/,/^Proto/p;"        "/af_unix/p'");    /*     * Close and unlink our socket path :     */    close(sck_unix);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -