Results 1 to 14 of 14

Thread: [2005] File will not send without Messagebox being displayed.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    [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:
    1. Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
    2.         'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
    3.         Dim FileReader As IO.BinaryReader
    4.         Dim Fs As IO.FileStream
    5.         Dim LastBytes
    6.         If File.Exists(FileName) Then
    7.             Fs = New IO.FileStream(FileName, FileMode.Open)
    8.             FileReader = New IO.BinaryReader(Fs)
    9.             SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
    10.             MsgBox(FileReader.BaseStream.Length)
    11.             SyncLock Clients(WhichClient).DataStream
    12.                 Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
    13.                     If FileReader.BaseStream.Position + 8 > FileReader.BaseStream.Length Then
    14.                         LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
    15.                     Else
    16.                         LastBytes = 8
    17.                     End If
    18.                     Clients(WhichClient).DataStream.Write(FileReader.ReadBytes(LastBytes), 0, LastBytes)
    19.                 Loop
    20.             End SyncLock
    21.             FileReader.BaseStream.Dispose()
    22.             FileReader.Close()
    23.             Fs.Dispose()
    24.             Fs.Close()
    25.         Else
    26.             MessageBox.Show("The File: " & FileName & " does not exist")
    27.         End If
    28.  
    29.     End Sub


    here is the client sub
    vb Code:
    1. Private Sub ReceiveAChunk(ByVal MyData As String)
    2.         'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
    3.         If MyFileInfo.FileSize = 0 Then
    4.             Dim TempString() As String
    5.             TempString = Split(MyData, "-|-")
    6.             'MyData current format
    7.             'Filename -|- Length
    8.             MyFileInfo.FileName = TempString(0)
    9.             MyFileInfo.FileName = Mid(MyFileInfo.FileName, InStrRev(MyFileInfo.FileName, "\"))
    10.             MyFileInfo.FileName = Application.StartupPath & MyFileInfo.FileName
    11.             MyFileInfo.FileSize = Val(TempString(1))
    12.             MsgBox(MyFileInfo.FileName & vbNewLine & "File Length:" & MyFileInfo.FileSize)
    13.             MyFileInfo.Transfering = True
    14.         End If
    15.  
    16.         Dim Filewriter As IO.BinaryWriter
    17.         Dim Fs As IO.FileStream
    18.         If File.Exists(MyFileInfo.FileName) Then
    19.             MsgBox("File exists")
    20.         Else
    21.             Fs = New IO.FileStream(MyFileInfo.FileName, FileMode.CreateNew)
    22.             Filewriter = New IO.BinaryWriter(Fs)
    23.  
    24.             Dim MyBytes(8) As Byte
    25.             Dim I As Integer
    26.             Dim LastBytes As Integer
    27.             SyncLock MyDataStream
    28.                 For MyFileInfo.BytesReceived = 0 To MyFileInfo.FileSize Step 8
    29.                     MyDataStream.Read(MyBytes, 0, 8)
    30.                     If MyFileInfo.BytesReceived + 8 > MyFileInfo.FileSize Then
    31.                         LastBytes = MyFileInfo.FileSize - MyFileInfo.BytesReceived
    32.                     Else
    33.                         LastBytes = 8
    34.                     End If
    35.                     For I = 0 To LastBytes - 1 Step 1
    36.                         Filewriter.BaseStream.WriteByte(MyBytes(I))
    37.                     Next
    38.                 Next
    39.                 MsgBox(" I guess were done?")
    40.             End SyncLock
    41.             Filewriter.BaseStream.Dispose()
    42.             Filewriter.Close()
    43.             Fs.Dispose()
    44.             Fs.Close()
    45.             MyFileInfo.Transfering = False
    46.         End If
    47.         'Else
    48.         'MessageBox.Show("The File: " & FileName & " does not exist")
    49.         'End If
    50.  
    51.     End Sub
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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.
    Attached Files Attached Files
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  3. #3
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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?

    Signatures suck

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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:
    1. Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
    2.         'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
    3.         Dim FileReader As IO.BinaryReader
    4.         Dim Fs As IO.FileStream
    5.         Dim LastBytes
    6.         If File.Exists(FileName) Then
    7.             Fs = New IO.FileStream(FileName, FileMode.Open)
    8.             FileReader = New IO.BinaryReader(Fs)
    9.             SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
    10.             While Clients(WhichClient).DataStream.CanWrite = False
    11.             End While
    12.             SyncLock Clients(WhichClient).DataStream
    13.                 Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
    14.                     If FileReader.BaseStream.Position + 2048 > FileReader.BaseStream.Length Then
    15.                         LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
    16.                     Else
    17.                         LastBytes = 2048
    18.                     End If
    19.                     Clients(WhichClient).DataStream.Write(FileReader.ReadBytes(LastBytes), 0, LastBytes)
    20.                 Loop
    21.             End SyncLock
    22.             FileReader.BaseStream.Dispose()
    23.             FileReader.Close()
    24.             Fs.Dispose()
    25.             Fs.Close()
    26.         Else
    27.             MessageBox.Show("The File: " & FileName & " does not exist")
    28.         End If
    29.     End Sub

    --CLIENT--
    vb Code:
    1. Private Sub ReceiveAChunk(ByVal MyData As String)
    2.         If MyFileInfo.FileSize = 0 Then
    3.             Dim TempString() As String
    4.             TempString = Split(MyData, "-|-")
    5.             'MyData current format
    6.             'Filename -|- Length
    7.             MyFileInfo.FileName = TempString(0)
    8.             MyFileInfo.FileName = Mid(MyFileInfo.FileName, InStrRev(MyFileInfo.FileName, "\"))
    9.             MyFileInfo.FileName = Application.StartupPath & MyFileInfo.FileName
    10.             MyFileInfo.FileSize = Val(TempString(1))
    11.             MyFileInfo.Transfering = True
    12.         End If
    13.         Dim Filewriter As IO.BinaryWriter
    14.         Dim Fs As IO.FileStream
    15.         If File.Exists(MyFileInfo.FileName) Then
    16.             MsgBox("File exists")
    17.         Else
    18.             Fs = New IO.FileStream(MyFileInfo.FileName, FileMode.CreateNew)
    19.             Filewriter = New IO.BinaryWriter(Fs)
    20.  
    21.             Dim BufferSize As Long
    22.             BufferSize = (32 * 1024) '32k
    23.             Dim MyBytes(BufferSize) As Byte
    24.             Dim I As Integer
    25.             Dim LastBytes As Integer
    26.             SyncLock MyDataStream
    27.                 For MyFileInfo.BytesReceived = 0 To MyFileInfo.FileSize Step BufferSize
    28.                     MyDataStream.Read(MyBytes, 0, BufferSize)
    29.                     If MyFileInfo.BytesReceived + BufferSize > MyFileInfo.FileSize Then
    30.                         LastBytes = MyFileInfo.FileSize - MyFileInfo.BytesReceived
    31.                         ReDim Preserve MyBytes(LastBytes)
    32.                     Else
    33.                         LastBytes = BufferSize
    34.                     End If
    35.                     Filewriter.Write(MyBytes)
    36.                 Next
    37.             End SyncLock
    38.             Filewriter.BaseStream.Dispose()
    39.             Filewriter.Close()
    40.             Fs.Dispose()
    41.             Fs.Close()
    42.             MyFileInfo.Transfering = False
    43.             'MessageBox.Show("Transfer of " & MyFileInfo.FileName & " was successful." & vbNewLine & "Total bytes Transfered: " & MyFileInfo.BytesReceived)
    44.         End If
    45.     End Sub
    Last edited by rack; Jul 29th, 2007 at 04:42 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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)
    Last edited by rack; Jul 29th, 2007 at 05:44 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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?
    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:
    1. Imports system.io
    2.  
    3. Public Class Form1
    4.     Private Sub SendAFile(ByVal FileName As String, ByVal WhichClient As Integer)
    5.         'file path C:\Documents and Settings\Owner\Desktop\Ya Ali - Ringtone.mp3
    6.         Dim FileReader As IO.BinaryReader
    7.         Dim Fs As IO.FileStream
    8.         Dim Filewriter As IO.BinaryWriter
    9.         Dim FsW As IO.FileStream
    10.         FsW = New IO.FileStream(Application.StartupPath & "\Jeremytest.mp3", FileMode.CreateNew)
    11.         Filewriter = New IO.BinaryWriter(FsW)
    12.         Dim LastBytes
    13.         If File.Exists(FileName) Then
    14.             Fs = New IO.FileStream(FileName, FileMode.Open)
    15.             FileReader = New IO.BinaryReader(Fs)
    16.             'SendMessage(FileName & "-|-" & FileReader.BaseStream.Length, "<<SERVER>>", "FileTransfer:", WhichClient)
    17.             'While Clients(WhichClient).DataStream.CanWrite = False
    18.             'End While
    19.             Dim BufferSize As Long
    20.             BufferSize = (32 * 1024) '32k
    21.             Dim TestNetworkBuffer(64 * 1024) As Byte
    22.             Dim MyBytes2(BufferSize) As Byte
    23.             'SyncLock Clients(WhichClient).DataStream
    24.             Do While FileReader.BaseStream.Position < FileReader.BaseStream.Length
    25.                 If FileReader.BaseStream.Position + BufferSize > FileReader.BaseStream.Length Then
    26.                     LastBytes = FileReader.BaseStream.Length - FileReader.BaseStream.Position
    27.                 Else
    28.                     LastBytes = BufferSize
    29.                 End If
    30.                 TestNetworkBuffer = FileReader.ReadBytes(LastBytes)
    31.                 MyBytes2 = TestNetworkBuffer
    32.                 Filewriter.Write(MyBytes2, 0, LastBytes)
    33.             Loop
    34.             'End SyncLock
    35.             FileReader.BaseStream.Dispose()
    36.             FileReader.Close()
    37.             Fs.Dispose()
    38.             Fs.Close()
    39.             Filewriter.BaseStream.Dispose()
    40.             Filewriter.Close()
    41.             FsW.Dispose()
    42.             FsW.Close()
    43.         Else
    44.             MessageBox.Show("The File: " & FileName & " does not exist")
    45.         End If
    46.     End Sub
    47.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    48.         SendAFile("C:\Documents and Settings\Owner\Desktop\" & TextBox1.Text, 0)
    49.  
    50.     End Sub
    51. End Class
    Last edited by rack; Jul 29th, 2007 at 06:46 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  7. #7
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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

    Signatures suck

  8. #8
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] File will not send without Messagebox being displayed.

    here is something that may be of use
    http://www.codeproject.com/useritems/UnoLibsNet_V2.asp

    Signatures suck

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  10. #10
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] File will not send without Messagebox being displayed.

    I can upload it here if you want

    Signatures suck

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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?
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] File will not send without Messagebox being displayed.

    1 day bump.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    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.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width