Giter Site home page Giter Site logo

Comments (3)

gansidui avatar gansidui commented on September 27, 2024

是的,每个packet都必须要有type。另外你没加type的话,server也不会崩溃,只是会断开conn而已。

from gotcp.

xiaofeihe avatar xiaofeihe commented on September 27, 2024

我换了 几台机器 都是这样 , 使用 nc 连接 ,或者使用端口扫描程序 扫描一下 就 崩溃。
(机器:linux (ubuntu 14.04) 和 debian (6) )

下面是 错误信息:

2014/10/29 00:03:23 readLoop panic: interface conversion: interface is nil, not int
panic: interface conversion: interface is nil, not int [recovered]
panic: interface conversion: interface is nil, not int

goroutine 49 [running]:
runtime.panic(0x519d20, 0xc20808e140)
/usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
main.(_ConnDelegate).OnClose(0xc208042060, 0xc208078180)
/home/he/dev/go/src/gotcp/example/server.go:75 +0x6f
github.com/gansidui/gotcp.func·001()
/home/he/dev/go/src/github.com/gansidui/gotcp/conn.go:93 +0xa8
sync.(_Once).Do(0xc2080781b8, 0x7f3ac38fbf18)
/usr/local/go/src/pkg/sync/once.go:40 +0x9f
github.com/gansidui/gotcp.(_Conn).Close(0xc208078180)
/home/he/dev/go/src/github.com/gansidui/gotcp/conn.go:94 +0x51
github.com/gansidui/gotcp.func·002()
/home/he/dev/go/src/github.com/gansidui/gotcp/conn.go:204 +0xc6
runtime.panic(0x519d20, 0xc20808e100)
/usr/local/go/src/pkg/runtime/panic.c:248 +0x18d
main.(_ConnDelegate).OnIOError(0xc208042060, 0xc208078180, 0x7f3ac3a741b0, 0xc2080002b0)
/home/he/dev/go/src/gotcp/example/server.go:79 +0x5c
github.com/gansidui/gotcp.(_Conn).readLoop(0xc208078180)
/home/he/dev/go/src/github.com/gansidui/gotcp/conn.go:223 +0x268
created by github.com/gansidui/gotcp.(_Conn).Do
/home/he/dev/go/src/github.com/gansidui/gotcp/conn.go:195 +0x66

我对go网络编程没有经验 , 但是 最近一个项目着急 使用 一个tcpserver 接受 现场仪器数据。
就是服务器 向 客户端 发送一条指令 客户端回馈请求。
我写了一个 程序:https://github.com/xiaofeihe/modbustcpserver
老是运行一段时间就 崩溃。 我想 使用你的 GOTCP 来做我程序 的 网络 服务。 但是 没有 头部 的 type ,和长度 这两个 内容。 (现场仪器 不可能 加上这两个) 希望 你能指教 我 一下

from gotcp.

gansidui avatar gansidui commented on September 27, 2024

哥哥啊, 你是直接使用example/server.go吧,这只是一个例子,跟example/client.go配套的,你需要根据你的需求重写。 另外你上面的crash信息是由下面的代码导致的,你没有加入type,那么读取包长时就直接返回错误了,然后断开conn并回调OnIOError和OnClose, 此时的c.GetExtraData()应该是nil。

func (this *ConnDelegate) OnClose(c *gotcp.Conn) {
    this.closeCount++
    fmt.Printf("OnClose[***%v***]\n", c.GetExtraData().(int))
}

func (this *ConnDelegate) OnIOError(c *gotcp.Conn, err error) {
    fmt.Printf("OnIOError[***%v***]:[%v]\n", c.GetExtraData().(int), err)
}

大概看了下你的程序 modbustcpserver, 运行一段时间之后崩溃,那是因为你没有分包导致的,如果你要使用TCP必须得自己分包,否则建议使用UDP。
你的代码的Handle函数中:

if lenght > 0 {
    buf[lenght] = 0
}
这个语句是有问题的你的crash可能就是这个导致的length大于你的buffer大小就会crash

感觉你是在用写C的方式写Go,字符串后面是不需要补'\0'的

from gotcp.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.