ik ben bezig met een programme te maken die mijn data kopieert alleen als ik een wat grotere bestand wil kopieer dan krijg ik een fout:
Value was either too large or too small for an Int32.
hoe kan ik dit voorkomen?
Broncode:
server:
Private _ListenThread As System.Threading.Thread
Dim txtPath = "c:\test\"
Dim txtPort = "1000"
Private Sub ListenForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Listen As New ListenThread(Integer.Parse(txtPort), txtPath)
_ListenThread = New System.Threading.Thread(AddressOf Listen.Listen)
_ListenThread.IsBackground = True
_ListenThread.Start()
End Sub
End Class
Public Class ListenThread
Private _ListenPort As Integer
Private _SaveLocation As String
Public Sub New(ByVal ListenPort As Integer, ByVal SaveLocation As String)
_ListenPort = ListenPort
_SaveLocation = SaveLocation
End Sub
Public Sub Listen()
Dim serv As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, _ListenPort)
serv.Start()
While True
Dim tcp As System.Net.Sockets.TcpClient
tcp = serv.AcceptTcpClient
Dim rt As New ReceiveThread(tcp, _SaveLocation)
Dim t As New System.Threading.Thread(AddressOf rt.Download)
t.IsBackground = True
t.Start()
End While
End Sub
End Class
Public Class ReceiveThread
Private _tcp As System.Net.Sockets.TcpClient
Private _location As String
Public Sub New(ByVal t As System.Net.Sockets.TcpClient, ByVal location As String)
_tcp = t
_location = location
End Sub
Public Sub Download()
'decalre vars etc
Dim ns As System.IO.Stream
ns = _tcp.GetStream
Dim br As New System.IO.BinaryReader(ns)
Dim length As Integer
length = Convert.ToInt32(br.ReadInt64())
Dim filename As String = br.ReadString()
Dim data(length) As Byte
Dim position As Integer = 0
Dim complete As Integer = 1
Dim returned As Integer = 0
Dim fs As System.IO.Stream
Try
'try to open the file location
fs = System.IO.File.OpenWrite(_location & "\" & filename)
While Not complete = 0
position = ns.Read(data, returned, length)
fs.Write(data, returned, position)
complete = position
returned += position
length -= position
End While
Catch ex As Exception
MessageBox.Show("Error occured in the download..." & Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Application.Restart()
Return
Finally
br.Close()
fs.Close()
End Try
End Sub
Client:
Sub transfer(ByVal txtFile)
Dim fs As System.IO.Stream
Try
fs = System.IO.File.OpenRead(txtFile)
Catch
MessageBox.Show("Couldn't open source file", "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim tcp As New System.Net.Sockets.TcpClient(txtIP, Integer.Parse(txtPort))
Dim ns As System.IO.Stream
ns = tcp.GetStream()
Dim bw As New System.IO.BinaryWriter(ns)
bw.Write(fs.Length)
bw.Write(System.IO.Path.GetFileName(txtFile))
Dim BytesRead As Integer = 0
Dim data(1000) As Byte
Do
BytesRead = fs.Read(data, 0, 999)
ns.Write(data, 0, BytesRead)
Application.DoEvents()
Loop While BytesRead > 0
bw.Write(data)
bw.Close()
fs.Close()
Value was either too large or too small for an Int32.
hoe kan ik dit voorkomen?

Broncode:
server:
Private _ListenThread As System.Threading.Thread
Dim txtPath = "c:\test\"
Dim txtPort = "1000"
Private Sub ListenForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Listen As New ListenThread(Integer.Parse(txtPort), txtPath)
_ListenThread = New System.Threading.Thread(AddressOf Listen.Listen)
_ListenThread.IsBackground = True
_ListenThread.Start()
End Sub
End Class
Public Class ListenThread
Private _ListenPort As Integer
Private _SaveLocation As String
Public Sub New(ByVal ListenPort As Integer, ByVal SaveLocation As String)
_ListenPort = ListenPort
_SaveLocation = SaveLocation
End Sub
Public Sub Listen()
Dim serv As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, _ListenPort)
serv.Start()
While True
Dim tcp As System.Net.Sockets.TcpClient
tcp = serv.AcceptTcpClient
Dim rt As New ReceiveThread(tcp, _SaveLocation)
Dim t As New System.Threading.Thread(AddressOf rt.Download)
t.IsBackground = True
t.Start()
End While
End Sub
End Class
Public Class ReceiveThread
Private _tcp As System.Net.Sockets.TcpClient
Private _location As String
Public Sub New(ByVal t As System.Net.Sockets.TcpClient, ByVal location As String)
_tcp = t
_location = location
End Sub
Public Sub Download()
'decalre vars etc
Dim ns As System.IO.Stream
ns = _tcp.GetStream
Dim br As New System.IO.BinaryReader(ns)
Dim length As Integer
length = Convert.ToInt32(br.ReadInt64())
Dim filename As String = br.ReadString()
Dim data(length) As Byte
Dim position As Integer = 0
Dim complete As Integer = 1
Dim returned As Integer = 0
Dim fs As System.IO.Stream
Try
'try to open the file location
fs = System.IO.File.OpenWrite(_location & "\" & filename)
While Not complete = 0
position = ns.Read(data, returned, length)
fs.Write(data, returned, position)
complete = position
returned += position
length -= position
End While
Catch ex As Exception
MessageBox.Show("Error occured in the download..." & Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Application.Restart()
Return
Finally
br.Close()
fs.Close()
End Try
End Sub
Client:
Sub transfer(ByVal txtFile)
Dim fs As System.IO.Stream
Try
fs = System.IO.File.OpenRead(txtFile)
Catch
MessageBox.Show("Couldn't open source file", "error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End Try
Dim tcp As New System.Net.Sockets.TcpClient(txtIP, Integer.Parse(txtPort))
Dim ns As System.IO.Stream
ns = tcp.GetStream()
Dim bw As New System.IO.BinaryWriter(ns)
bw.Write(fs.Length)
bw.Write(System.IO.Path.GetFileName(txtFile))
Dim BytesRead As Integer = 0
Dim data(1000) As Byte
Do
BytesRead = fs.Read(data, 0, 999)
ns.Write(data, 0, BytesRead)
Application.DoEvents()
Loop While BytesRead > 0
bw.Write(data)
bw.Close()
fs.Close()