We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

ASYNC Socket, How to use the BeginSendFile?

Medium Priority
2,317 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Carlos VillegasFull Stack .NET Developer

Commented:
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
Carlos VillegasFull Stack .NET Developer

Commented:
MSDN sorry...

Author

Commented:
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.

Author

Commented:
Anyone else really have tried this method?
Carlos VillegasFull Stack .NET Developer

Commented:
Yes Im done an example for you, wait a moment...
Full Stack .NET Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Carlos VillegasFull Stack .NET Developer

Commented:
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.

Author

Commented:
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.

Author

Commented:
and thanks for that link...
Carlos VillegasFull Stack .NET Developer

Commented:
Yes, SendFile is just a plus ;) and I agreed with open a new connection for the file transfer.
Carlos VillegasFull Stack .NET Developer

Commented:
Glad to help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.