473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

file transfer help?

vb.net 2003 Windows application

We have a Client/Server system set up by communicating through a
TCPClient object. The server loops continuously using a tcplistener
device and each time a client object attempts a connection, a new
instance of a Client object is created (in the server's clients
hashtable). The client object on the server (the client solution itself
has same communication setup only with the code in the main form)
handles communiction by using:
visual basic
code:--------------------------------------------------------------------------------
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String

Try
' Ensure that no other threads try to use the stream at the
same time.
SyncLock client.GetStream
' Finish asynchronous read into readBuffer and get
number of bytes read.
BytesRead = client.GetStream.EndRead(ar)
End SyncLock

' Convert the byte array the message was saved into, minus
one for the
' Chr(13).
strMessage = Encoding.ASCII.GetString(readBuffer, 0,
BytesRead - 1)
'LineReceived event points to the server frm (which has the hashtable
of object of type Client (i.e. 'Me')) method 'OnLineReceived'
which splits the strMessage into "command" and which ever data is
supplied
Then performs a function based on the command.
RaiseEvent LineReceived(Me, strMessage)

' Ensure that no other threads try to use the stream at the
same time.
SyncLock client.GetStream
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub

On the Client solution side, when run a tcpclient object is created:

client = New TcpClient(serverIP, serverPort)

' Start an asynchronous read invoking DoRead to avoid
lagging the user
' interface.
client.GetStream.BeginRead(readBuffer, 0,
READ_BUFFER_SIZE, AddressOf DoRead, Nothing)

and a command is sent to login ("CONNECT|username|password")
'|' is the delimeter used (or Chr(124))
--------------------------------------------------------------------------------

The problem starts when we've tried to implement a file transfer
between the server and client. We have created loops that read from a
file into a 'buffer(1024) as byte' in 1024 byte chunks, then
writing that buffer to the 'netstream = client.getstream' object
using:
visual basic
code:--------------------------------------------------------------------------------
Public Sub SendFile(ByVal filename As String, ByVal category As String,
ByVal size As Integer)
'Integer storing the total amount of bytes read when filling
the buffer
Dim totalbytesread As Integer = 0
Dim bytesread As Integer
'Byte array buffer of size specified by BUFFER_SIZE
Dim buffer(BUFFER_SIZE) As Byte
'Dim size As Integer = info.Length

If fs Is Nothing Then
fs = New FileStream("C:\i.doc", FileMode.Open,
FileAccess.Read, FileShare.Read, BUFFER_SIZE)
End If
Try

SyncLock netstream
While (totalbytesread < size And netstream.CanWrite)
bytesread = fs.Read(buffer, 0, BUFFER_SIZE)

netstream.Write(buffer, 0, bytesread)

totalbytesread = totalbytesread + bytesread
End While
End SyncLock
fs.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End sub
--------------------------------------------------------------------------------

And at the client end we have tried a similar loop that reads from the
tcpclient object's stream in 1024 byte chunks and then writes to a
file stream. For some reason, they can read some of the byte chunks and
write it to the file. But not all of the byte chunks are received, and
the chunks are often out of order. We have tried setting the receiving
end of the file transfer
Client.getstream.beginread(buffer,0,BUFFER_SIZE,Ad dressOf
ReceiveFile,Nothing) where ReceiveFile loops through the stream and
writes to a file.

We've based the communication model on an msdn chat client/server
example, and have attempted as many different ways to loop through the
stream on both the sending and receiving end, including:
Send file end:
· Read file into buffer(filesize)
· Write buffer into stream

