1 Star 0 Fork 1

flyiot/flylibs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
servetcp.go 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
flyrainning 提交于 2023-03-03 16:52 . u
package mbserver
import (
"io"
"log"
"net"
"strings"
)
func (s *Server) accept(listen net.Listener) error {
for {
conn, err := listen.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return nil
}
log.Printf("Unable to accept connections: %#v\n", err)
return err
}
go func(conn net.Conn) {
defer conn.Close()
for {
packet := make([]byte, 512)
bytesRead, err := conn.Read(packet)
if err != nil {
if err != io.EOF {
log.Printf("read error %v\n", err)
}
return
}
// Set the length of the packet to the number of read bytes.
packet = packet[:bytesRead]
frame, err := NewTCPFrame(packet)
if err != nil {
log.Printf("bad packet error %v\n", err)
return
}
request := &Request{conn, frame}
s.requestChan <- request
}
}(conn)
}
}
// ListenTCP starts the Modbus server listening on "address:port".
func (s *Server) ListenTCP(addressPort string) (err error) {
listen, err := net.Listen("tcp", addressPort)
if err != nil {
log.Printf("Failed to Listen: %v\n", err)
return err
}
s.listeners = append(s.listeners, listen)
go s.accept(listen)
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flyiot/flylibs.git
git@gitee.com:flyiot/flylibs.git
flyiot
flylibs
flylibs
741ab3437bd2

搜索帮助