douyao4632 2019-04-18 11:34
浏览 59

TCP连接似乎一次读取太多字节

I'm writing an FTP server in Go and I've run into a problem when listing the files/directories. What seems to be happening is that the server is sending the files each separately and correctly but when reading them from the connection, they get read multiple at a time.

I've tried changing the buffer size as well as sending a confirmation before sending each file name yet when reading from the connection it ends up reading more bytes than expected.

This is the relative part from the server:

for _, fn := range files {
    conn.Write([]byte(fn.Name()))
}

In the code above, files is []os.FileInfo from ioutil.ReadDir("./") to get the files in the current directory. When tested this sends the correct file name as well as the correct bytes for each name.

On the client I have this:

var buf [256]byte
for i := 0; i < int(amnt); i++ {
    n, err = conn.Read(buf[:])
    if err != nil {
        fmt.Printf("Error getting files: %s
", err)
    }
    fmt.Printf("%s
", string(buf[:n]))
}

int(amnt) is just an int for how many files are in the directory and is correct.

The server sends everything correctly so I am sure that the problem lies in reading into buf but I can't figure out why it would be reading in more than one file name at a time.

  • 写回答

1条回答 默认 最新

  • douhulao7642 2019-04-18 14:25
    关注

    One strategy to address this could be to develop a loose structured protocol which will involve having the client delineate files and the server parse files. This might be done by sending a new line after each file that the client writes. This way the server can scan lines from the TCP connection using go's Scanner:

    for _, fn := range files {
        conn.Write([]byte(fn.Name()))
        conn.Write([]byte(`
    `))
    }
    
    scanner := bufio.NewScanner(conn)
    for {
            if ok := scanner.Scan(); !ok {
                break
            }
            fmt.Println(scanner.Text())
        }
        fmt.Println("Scanning ended")
    

    Go makes this possible because the TCP connection implements the io.Reader interface.

    评论

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误