search

Home  >  Q&A  >  body text

nginx - What is the .sock file used for?

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

PHP中文网PHP中文网2753 days ago845

reply all(2)I'll reply

  • 某草草

    某草草2017-05-16 17:05:49

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

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 17:05:49

    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

    reply
    0
  • Cancelreply