Link to home
Start Free TrialLog in
Avatar of Christian de Bellefeuille
Christian de BellefeuilleFlag for Canada

asked on

ASYNC Socket, How to use the BeginSendFile?

I've written a class using Sockets.  I've noticed that there's a BeginSendFile method in the sockets.  The only code sample i've found was on MSDN, and it doesn't show much thing... (it show the code on the sender side only.   Nothing about the receiver.  I don't know where the file is stored, how it's received, etc).

Is there anyone who could help me with some code:
to send a file
to receive file
to show the progression of the file transfer

Thanks for your help
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hi, well I think that the MSND documentation is very complete in this case (BeginSendFile method):
http://msdn.microsoft.com/en-us/library/ac1zy13z.aspx

But I dont think that you can know the progress by using the BeginSendFile (maybe making a new class that inherit from System.Net.Sockets.Socket?) method, instead you must use the BeginSend method to be able to know the send progress.
http://msdn.microsoft.com/en-us/library/7h44aee9.aspx
MSDN sorry...
Avatar of Christian de Bellefeuille

ASKER

You probably just have checked the MSDN without trying this sample right?

Because this example throw an error (i'll translate because my compiler is in french):
"Impossible to cast object of type 'System.Net.Sockets.TransmitFileOverlappedAsyncResult' in type 'System.Net.Sockets.Socket'"

And as i said... this method is really not well documented because it doesn't explain how it's received, just how it's sent.
Anyone else really have tried this method?
Yes Im done an example for you, wait a moment...
ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, the progression of the file transfer can be done on the server side (receiver) but first you need to implement a header or something that send to the client the total file lenght before start transfer the file data, with that you can calculate the progress, on the client side (sender), I think that you need to use another method instead of SendFile (maybe the Send method(a, b, c)), in that way you can know who much data has been sent.

Have you seen the System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener classes? this are more simplified to use.
Ok, so the BeginSendFile still send data the same way that i would do with BeginSend.

Of course i've seen TCPClient and TCPListener, but as you might also notice, there's no SendFile or anything that look like that in these classes.  We have developped our communication class based on the Socket, and it's working at 100%.  

It's just that there was a "BeginSendFile", i was wondering what could be done with it, and if it was doing a good job.   But as far as you have shown me, all what it does is opening the file for me, read it, then it's basically a BeginSend of bunch of data.   I was expecting more of it...

So thanks for your help.  I might use it to save a couple of lines of code.  But my guess is that i'll create an additionnal socket connection just for the file transfer, because our application is a VNC like app and sending a large file could simply take over all the communication for a while.

and thanks for that link...
Yes, SendFile is just a plus ;) and I agreed with open a new connection for the file transfer.
Glad to help!