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

vignesh.a.p

New member
Joined
Mar 15, 2007
Messages
2
Programming Experience
Beginner
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!
 
I wouldn't send using a Timer interval, also your sendfile algoritm looks inaccurate. Download and check out the attachment in post 2 of this thread: http://www.vbdotnetforums.com/showthread.php?t=16379 It demonstrates a client connecting to a server and sends a file in chunks, it also sends the filesize and filename first so server know how to handle the incomming stream, it's a very basic client/server example.
 
Thanks !!!

I understood one thing from your code..... my code in entirety is very bad... but with my two months of VB experience I couldn't do any better... :(

Threading is new clue.. I'd learn more about it and re-work my application.

Can you suggest any classes, books, web resources that I should refer. most VB books skip networking...

Thank you..
 
I've only read framework documentation and articles/codes found on the internet myself about sockets, so I don't have a book recommendation, you could try asking in the book section of our forums. Threading is a general subject you can learn without touching net/sockets, but using net/sockets almost always requires use and knowledge of threading - it's worth to study a tutorial or twenty and trying out.
 
Back
Top