• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

UNIX DOMAIN SOCKETS IN GO unix域套接字

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Unix domain sockets in Go - Golang News https://golangnews.org/2019/02/unix-domain-sockets-in-go/

 

package main

import (
"fmt"
"log"
"net"
"os"
"time"
)

const SockAddr = "/tmp/echo.sock"

var reqCounter int64
var startTime time.Time

func init() {
startTime = time.Now()

}
func echoServer(c net.Conn) {
reqCounter += 1
lastMilliseconds := time.Since(startTime)
b1 := make([]byte, 128)
n, err := c.Read(b1)
fmt.Println("n, err", n, err, "b1", b1, "\nrcv\n", string(b1), "Client connected [%s]", c.RemoteAddr().Network())
c.Close()
ns := 10000000000.0
lastSeconds := float64(lastMilliseconds) / ns
fmt.Println("\nreqCounter=", reqCounter, "QPS=", float64(reqCounter)/lastSeconds)
}

func main() {

if err := os.RemoveAll(SockAddr); err != nil {
log.Fatal(err)
}

l, err := net.Listen("unix", SockAddr)
if err != nil {
log.Fatal("listen error:", err)
}
defer l.Close()

for {
// Accept new connections, dispatching them to echoServer
// in a goroutine.
conn, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}

go echoServer(conn)
}
}


package main

import (
"fmt"
"net"
"time"
)

const SockAddr = "/tmp/echo.sock"

func main() {

for {
c, err := net.Dial("unix", SockAddr)
if err != nil {
fmt.Println("c,err", c, err)
continue
}

clientStr := "abc世界-1--" + time.Now().String()
b1 := []byte(clientStr)
n, err := c.Write(b1)
fmt.Println(time.Now(), n, err, "clientStr\n", clientStr)

e := c.Close()
fmt.Println("e", e)

}
}


  

 

Go/src/net/unixsock.go:57

// ResolveUnixAddr returns an address of Unix domain socket end point.
//
// The network must be a Unix network name.
//
// See func Dial for a description of the network and address
// parameters.
func ResolveUnixAddr(network, address string) (*UnixAddr, error) {
   switch network {
   case "unix", "unixgram", "unixpacket":
      return &UnixAddr{Name: address, Net: network}, nil
   default:
      return nil, UnknownNetworkError(network)
   }
}

  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap