跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Go 编程语言] go tcp 服务器,如何解包超大数据包

发表于

如题,据说论坛日经月经都是 TCP 粘包?

参考了某论坛源码,直接贴链接不会被封号吧 某 csdn Golang 实现之 TCP 长连接-------服务端和客户端

/article/details/131402492

其中的解包核心代码

//如果消息体超过 4096(默认长度) var pack []byte if length > 4096 { pack = make([]byte, 0, int(length-1)) readableLength := length - 1 for { if readableLength < 4096 { slice := make([]byte, readableLength) _, err = reader.Read(slice) pack = append(pack, slice...) break } slice := make([]byte, int32(reader.Buffered())) _, err = reader.Read(slice) pack = append(pack, slice...) //更新可读长度 readableLength = readableLength - int32(len(slice)) } // buffer 返回缓冲中现有的可读的字节数,2+length+1 表示帧类型+数据长度+帧尾 } else if length < 4096 && int32(reader.Buffered()) < length-1 { //退回已读取的帧头 reader.UnreadByte() return Msg{Type: 1}, errors.New("数据长度不足") } else { // 读取剩余帧内容 pack = make([]byte, int(length-1)) _, err = reader.Read(pack) if err != nil { return Msg{Type: 1}, err } }

由于本人发送的消息体达到 2w ,进入第一个循环,打印出 len(slice),除了第一次有数据,后面 slice 长度全是 0 ,后面这个 pack 一直循环中,读取不到更多数据

求解?数据哪里去了,怎样能取出?

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.