当前位置:编程学习 > C#/ASP.NET >>

网络传输文件的问题

传送图片接收不完整,请大家看一下是什么问题,帮我修改一下源代码,谢谢


服务端代码

Private Sub 接收数据线程()
        Dim bytes() As Byte = New [Byte](1024 * 100) {}
        Dim data As String = ""
        Dim tokens() As String
        Dim length As Integer

        While True
            Dim bytesRec As Integer
            While True
                Try
                    bytesRec = 客户端Socket.Receive(bytes)
                Catch ex As Exception
                    接收数据线程变量.Abort()
                End Try
                data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
                data = Trim(data)
                data = data.Replace(Chr(0), "")
                tokens = data.Trim.Split("|")
                Select Case tokens(0)
                    Case "消息"
                        RichTextBox1.Text += tokens(1) + vbNewLine
                    Case "文件"
                        文件名 = tokens(1)
                        发送消息("开始传输文件")
                        length = 客户端Socket.Receive(bytes)
                        Dim writer As FileStream = New FileStream(Path + "\bmp2.jpg", FileMode.OpenOrCreate, FileAccess.Write)
                        writer.Write(bytes, 0, length)
                        writer.Close()
                    Case "开始传输文件"
                        Dim read As FileStream = New FileStream(Path + "\bmp.jpg", FileMode.Open, FileAccess.Read)
                        length = read.Read(bytes, 0, 1024 * 100)
                        客户端Socket.Send(bytes, 0, length, SocketFlags.None)
                        read.Close()
                    Case "退出"
                        客户端Socket.Shutdown(SocketShutdown.Both)
                        客户端Socket.Close()
                        接收数据线程变量.Abort()
                End Select
            End While
        End While
    End Sub

    Private Function 发送消息(ByVal 消息 As String) As Boolean
        Dim MsgByte As Byte() = Encoding.UTF8.GetBytes(消息)
        Try
            客户端Socket.Send(MsgByte)
            Return True
        Catch ex As Exception
        End Try
    End Function



客户端代码

Private Sub 接收数据线程()
        Dim bytes() As Byte = New [Byte](1024 * 100) {}
        Dim data As String = ""
        Dim tokens() As String
        Dim length As Integer

        While True
            Dim bytesRec As Integer
            While True
                Try
                    bytesRec = 客户端Socket.Receive(bytes)
                Catch ex As Exception
                    接收数据线程变量.Abort()
                End Try
                data = Encoding.UTF8.GetString(bytes, 0, bytesRec)
                tokens = data.Trim.Split("|")
                Select Case tokens(0)
                    Case "消息"
                        RichTextBox1.Text += tokens(1) + vbNewLine
                    Case "文件"
                        文件名 = tokens(1)
                        发送消息("开始传输文件")
                        length = 客户端Socket.Receive(bytes)
                        Dim writer As FileStream = New FileStream(Path + "\bmp2.jpg", FileMode.OpenOrCreate, FileAccess.Write)
                        writer.Write(bytes, 0, length)
                        writer.Close()
                    Case "开始传输文件"
                        Dim read As FileStream = New FileStream(Path + "\bmp.jpg", FileMode.Open, FileAccess.Read)
                        length = read.Read(bytes, 0, 1024 * 100)
                        客户端Socket.Send(bytes, 0, length, SocketFlags.None)
                        read.Close()
                    Case "退出"
                        客户端Socket.Shutdown(SocketShutdown.Both)
                        客户端Socket.Close()
                        接收数据线程变量.Abort()
                End Select
            End While
        End While
    End Sub

    Private Function 发送消息(ByVal 消息 As String) As Boolean
        Dim MsgByte As Byte() = Encoding.UTF8.GetBytes(消息)
        Try
            客户端Socket.Send(MsgByte)
            Return True
        Catch ex As Exception
        End Try
    End Function --------------------编程问答-------------------- 大概你没有发送完,或者没有接收完
BTW:VB不会,看着很累
--------------------编程问答-------------------- 发错地方了哥们...\

不过思路是:
接收文件前要计算好文件大小,根据缓存大小处出最后一次接收要接收到的数据是多长,再进行保存,
就最后一次比较麻烦 ,你所说的问题,一般都是最后一次接收保存了所有缓存里的数据
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,