[2005] File will not send without Messagebox being displayed.
in the server code, if I take out the MsgBox(FileReader.BaseStream.Length) the client freezes, and the filesize of the transfered file is less then the complete file size. Line 10
here is the server sub
vb Code:
Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
Dim FileReader As IO.BinaryReader
Dim Fs As IO.FileStream
Dim LastBytes
If File.Exists(FileName) Then
Fs = New IO.FileStream(FileName, FileMode.Open)
FileReader = New IO.BinaryReader(Fs)
SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
MsgBox(FileReader.BaseStream.Length)
SyncLock Clients(WhichClient).DataStream
Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
If FileReader.BaseStream.Position + 8 > FileReader.BaseStream.Length Then
LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
Else
LastBytes = 8
End If
Clients(WhichClient).DataStream.Write(FileReader.ReadBytes(LastBytes), 0, LastBytes)
Loop
End SyncLock
FileReader.BaseStream.Dispose()
FileReader.Close()
Fs.Dispose()
Fs.Close()
Else
MessageBox.Show("The File: " & FileName & " does not exist")
End If
End Sub
here is the client sub
vb Code:
Private Sub ReceiveAChunk(ByVal MyData As String)
'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
If MyFileInfo.FileSize = 0 Then
Dim TempString() As String
TempString = Split(MyData, "-|-")
'MyData current format
'Filename -|- Length
MyFileInfo.FileName = TempString(0)
MyFileInfo.FileName = Mid(MyFileInfo.FileName, InStrRev(MyFileInfo.FileName, "\"))
MyFileInfo.FileName = Application.StartupPath & MyFileInfo.FileName
MyFileInfo.FileSize = Val(TempString(1))
MsgBox(MyFileInfo.FileName & vbNewLine & "File Length:" & MyFileInfo.FileSize)
MyFileInfo.Transfering = True
End If
Dim Filewriter As IO.BinaryWriter
Dim Fs As IO.FileStream
If File.Exists(MyFileInfo.FileName) Then
MsgBox("File exists")
Else
Fs = New IO.FileStream(MyFileInfo.FileName, FileMode.CreateNew)
Filewriter = New IO.BinaryWriter(Fs)
Dim MyBytes(8) As Byte
Dim I As Integer
Dim LastBytes As Integer
SyncLock MyDataStream
For MyFileInfo.BytesReceived = 0 To MyFileInfo.FileSize Step 8
MyDataStream.Read(MyBytes, 0, 8)
If MyFileInfo.BytesReceived + 8 > MyFileInfo.FileSize Then
LastBytes = MyFileInfo.FileSize - MyFileInfo.BytesReceived
Else
LastBytes = 8
End If
For I = 0 To LastBytes - 1 Step 1
Filewriter.BaseStream.WriteByte(MyBytes(I))
Next
Next
MsgBox(" I guess were done?")
End SyncLock
Filewriter.BaseStream.Dispose()
Filewriter.Close()
Fs.Dispose()
Fs.Close()
MyFileInfo.Transfering = False
End If
'Else
'MessageBox.Show("The File: " & FileName & " does not exist")
'End If
End Sub
2 Attachment(s)
Re: [2005] File will not send without Messagebox being displayed.
If this helps, here is the entire Server, and Client project as it is right now.
I've tried everything I know, and could think of.
The only thing I could guess could have anything to do with it is...
maybe it needs a wait, or read, or something after the sendmessage command? Or maybe it needs a .flush? I am lost. I know that I don't understand the stream enough to debug what is going wrong.
I write 8 bytes to a stream with a buffer of 8096 or something in size. then I read on the other end 8 bytes. and it does that till the end.
Maybe I need to put a wait, in the loop? if mydatastream.canwrite=false then
wait for a mili second? I dunno. Anyone that has any guesses i'm willing to try them.
Re: [2005] File will not send without Messagebox being displayed.
I downloaded your source and will look at it soon...
have you considered using UDP?, apparently it is unreliable and sometimes packets dont make it to their destination, also it is unordered (meaning packet 1 may be received before packet 2 but this is easily taken care of if you send an object which will hold order detail along with the data), one other bad point with UDP is that there is no way of telling if the packet is even received and another bad point is that a message can be received more than once.
It does have some good points thaugh, like bandwidth for one example, but in my eyes performance is nothing without reliability (who wants an app that will send the wrong message 10 times faster).Its just easier to work with, but nobody seems to use it?
Re: [2005] File will not send without Messagebox being displayed.
Ok, I got it to not hang, but now it's sending 12 extra bytes????
file size to start with is 377,106 bytes.
file size at the end is 377,118 bytes.
377,106 divided by 32k is 11.50836. Maybe i'm adding 1 extra byte each time I write or read or something?
Also, I just played the song file, its all screwed up, it loops through about 3-5seconds the entire 47 seconds of the song.
(EDIT)
If I put a messagebox just after line 35 in the client, during the last loop the client hangs, and the file does not get completed. Though if I remove this, it does get completed, but with 12 extra bytes, and with the file being screwed up.
(Edit 2)
I set the receive buffer size at 64k, and the send buffer size at 64k, to make sure I wasn't exceeding those somehow. File ends up being shorter now, and client hangs, also with clicks in MP3 song, so extra bytes are still getting in there.
Here is the server, and client code as it is now.
--SERVER--
vb Code:
Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
Dim FileReader As IO.BinaryReader
Dim Fs As IO.FileStream
Dim LastBytes
If File.Exists(FileName) Then
Fs = New IO.FileStream(FileName, FileMode.Open)
FileReader = New IO.BinaryReader(Fs)
SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
While Clients(WhichClient).DataStream.CanWrite = False
End While
SyncLock Clients(WhichClient).DataStream
Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
If FileReader.BaseStream.Position + 2048 > FileReader.BaseStream.Length Then
LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
Else
LastBytes = 2048
End If
Clients(WhichClient).DataStream.Write(FileReader.ReadBytes(LastBytes), 0, LastBytes)
Loop
End SyncLock
FileReader.BaseStream.Dispose()
FileReader.Close()
Fs.Dispose()
Fs.Close()
Else
MessageBox.Show("The File: " & FileName & " does not exist")
End If
End Sub
--CLIENT--
vb Code:
Private Sub ReceiveAChunk(ByVal MyData As String)
If MyFileInfo.FileSize = 0 Then
Dim TempString() As String
TempString = Split(MyData, "-|-")
'MyData current format
'Filename -|- Length
MyFileInfo.FileName = TempString(0)
MyFileInfo.FileName = Mid(MyFileInfo.FileName, InStrRev(MyFileInfo.FileName, "\"))
MyFileInfo.FileName = Application.StartupPath & MyFileInfo.FileName
MyFileInfo.FileSize = Val(TempString(1))
MyFileInfo.Transfering = True
End If
Dim Filewriter As IO.BinaryWriter
Dim Fs As IO.FileStream
If File.Exists(MyFileInfo.FileName) Then
MsgBox("File exists")
Else
Fs = New IO.FileStream(MyFileInfo.FileName, FileMode.CreateNew)
Filewriter = New IO.BinaryWriter(Fs)
Dim BufferSize As Long
BufferSize = (32 * 1024) '32k
Dim MyBytes(BufferSize) As Byte
Dim I As Integer
Dim LastBytes As Integer
SyncLock MyDataStream
For MyFileInfo.BytesReceived = 0 To MyFileInfo.FileSize Step BufferSize
MyDataStream.Read(MyBytes, 0, BufferSize)
If MyFileInfo.BytesReceived + BufferSize > MyFileInfo.FileSize Then
LastBytes = MyFileInfo.FileSize - MyFileInfo.BytesReceived
ReDim Preserve MyBytes(LastBytes)
Else
LastBytes = BufferSize
End If
Filewriter.Write(MyBytes)
Next
End SyncLock
Filewriter.BaseStream.Dispose()
Filewriter.Close()
Fs.Dispose()
Fs.Close()
MyFileInfo.Transfering = False
'MessageBox.Show("Transfer of " & MyFileInfo.FileName & " was successful." & vbNewLine & "Total bytes Transfered: " & MyFileInfo.BytesReceived)
End If
End Sub
Re: [2005] File will not send without Messagebox being displayed.
Well, I just tried a .bmp file, to see if I could visually see the changes, looks like it distorts the contents of the file so much, the .bmp can't be displayed.
I'm fresh out of ideas. I'm stuck. Please, anyone that has an option, or idea, no matter how small, remote, or unlikely, please post.
When using binarywriter.write(Byte() as bytearray, start, amountofdata)
the following code doesn't work.
Filewriter.Write(MyBytes, MyFileInfo.BytesReceived, LastBytes)
I get the error message:
Code:
offset and length were out of bounds for the array or count is greater
than the number of elements from index to the end of the source collection.
(EDIT)
Ok I figured out the above, but it doesn't fix the fact the file ends up competely screwed up.
Filewriter.Write(MyBytes, 0, LastBytes)
Re: [2005] File will not send without Messagebox being displayed.
It has to be something with the networkstream, or the way i'm writing to it, or reading from it.
I made a Binaryreader / writer application. Used the Filestream object also, and the file turned out exact duplicate.
Going to give up for now, and look back at this later.
(EDIT)
Ok, I can't give up my mind won't let me
Here is something from the help files, Does this have anything to do with my problems?
Quote:
The ReceiveBufferSize property gets or sets the number of bytes that you are expecting to store in the receive buffer for each read operation. This property actually manipulates the network buffer space allocated for receiving incoming data.
Your network buffer should be at least as large as your application buffer to ensure that the desired data will be available when you call the NetworkStream.Read method. Use the ReceiveBufferSize property to set this size. If your application will be receiving bulk data, you should pass the Read method a very large application buffer.
If the network buffer is smaller than the amount of data you request in the Read method, you will not be able to retrieve the desired amount of data in one read operation. This incurs the overhead of additional calls to the Read method.
(/Edit)
(edit 2)
Ok, I wonder if I have to change the way i'm thinking. Currently I'm thinking, the server sends 32k, the client gets 32k. So they read like that. Maybe I have to set it up so that if say only 12k gets sent, the client knows to read 12k. I dunno i'm grabbing at strings now...
vb Code:
Imports system.io
Public Class Form1
Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
Dim FileReader As IO.BinaryReader
Dim Fs As IO.FileStream
Dim Filewriter As IO.BinaryWriter
Dim FsW As IO.FileStream
FsW = New IO.FileStream(Application.StartupPath & "\Jeremytest.mp3", FileMode.CreateNew)
Filewriter = New IO.BinaryWriter(FsW)
Dim LastBytes
If File.Exists(FileName) Then
Fs = New IO.FileStream(FileName, FileMode.Open)
FileReader = New IO.BinaryReader(Fs)
'SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
'While Clients(WhichClient).DataStream.CanWrite = False
'End While
Dim BufferSize As Long
BufferSize = (32 * 1024) '32k
Dim TestNetworkBuffer(64 * 1024) As Byte
Dim MyBytes2(BufferSize) As Byte
'SyncLock Clients(WhichClient).DataStream
Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
If FileReader.BaseStream.Position + BufferSize > FileReader.BaseStream.Length Then
LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
Else
LastBytes = BufferSize
End If
TestNetworkBuffer = FileReader.ReadBytes(LastBytes)
MyBytes2 = TestNetworkBuffer
Filewriter.Write(MyBytes2, 0, LastBytes)
Loop
'End SyncLock
FileReader.BaseStream.Dispose()
FileReader.Close()
Fs.Dispose()
Fs.Close()
Filewriter.BaseStream.Dispose()
Filewriter.Close()
FsW.Dispose()
FsW.Close()
Else
MessageBox.Show("The File: " & FileName & " does not exist")
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SendAFile("C:\Documents and Settings\Owner\Desktop\" & TextBox1.Text, 0)
End Sub
End Class
Re: [2005] File will not send without Messagebox being displayed.
Try this.
First make a class that has atleast a bytes property (may need more)
Code:
<Serializable()>Public class FileToSend
Private Bytes() as byte
Property Bytes
Get
Set
End Property
Public Sub AddFile(byval path as string)
'Serialize file into memory stream to add to bytes
End Sub
Public Sub SaveFile(byval path as string)
'Deserialize file and save to disc
End Sub
End Sub
Then serialize and send this object.
Encapsulating the file inside an object may preserve the contents a bit better, If you dont know how to serialize and deserialize the file into a byte array using a memory stream I have an example , that you can find in another post from a few weeks ago, or I could reupload it tommorow
Here is the Zip : http://www.vbforums.com/attachment.p...9&d=1184977873
Here is the thread : http://www.vbforums.com/showthread.p...47#post2949647
Re: [2005] File will not send without Messagebox being displayed.
Re: [2005] File will not send without Messagebox being displayed.
... I'm not a member of the code project.
I'll read up about Serialization.
Re: [2005] File will not send without Messagebox being displayed.
I can upload it here if you want
Re: [2005] File will not send without Messagebox being displayed.
I read up about serialazation, I don't think it directly relates, other then the fact they can both work with bytes and byte arrays.
Sure, i'll check out the other persons code if there is code.
I know other people are looking at this, do you guys see anything at all?
Re: [2005] File will not send without Messagebox being displayed.
I searched google, I searched Microsofts online help, there 101 source code, the MSDN Help files for the different functions, Binarywriter, BinaryReader, Filestream, Networkstream. I searched VBForums. I've read a lot of articles about what streams are. I've read a lot of posts about people wanting to create chat programs. I have yet to find source code for a working file transfer in .net (not using winsock control). I know that the problem I am having has to do with the networkstream, and my incomplete understanding of it. Because I wrote a program that reads from a file, into a filestream then into a binaryreader, it writes the data it gets into a byte array, then it writes the byte array into a binarywriter that writes that data into a filestream to a new file. I am going to sleep. Maybe my sub-consious will figure something out.
Re: [2005] File will not send without Messagebox being displayed.
Re: [2005] File will not send without Messagebox being displayed.
Last attempt, Bump.
Tomorrow if I don't get an answer, I'll try searching for a book. If I figure it out, i'll post the code, and the answer. Thanks for all the help so far, and for any help that may get posted tonight while I sleep.