nginx - What is the .sock file used for?
PHP中文网
PHP中文网 2017-05-16 17:03:49
0
2
760

I would like to ask what the .sock file is used for. I have no idea at all. . .

PHP中文网
PHP中文网

认证高级PHP讲师

reply all (2)
某草草

means.sock为后缀的文件而已。UNIX 系统不以后缀区分文件类型,但为了方便,通常使用后缀来标识一下。.sock文件极有可能是UNIX 域套接字(UNIX domain socket),即通过文件系统(而非网络地址)进行寻址和访问的套接字。参见 man 手册 unix(7)(命令:man 7 unix).

    淡淡烟草味

    Unix domain socket
    http://en.wikipedia.org/wiki/Unix_domain_socket

    The opposite is tcp sockt
    http://lists.freebsd.org/pipermail/freebsd-performance/2005-February/001143.html

    Example (golang):
    From this

    server.go

    package main import "net" func echoServer(c net.Conn) { for { buf := make([]byte, 512) nr, err := c.Read(buf) if err != nil { return } data := buf[0:nr] println("Server got:", string(data)) _, err = c.Write(data) if err != nil { panic("Write: " + err.Error()) } } } func main() { l, err := net.Listen("unix", "/tmp/echo.sock") if err != nil { println("listen error", err.Error()) return } for { fd, err := l.Accept() if err != nil { println("accept error", err.Error()) return } go echoServer(fd) } }

    client.go

    package main import ( "io" "net" "time" ) func reader(r io.Reader) { buf := make([]byte, 1024) for { n, err := r.Read(buf[:]) if err != nil { return } println("Client got:", string(buf[0:n])) } } func main() { c, err := net.Dial("unix", "/tmp/echo.sock") if err != nil { panic(err.Error()) } defer c.Close() go reader(c) for { _, err := c.Write([]byte("hi....")) if err != nil { println(err.Error()) break } time.Sleep(1e9) } }

    When running the server, the /tmp/echo.sock file will be suggested

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!