Receive file end:
· Read buffer(filesize) from stream
(client.getstream.read(buffer,0,buffer.length)
· Write buffer to filestream (fs.write(buffer,0,buffer.length)

Which worked for small txt and doc files (without images) and worked
for one 64.1kb bmp image. Any help would be appreciated on how to get
this to work.Some code examples would be greatly appreciated

Thanx in advance

Oct 12 '06 #1
2 4180
You and I learned from the same example code. :)

Bonzol wrote:
And at the client end we have tried a similar loop that reads from the
tcpclient object's stream in 1024 byte chunks and then writes to a
file stream. For some reason, they can read some of the byte chunks and
write it to the file. But not all of the byte chunks are received, and
the chunks are often out of order.
<snip>

I didn't spot anything wrong with the partial code you provided, so I'm
going to take a guess based on the way you worded this.

TCP breaks up or combines what to you were individual transmissions, as
needed and according to its own rules; though it always maintains the
order.

As an example, let's say your server does this:
1) Wait for a connection
2) .Send(buf, 0, 1024)
3) .Send(buf, 0, 1024)

And your client does this:
1) Connect to server
2) .Read(buf, 0, 1024)
3) .Read(buf, 0, 1024)

Those two reads might actually get *less* than 1024 bytes each.
Meaning it would take additional reads to get all the data.

And here's where I'm guessing. I'm betting since your client *knows*
the file size, it *expects* each .Read or .EndRead to return a certain
amount of data, say 1024 bytes at a time for most of the file, and it
doesn't check the return value to see how many bytes were actually
read. It then .Writes the entire 1024 byte buffer to a file. The end
of that buffer may contain data from a previous read that wasn't
overwritten (perceived as out-of-order data). And since the total
amount of bytes read is less than the file size, you're also missing
data. The received file size would be correct, with the difference
made of duplicated, out-of-order data.

Here's what a file receive on the client using .Read would look like:

While (totalbytesread < size And netstream.CanWrite)
bytesread = fs.Read(buffer, 0, BUFFER_SIZE)
netstream.Write(buffer, 0, bytesread)
totalbytesread = totalbytesread + bytesread
End While

One more tip that might be applicable: Make sure you don't attempt to
convert a byte array containing binary data, such as from a picture
file, to a string using ASCIIEncoding. It doesn't handle byte values
128-255. Other encoding types I've tried also garble binary data in
various ways. There may yet be one that works, but I've decided it's
better to make sure binary data stays in byte arrays and never gets
converted to a string.

Well, I hope something here solves your problem. I'll continue
monitoring this thread until you have resolution.

Oct 12 '06 #2
Thanks heaps for the help, it explained what was happening
in an earlier attempt and gave me ideas on how to fix the problem.
I managed to get the transfer going by using the asynchronous
stream writing (beginWrite then endwrite) and reading (beginread and
endread) functions.

The basic pseudocode for that was

For Sending the file through the stream:

Begin an Asynchronous write to the stream connecting the client and
server
Loop untill the count of bytes read from file is >= filesize
-read 1024 bytes from file into byte array (which gives an
integer of how many bytes were read)
-write the amount of bytes read to the stream from byte array
-increase the count of bytes by amount of bytes read
end loop
end Asynchronous write
close filestream
For receiving files from the stream:

Loop until count of bytes read from file is >= filesize
- begin Asynchronous read from stream into byte array
- end Asynchronous read from stream (which gives the number of bytes
read)
- write the number of bytes read to file from byte array
end loop
close filestream
I think it was a combination asynchronous reads without async writes,
or
asyn writes without async reads that caused the headache. Thanks again,
hopefully
this might help some other people avoid an overload of stress.

Oct 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: clintonG | last post by:
After the last six days trying to download VS2005 Professional it seems there may be a problem with my instance of the File Transfer Manager (FTM) or somewhere in the network in between. I can't...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
8
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: ...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
3
by: Jay | last post by:
Hi, I have some unique situations where i need to transfer a file from Server to client but without any Open or Save dialog box shown to the user. The file should save itself directly to client...
2
by: Jay | last post by:
Hi, I have some unique situations where i need to transfer a file from Server to client but without any Open or Save dialog box shown to the user. The file should save itself directly to client...
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
2
by: tedpottel | last post by:
Hi, My program has the following code to transfer a binary file f = open(pathanme+filename,'rb') print "start transfer" self.fthHandle.storbinary('STOR '+filename, f) How can I do an ASCII...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.