我在VB.Net中创建了一个文件共享程序,但是当我尝试发送文件时,接收部分没有正确接收文件。只是我发送的文件的一部分。说,我发送了100 MB文件,只收到10 MB。有时22 KB,39 MB等。接收文件的大小并不总是相同。您认为这会导致什么问题?如何正确接收整个文件?
发送部分
Sub Main()
Dim ip As New IPEndPoint(IPAddress.Any, 8888)' Port Server
Dim sock As New Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)' Protocol type
sock.Bind(ip)
sock.Listen(100)
While True' Check Client Connection
Try
msg("Accept Client Connect")
Dim csock As Sockets.Socket = sock.Accept
Dim csend(1024 * 10000) As Byte
Dim path As String = "C:\Users\IT\Desktop\test\" ' Location to save file
Dim bLen As Integer = csock.Receive(csend) ' Byte Read Len
Dim fileLen As Integer = BitConverter.ToInt32(csend, 0) ' File Len
Dim fName As String = Encoding.ASCII.GetString(csend, 4, fileLen) ' File Name
msg("Start Receive....." & fName)
Dim bWrite As New BinaryWriter(File.Open(path + fName, FileMode.Append))
bWrite.Write(csend, 4 + fileLen, bLen - 4 - fileLen)
msg("File receiced and Save " & path)
bWrite.Close()
csock.Close()
Catch ex As Exception
msg(ex.Message)
End Try
End While
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
接收部分
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ipServer As IPAddress() = Dns.GetHostAddresses("127.0.0.1") ' IP Server
Dim ip As IPEndPoint = New IPEndPoint(ipServer(0), 8888)
Dim csock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)
Dim fName As String = TextBox1.Text
Dim path As String = TextBox2.Text
Dim fNameByte() As Byte = Encoding.ASCII.GetBytes(fName)
Dim fData() As Byte = File.ReadAllBytes(path & "\" & fName)
Dim cData(4 + fName.Length + fData.Length) As Byte
Dim fDataLen() As Byte = BitConverter.GetBytes(fNameByte.Length)
Try
fDataLen.CopyTo(cData, 0)
fNameByte.CopyTo(cData, 4)
fData.CopyTo(cData, 4 + fNameByte.Length)
csock.Connect(ip)
csock.Send(cData)
MsgBox("Success Send File ")
csock.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
TextBox2.Text = System.IO.Path.GetDirectoryName(OpenFileDialog1.FileName)
End If
End Sub
答案 0 :(得分:0)
错误1, a使用 附加 模式创建了二进制文件,该模式应为 创建 第07行
错误2,应该使用嵌套循环来接收数据包。在这里我添加do~循环。如果条件检查天气是另一个数据包可用或不通过检查它的大小,如果size = 0然后退出循环 第08~13行
错误3,当收到第一个数据包时,它被写入文件中减去元数据。比如bWrite.Write(csend,4 + fileLen,bLen-4-fileLen)。 在第06/10行 这对于第一个数据包是好的,但对于剩余的数据包没有因为元数据在开始时只发送一次,所以不应该从每个数据包中删除它。保留值应为0,并且在这种情况下包的长度为bLen。正如您在 第12行 所见,我将重置这些值。
01, Dim csock As Sockets.Socket = sock.Accept
02, Dim csend(1024 * 10000) As Byte
03, Dim bLen As Integer = csock.Receive(csend) ' Byte Read Len
04, Dim fileLen As Integer = BitConverter.ToInt32(csend, 0) ' File Len
05, Dim fName As String = Encoding.ASCII.GetString(csend, 4, fileLen) ' File Name
06, Dim strt As Integer = 4 + fileLen, ln As Integer = bLen - 4 - fileLen
07, Dim bWrite As New BinaryWriter(File.Open(path + fName, FileMode.Create))
08, Do
09, If bLen = 0 Then Exit Do
10, bWrite.Write(csend, strt, ln)
11, bLen = csock.Receive(csend) ' Byte Read Len
12, strt = 0 : ln = bLen
13, Loop
抱歉,我尽力解释它。