剥离文件并通过TCP同时将大块写入服务器显示断开的管道错误

vc25:

我的客户端将文件分成多个块(每个块128mb),然后它将使用goroutines将这些块同时上传到多个服务器。

但是,当我使用多个goroutine时,我的客户端程序出现了错误。

write tcp [::1]:49324->[::1]:2001: write: broken pipe

在我的服务器上,错误是

EOF

请注意,管道断裂错误和EOF错误发生在不同的块中。例如,写入块1时可能发生管道破裂错误,而服务器接收块2时可能发生EOF错误。

下面是客户端代码:

//set maximum no. of goroutine running in the back
maxGoroutines := 3
guard := make(chan struct{}, maxGoroutines)

var sentByte int64

for i:= 0; i < chunkCount; i += 1{
    guard <- struct{}{} 

    go func(i int){
        index := i%len(serverList)
        vsConnection, _ := net.Dial("tcp", serverList[index])

        sentByte=0
        file, _ := os.Open(fileName)
        file.Seek(int64(i)*CHUNKSIZE,0) //CHUNKSIZE is 134217728
        for { 
            n, _ := file.Read(sendBuffer)

            n2, err2 := vsConnection.Write(sendBuffer[:n])
            if err2 != nil {
                fmt.Println("err2",err2,chunkName)              
            }
            if(n2!=65536){ //65536 is size of sendBuffer
                fmt.Println("n2",n2)
            }
            sentByte = sentByte+int64(n)
            if(sentByte == CHUNKSIZE){
                break;
            }
        }
        vsConnection.Close()
        file.Close()
        <-guard
    }(i)
}

下面是服务器代码:

func main() {
    mapping := cmap.New()
    server, error := net.Listen("tcp", ":2001")
    if error != nil {
        fmt.Println("There was an error starting the server" +    error.Error())
        return
    }

    for {
        connection, error := server.Accept()
        if error != nil {
            fmt.Println("There was am error with the connection" + error.Error())
            return
        }
        //one goroutine per connection
        go ConnectionHandler(connection,mapping)
    }
}

func ConnectionHandler(connection net.Conn, mapping cmap.ConcurrentMap) {
    fmt.Println("Connected")
    //make a buffer to hold data        
    var bufferFile bytes.Buffer
    writer := bufio.NewWriter(&bufferFile)

    var receivedBytes int64
    receivedBytes=0
    for {

        if(CHUNKSIZE<=receivedBytes){
            break
        }
        n,err := io.CopyN(writer, connection, BUFFERSIZE)
        receivedBytes += n
        if err != nil {
            fmt.Println("err", err.Error(), fileName)
            break
        }
    }
    mapping.Set(fileName,bufferFile.Bytes())
    connection.Close()

}

预先非常感谢。

abhink:

在您的客户端中,sentByte应该是发送方goroutine的局部变量。由于已将其声明为全局变量,因此代码中存在竞争条件。尝试以下修复:

go func(i int){
    index := i%len(serverList)
    vsConnection, _ := net.Dial("tcp", serverList[index])

    sentByte := 0 // make sentByte a local variable, so each goroutine 
                  // has its own copy 
    file, _ := os.Open(fileName)
    file.Seek(int64(i)*CHUNKSIZE,0) //CHUNKSIZE is 134217728
    for { 
        n, _ := file.Read(sendBuffer)
        // ...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过TCP套接字将音频写入服务器

将OpenCV框架写入gstreamer rtsp服务器管道

启动 MySQL 服务器 > 将输出通过管道传输到文件

从azure devops服务器下载文件将错误数据写入文件

如何通过节点服务器将JSON对象写入文件?

Scrapyd:将CSV文件写入远程服务器

将文件从远程服务器写入本地目录?

将文件从Jenkins主服务器复制到管道中的从服务器

通过Twine将py文件上传到代理服务器时出现SSL错误

确定TCP服务器中的断开连接

Node.js:通过socketio将客户端与服务器断开连接

Java:从服务器写入日志文件

PHP写入服务器上的文件

将行写入 SQL 服务器

SquirrelMail - 错误:IMAP 服务器断开连接

在Android上同时循环从服务器读取和写入

同时将视频从ios上传到服务器,然后给我502错误的网关错误

Serilog未写入MsSql服务器数据库,也未显示任何错误

通过 Volley Multi-part Request 将捕获的图像发送到本地烧瓶服务器;接收 java.net.socketexception 管道损坏错误

通过Guzzle将文件发布到Web服务器时出现HTTP 417错误(预期失败)

通过套接字将Java客户端中的int写入c服务器

如何将Go-lang服务器同时用作文件服务器和后端逻辑服务器

通过TCP / IP套接字发送文件(Web服务器)

通过python中的TCP套接字在客户端-服务器之间发送文件?

服务器迁移后Wordpress临时文件夹写入错误

Java通过TCP将进程数组发送到服务器

Ruby如何通过TCP(http)服务器将图像发送到客户端

通过TCP将android(socket客户端)与c ++(socket服务器)连接

500内部服务器错误,同时将数据保存到mongoDB