有人能说出以下两个项目有什么问题吗?出于某种原因,似乎接收的字节数多于发送的字节数。感谢
接收代码:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
'Dim SumBytes As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ProcessIt As New Thread(AddressOf Process)
ProcessIt.IsBackground = True
ProcessIt.Start()
End Sub
Private Sub Process()
Dim ServerInst As New TcpListener(8000) 'port to receive instructions
Dim ClientInst As TcpClient
ServerInst.Start()
ClientInst = ServerInst.AcceptTcpClient
Dim netStream As NetworkStream = ClientInst.GetStream
Dim valid As Boolean = True
Dim number As Integer 'number of packets
Dim TotalNumberOfBytes As Integer
Dim FilePath As String
Dim portN As ULong
While valid = True
Dim buffer(10024 - 1) As Byte
Dim bytesRead As Integer = netStream.Read(buffer, 0, buffer.Length)
number = BitConverter.ToInt64(buffer, 0)
TotalNumberOfBytes = BitConverter.ToUInt64(buffer, 8)
portN = BitConverter.ToInt64(buffer, 16)
FilePath = System.Text.Encoding.ASCII.GetString(buffer, 25, buffer(24)) 'filename
If My.Computer.FileSystem.FileExists("C:\Test" & FilePath) Then
'Dim info2 As New FileInfo(FolderPath & FilePath)
'Dim length2 As Long = info2.Length
'If Math.Ceiling(length2 / 10023) <> number Then
' netStream.Flush()
' ReDim buffer(10023)
' buffer(0) = Math.Ceiling(length2 / 10023)
' netStream.Write(buffer, 0, buffer.Length)
' Directory.CreateDirectory(Path.GetDirectoryName(FolderPath & FilePath))
' writer = New BinaryWriter(System.IO.File.Open(FolderPath & FilePath, FileMode.Append))
' ProcessFile(FolderPath & FilePath, netStream, buffer, Math.Ceiling(length2 / 10024), writer)
'Else
' netStream.Flush()
' ReDim buffer(10023)
' buffer(0) = 0
' buffer(1) = 1
' netStream.Write(buffer, 0, buffer.Length)
'End If
Else
'netStream.Flush()
'ReDim buffer(10023)
'buffer(0) = 0
'netStream.Write(buffer, 0, buffer.Length)
Directory.CreateDirectory(Path.GetDirectoryName("C:\Test" & FilePath))
'writer = New BinaryWriter(System.IO.File.Open(FolderPath & FilePath, FileMode.Create))
'ProcessFile(FolderPath & FilePath, netStream, buffer, 0, writer)
End If
Dim rec As New ReceiveFile(FilePath, 8001, number)
End While
End Sub
Public Class ReceiveFile
Public Sub New(ByVal FilePath As String, ByVal port As Integer, ByVal numberOfPackets As Integer)
'Constructor
Dim ServerInstance As New TcpListener(port) 'port to receive file
ServerInstance.Start()
Dim ClientInstance As TcpClient = ServerInstance.AcceptTcpClient
Dim netStreamInstance As NetworkStream = ClientInstance.GetStream
Dim Buffer(10034) As Byte
Dim writer As BinaryWriter = New BinaryWriter(System.IO.File.Open("C:\Test" & FilePath, FileMode.Create))
Dim count As Integer = 0
Dim buff2(0) As Byte
Do While True
Dim bytesRead As ULong
Try
bytesRead = netStreamInstance.Read(Buffer, 0, Buffer.Length)
Catch ex As Exception
End
End Try
If System.Text.Encoding.ASCII.GetString(Buffer, 0, 11) <> "PenSendProc" Then
Continue Do
End If
Dim BufferData(UBound(Buffer) - 11) As Byte
For i = 0 To UBound(BufferData)
BufferData(i) = Buffer(i + 11)
Next
If count = numberOfPackets - 2 Then
ReDim Preserve BufferData(bytesRead - 1 - 11)
writer.Write(BufferData)
netStreamInstance.Write(buff2, 0, buff2.Length)
Exit Do
End If
writer.Write(BufferData)
netStreamInstance.Flush()
netStreamInstance.Write(buff2, 0, buff2.Length)
count += 1
Loop
writer.Close()
netStreamInstance.Write(buff2, 0, buff2.Length)
ServerInstance.Stop()
ClientInstance.Close()
End Sub
End Class
End Class
要发送的代码:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
Dim number As ULong
Dim CancelTimerThread As Boolean
Dim TimeItThread As Thread
Dim file As System.IO.StreamWriter
Dim threadTimeIt As Thread
Dim NumberOfBytes As ULong
Dim PortNumber As ULong = 8001
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Dim SendItClient As New TcpClient
'SendItClient.Connect("10.0.20.61", 8000)
'Dim nstm As NetworkStream = SendItClient.GetStream
'Dim GetFolder As New FolderBrowserDialog
'GetFolder.ShowDialog()
'Dim path1 As String
'path1 = GetFolder.SelectedPath
'GetBytes(path1)
'SendEachFileInDir(path1, path1, nstm)
'SendItClient.Close()
End Sub
Private Sub GetBytes(ByVal path As String)
For Each foundfile As String In My.Computer.FileSystem.GetFiles(path)
Dim pstm As Stream = New FileStream(foundfile, FileMode.Open, FileAccess.Read)
NumberOfBytes += pstm.Length
pstm.Close()
Next
If My.Computer.FileSystem.GetDirectories(path).Count > 0 Then
For Each Dir As String In My.Computer.FileSystem.GetDirectories(path)
GetBytes(Dir)
Next
End If
End Sub
Private Sub SendEachFileInDir(ByVal path As String, ByVal path1 As String, ByVal nstm As NetworkStream)
For Each foundfile As String In My.Computer.FileSystem.GetFiles(path)
Dim fstm As Stream = New FileStream(foundfile, FileMode.Open, FileAccess.Read)
Dim buffer(10024 - 1) As Byte
Dim size As ULong 'no of packets
size = Math.Ceiling(fstm.Length / 10024) + 1
Dim ByteSize() As Byte = BitConverter.GetBytes(size)
Dim ByteTotalNumber() As Byte = BitConverter.GetBytes(NumberOfBytes)
Dim PortNumberBytes() As Byte = BitConverter.GetBytes(PortNumber)
Dim filenamebytes() As Byte = System.Text.Encoding.ASCII.GetBytes(Mid(foundfile, path1.Length + 1))
Dim SendBytes(UBound(ByteSize) + UBound(filenamebytes) + UBound(ByteTotalNumber) + UBound(PortNumberBytes) + 4) As Byte
Dim count As Integer = 0
For i = 0 To UBound(ByteSize)
SendBytes(count) = ByteSize(i)
count += 1
Next
For i = 0 To UBound(ByteTotalNumber)
SendBytes(count) = ByteTotalNumber(i)
count += 1
Next
For i = 0 To UBound(PortNumberBytes)
SendBytes(count) = PortNumberBytes(i)
count += 1
Next
SendBytes(count) = filenamebytes.Length
count += 1
For i = 0 To UBound(filenamebytes)
SendBytes(count) = filenamebytes(i)
count += 1
Next
nstm.Write(SendBytes, 0, SendBytes.Length)
'ReDim buffer(1023)
'nstm.Read(buffer, 0, buffer.Length)
'Dim val As Integer = buffer(0)
'If val = 0 And buffer(1) = 1 Then
' Continue For
'End If
'Dim buff2(0) As Byte
' count = 0
' nstm.Flush()
' Do While True
' ReDim buffer(10023)
' Dim bytesRead As Integer = fstm.Read(buffer, 0, buffer.Length)
'If bytesRead = 0 Then
'Exit Do
'End If
'If count >= val Then
'If count = 10 Then
' End
'End If
'nstm.Write(buffer, 0, bytesRead)
'nstm.Read(buffer, 0, buffer.Length)
'nstm.Read(buff2, 0, buff2.Length)
'End If
'count += 1
'Loop
'nstm.Read(buff2, 0, buff2.Length)
Dim SendIt As New SendFile(foundfile, 8001, fstm)
Next
If My.Computer.FileSystem.GetDirectories(path).Count > 0 Then
For Each Dir As String In My.Computer.FileSystem.GetDirectories(path)
SendEachFileInDir(Dir, path1, nstm)
Next
End If
End Sub
Public Class SendFile
Public Sub New(ByVal foundfile As String, ByVal port As Integer, ByVal fstm As FileStream)
Dim count As Integer = 0
Dim SendFileClient As New TcpClient
Dim netsStream As NetworkStream
Dim Buffer(10023) As Byte
Dim buff2(0) As Byte
Dim buffstart() As Byte = System.Text.Encoding.ASCII.GetBytes("PenSendProc")
'Dim fstm As Stream = New FileStream(foundfile, FileMode.Open, FileAccess.Read)
SendFileClient.Connect("10.0.20.1", port)
netsStream = SendFileClient.GetStream
Do While True
ReDim Buffer(10023)
Dim bytesRead As Integer = fstm.Read(Buffer, 0, Buffer.Length)
Dim CountIt As Integer = 0
If bytesRead = 0 Then
Exit Do
End If
Dim BufferSend(UBound(buffstart) + UBound(Buffer) + 1) As Byte
For i = 0 To UBound(buffstart)
BufferSend(CountIt) = buffstart(i)
CountIt += 1
Next
For i = 0 To UBound(Buffer)
BufferSend(CountIt) = Buffer(i)
CountIt += 1
Next
If bytesRead < 10024 Then
'MessageBox.Show("jj")
End If
netsStream.Write(BufferSend, 0, 11 + bytesRead)
netsStream.Read(buff2, 0, buff2.Length)
netsStream.Flush()
count += 1
Loop
netsStream.Read(buff2, 0, buff2.Length)
End Sub
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SendItClient As New TcpClient
SendItClient.Connect("10.0.20.1", 8000)
Dim nstm As NetworkStream = SendItClient.GetStream
Dim GetFolder As New FolderBrowserDialog
GetFolder.ShowDialog()
Dim path1 As String
path1 = GetFolder.SelectedPath
GetBytes(path1)
SendEachFileInDir(path1, path1, nstm)
SendItClient.Close()
End Sub
End Class
谢谢你的时间.ddddddddddd