Results 1 to 5 of 5

Thread: Sending file through TCP.... HELP !!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    2

    Sending file through TCP.... HELP !!!

    hello all and thank you in advance for any and all assistance

    I'm working on a LAN file transfer application. I've established a TCP connection through TCP client and listener. I then used stream to send data. It worked fine for small files. But when I tried for larger files by sending chunks per instance it showed an exception error. I used timer to loop it. Here is the code...

    Code:
    Private Sub sendtimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendtimer.Tick
    
            sendcount += 1
    
    
    
                If sendcount <= Math.Truncate(sendsize / 2048) Then '2048 buffer
    
                    Dim filetonet(2047) As Byte
                    sendfile.Read(filetonet, 0, 2048)
                    sendstream.Write(filetonet, 0, 2048)
                    
                ElseIf remain <> 0 Then
    
                    Dim filetonet(remain - 1) As Byte
                    sendfile.Read(filetonet, 0, filetonet.Length)
                    sendstream.Write(filetonet, 0, filetonet.Length)
    
    
                    'post send operations
                   
                    sendtimer.Enabled = False
                    status.Text = "File Sent"
                    sendfile.Close()
                    sendstream.Dispose()
                    sendclient.Close()
    
                Else
    
                    'post send operations
                    
                    sendtimer.Enabled = False
                    sendfile.Close()
                    status.Text = "File Sent"
                    sendstream.Dispose()
                    sendclient.Close()
    
                End If
    
    End Sub
    The real problem is when this timer ticks for first time it sends 2048 bytes. But for second time the highlighted line produces cannot write error. Watch window shows that

    my sendclient.connected = true for first time and second time its false.

    why does it disconnect automatically? is there any other better way to do my transfer.. kindly help!

  2. #2
    New Member
    Join Date
    Mar 2007
    Posts
    2

    Smile Re: Sending file through TCP.... HELP !!!

    Try this...
    Attached Files Attached Files

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Sending file through TCP.... HELP !!!

    Quote Originally Posted by vignesh.a.p
    I used timer to loop it.
    That's your error. Send chunk N+1 after chunk N is sent. Using a timer you really have no idea what state the system is in when you send the next chunk. It may have been waiting a long time or, due to the path being slow, it may not even have started sending.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    2

    Question Re: Sending file through TCP.... HELP !!!

    The same error occurs when i use 'for' or 'while' loop... I'm new to VB... I could do only this far with my knowledge. Can anyone suggest a better way to do this transfer? Any suggestions regarding the class I should refer?

    I got a doubt..... I'm reading data from a stream to a buffer. when i again repeat the same line of code, will the data from beginning of stream gets read again, or data after the last read position gets read?

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Sending file through TCP.... HELP !!!

    That depends on the state of the stream, which is why you should let the state of the stream tell you when to read data. Or don't use a stream. What control are you using for your TCP connection? Winsock?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

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