ass 发表于 3 天前

应该想想怎么把Conn结构体实现深拷贝

哈宝儿 发表于 3 天前

自己搞个缓冲区,想独几次读几次

哈宝儿 发表于 3 天前

准确的说是,读到的数据用自定义格式 全部打到chan队列,读了再丢回去可以一直读

哈宝儿Mr.Bean 发表于 3 天前

图一中 从Conn读了一次到buffer,那为什么第二次不可以从第一个buffer中复制一份,两次读取的内容不一样吗?我记得TCP这块是要解决粘包问题,一次缓冲区装不下的话,是读不完的&nbsp;&nbsp;读完没了不是正常?创个变量复制一份不就行<img src="https://hostloc.com/static/image/smiley/default/sweat.gif" smilieid="10" border="0" alt="" />

哈宝儿 发表于 3 天前

<div class="quote"><blockquote><font color="#999999">Mr.Bean 发表于 2024-11-19 19:06</font><br />
<font color="#999999">读完没了不是正常?创个变量复制一份不就行</font></blockquote></div><br />
我估计他想要peekData<br />
<br />
package main<br />
<br />
import (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;fmt&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;io&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;net&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &quot;sync&quot;<br />
)<br />
<br />
type PeekableConn struct {<br />
&nbsp; &nbsp; &nbsp; &nbsp; conn&nbsp; &nbsp;net.Conn<br />
&nbsp; &nbsp; &nbsp; &nbsp; buffer []byte<br />
&nbsp; &nbsp; &nbsp; &nbsp; mu&nbsp; &nbsp;&nbsp;&nbsp;sync.Mutex<br />
}<br />
<br />
// NewPeekableConn 返回一个新的 PeekableConn 实例<br />
func NewPeekableConn(conn net.Conn) *PeekableConn {<br />
&nbsp; &nbsp; &nbsp; &nbsp; return &amp;PeekableConn{conn: conn, buffer: make([]byte, 4096)}<br />
}<br />
<br />
// Peek 从连接中读取数据但不移除它<br />
func (p *PeekableConn) Peek(n int) ([]byte, error) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; p.mu.Lock()<br />
&nbsp; &nbsp; &nbsp; &nbsp; defer p.mu.Unlock()<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; // 先读取数据到 buffer<br />
&nbsp; &nbsp; &nbsp; &nbsp; nRead, err := p.conn.Read(p.buffer)<br />
&nbsp; &nbsp; &nbsp; &nbsp; if err != nil &amp;&amp; err != io.EOF {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil, err<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; // 返回前 n 个字节<br />
&nbsp; &nbsp; &nbsp; &nbsp; if n &gt; nRead {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = nRead<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; return p.buffer[:n], nil<br />
}<br />
<br />
// Read 读取数据并将其从连接中移除<br />
func (p *PeekableConn) Read(b []byte) (int, error) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; return p.conn.Read(b)<br />
}<br />
<br />
func main() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; listener, err := net.Listen(&quot;tcp&quot;, &quot;:8080&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Error:&quot;, err)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; defer listener.Close()<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; for {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn, err := listener.Accept()<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Error:&quot;, err)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; go func(c net.Conn) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer c.Close()<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peekableConn := NewPeekableConn(c)<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Peek 数据<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data, err := peekableConn.Peek(10)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Peek Error:&quot;, err)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf(&quot;Peeked Data: %s\n&quot;, data)<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 正常读取数据<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _, err = peekableConn.Read(data)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Read Error:&quot;, err)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf(&quot;Read Data: %s\n&quot;, data)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }(conn)<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
}<br />
<br />
通过对连接的读写进行同步,从而达到既可以“窥视”数据又不影响后续的数据读取

jiuqimax 发表于 3 天前

把socket学了再来问<img src="https://hostloc.com/static/image/smiley/default/smile.gif" smilieid="1" border="0" alt="" />

忘江湖 发表于 3 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16043930&ptid=1365358" target="_blank"><font color="#999999">zhou 发表于 2024-11-19 17:34</font></a></font><br />
当然不行,常见方法都试过了</blockquote></div><br />
tcp socket里面,要么你就自己粘包处理,要么就ReadFull让go给你自动粘包一次性读完所有客户端发来的数据,从你的回帖可以看出你对socket机制确实都不熟悉,所以没法沟通。

zhou 发表于 3 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16044245&ptid=1365358" target="_blank"><font color="#999999">哈宝儿 发表于 2024-11-19 19:10</font></a></font><br />
我估计他想要peekData<br />
<br />
package main</blockquote></div><br />
感谢大佬提供思路,需求就是这样的,目前已解决!<img src="https://hostloc.com/static/image/smiley/yct/017.gif" smilieid="40" border="0" alt="" />

zhou 发表于 3 天前

<i class="pstatus"> 本帖最后由 zhou 于 2024-11-20 10:41 编辑 </i><br />
<br />
已解决,感谢<a href="https://hostloc.com/home.php?mod=space&uid=57559" target="_blank">@哈宝儿</a>&nbsp;&nbsp;大佬提供思路<br />
参考 stackoverflow: https://stackoverflow.com/questions/26196813/peek-into-conn-without-reading-in-go<br />
<img id="aimg_iZlM8" onclick="zoom(this, this.src, 0, 0, 0)" class="zoom" src="https://pic.cm/i/2024/11/20/h2pzm7.png" onmouseover="img_onmouseoverfunc(this)" onload="thumbImg(this)" border="0" alt="" /><br />
<br />
<font size="6">我不理解有些人为什么一直GPT GPT的,并且无差别喷懂不懂的,既然你那么懂,倒是给出解决方案啊???在这里无脑就喷我目的是让你显眼吗???</font>

xiuxing 发表于 3 天前

<div class="quote"><blockquote><font size="2"><a href="https://hostloc.com/forum.php?mod=redirect&goto=findpost&pid=16045850&ptid=1365358" target="_blank"><font color="#999999">zhou 发表于 2024-11-20 10:35</font></a></font><br />
已解决,感谢@哈宝儿&nbsp;&nbsp;大佬提供思路<br />
参考 stackoverflow: https://stackoverflow.com/questions/26196813/p ...</blockquote></div><br />
大佬,带带
页: 1 2 3 [4] 5
查看完整版本: 【已解决】有没有golang大手子,问题求解