File Transfer; please have a look and help!

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner
Good morning all!

I’ am having trouble getting this code to work properly. This is the client code:

VB.NET:
Dim IP As String = InputBox("Enter the IP address or hostname of you traget!", _
"IP Address", "127.0.0.1")
Client = New TcpClient
Client.Connect(IP, 1234)
Dim NWStream As NetworkStream = Client.GetStream
Dim bytesToSend(Client.SendBufferSize) As Byte
Dim FI As New FileInfo(OpenFileDialog1.FileName)
ProgressBar1.Maximum = FI.Length
Dim FileSTR As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim FileReader As New BinaryReader(FileSTR)
Dim numBytesRead As Integer
Dim Ipos As Integer
 
Do Until Ipos >= FI.Length
numBytesRead = FileSTR.Read(bytesToSend, 0, bytesToSend.Length)
NWStream.Write(bytesToSend, 0, numBytesRead)
Ipos = Ipos + numBytesRead
ProgressBar1.Value += numBytesRead
ProgressBar1.Update()
NWStream.Flush()
Loop
NWStream.Flush()
FileSTR.Close()
FileReader.Close()
 
 
This is the server code:
 
Try
Dim Client As TcpClient = TCP.AcceptTcpClient()
Dim NWStream As NetworkStream = client.GetStream
Dim bytesToRead(client.ReceiveBufferSize) As Byte
Dim numBytesRead As Integer '= NWStream.Read(bytesToRead, 0, CInt(client.ReceiveBufferSize))
Dim BUFFER_SIZE As Integer = client.ReceiveBufferSize
Dim FileSTR As New FileStream(TextBox1.Text, FileMode.CreateNew, FileAccess.Write)
 
Do
numBytesRead = 0
numBytesRead = NWStream.Read(bytesToRead, 0, BUFFER_SIZE)
FileSTR.Write(bytesToRead, 0, numBytesRead)
StatusBarPanel1.Text = "Downloaded: " & numBytesRead & " KB."
'The NWStream.DataAvailable always returned false in-between packets so double check by using "And numBytesRead = 0"
Loop Until NWStream.DataAvailable = False 'And numBytesRead = 0
 
FileSTR.Close()
NWStream.Close()
Client.Close()
MsgBox("All done!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

It works, but it is only able to transfer files less then 1 mb.
How can I get this code to be able to send lager files?

Thank in advances

Side note: This code came from another project which you can find here: http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=2171&lngWId=10&txtForceRefresh=910200510483466942
 
Last edited:
Ok, I finally got it to work. All I did was put the "send" part of the client code in its own thread and replace this part in the server code
VB.NET:
[/color][color=blue][font='Courier New']Loop[/font][/color][color=black][font='Courier New'] [/font][/color][color=blue][font='Courier New']Until[/font][/color][color=black][font='Courier New'] NWStream.DataAvailable = [/font][/color][color=blue][font='Courier New']False
with the second part
VB.NET:
[/font][/color][color=blue][font='Courier New']Loop[/font][/color][color=black][font='Courier New'] [/font][/color][color=blue][font='Courier New']Until[/font][/color][color=black][font='Courier New'] numBytesRead = 0
, and it
worked. The only problem is now the loop in the server code does not stop. It does not affect or “hang” the server, but it also does not send a message box to the user letting them know that the download is complete; also it does not close the download file, but they can tell when the download is complete, if the total number of bytes downloaded stops adding up in the status bar. If any of you guys want the finally code then let me know and I will post it up.

[/font]
 
Back
Top