Re: file transfer with sockets
- From: "David" <nospam@xxxxxxxxxx>
- Date: Wed, 16 May 2007 17:27:39 -0400
I've been reading up on streams and file I/O to make this come together. Two
questions:
1) should I just send each file chunk consecutively or wait for a
confirmation from client for each chunk before sending the next?
2) my current structure is a simple command/response setup. I send command
to server, server interprets command, executes it, then simply returns
results of the command... I did this using asynch calls for both client and
server. Is it ok to use the synch calls now from within my asynch callbacks
for the file transfer? a read a book that said something about not mixing
the asynch and synch calls? starving the threadpool? I'm in over my head but
making progress :)
"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:%23Owd0i9lHHA.2060@xxxxxxxxxxxxxxxxxxxxxxx
David,
Yes, you would use a filestream. You would make calls to Read on the
filestream instance, reading say, 1KB of bytes (or any other number you
feel is appropriate depending on the size of the files you will typically
send) at a time, and then passing the byte array that you read the
contents into to the Send method on the socket.
You could load the entire contents of the file into one big byte array,
but that wouldn't be too efficient, and would gobble up memory for large
files (hence the reading in chunks).
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"David" <nospam@xxxxxxxxxx> wrote in message
news:eIZX$W9lHHA.3760@xxxxxxxxxxxxxxxxxxxxxxx
Hi Nicholas,
"Once you have the file on the server side, you can just
read the bytes from the file in chunks, and then pass them to the Send
method, sending the bytes over the stream. Of course, your client has to
read them, and it has to read only the number of bytes that you are going
to
send (which it already knows because you sent the length already)."
I don't know what that would actually look like on sending and receiving
end? However I get the concept.
I'm embarrassed to say but I really haven't used .net's streaming IO
model yet... I used vb4 a long time ago, my programming activities now
are mostly scripting for automation related to network administration.
I'm just now really trying to get into c#/.net/network programming. I get
that all .net IO uses streams, and that I would use a stream (or
fileStream?) to read the file from disk on server, assuming reading it
into a byte buffer, then using the socket to send that byte buffer. But
how do I, for example, control and keep track of the 'chunks', you
mentioned? would it be one call to socket.send after accumulating all the
file chunks or a loop resulting in multiple calls to socket.send? I
assume a loop is more likely so it would work on small and large files...
unfortunately I am still at the beginning learning stages of this and
lack some crucial fundamentals. But I can pick it up real quick because
of past programming experience and some admin scripting.
"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message news:%23KJZYx8lHHA.5048@xxxxxxxxxxxxxxxxxxxxxxx
David,
You don't have to use the NetworkStream object, you can use a Socket
object just fine. Once you have the file on the server side, you can
just read the bytes from the file in chunks, and then pass them to the
Send method, sending the bytes over the stream. Of course, your client
has to read them, and it has to read only the number of bytes that you
are going to send (which it already knows because you sent the length
already).
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"David" <nospam@xxxxxxxxxx> wrote in message
news:%23Tn5Mu8lHHA.3736@xxxxxxxxxxxxxxxxxxxxxxx
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 example to follow
for using a tcp socket to transfer files between client/server,
server/client. Both server and client are my program so I'm not looking
for how to implement an FTP client, or how to download a file from a web
server via http etc... Protocol between my client/server is a simple
command based structure and I intended on it going something like this
in english:
this example is client to server scenario but I'll implement on both
ends.. so client/server can be server/client etc..
1) client requests a file from server
2) server verifies file exists and sends back an 'ok' with the files
size (or message saying no file exists)
3) client receives file's size and sends the 'ok' to start the transfer
4) server sends file / client receives file
5) client sends 'ok' after receive is done
I've given these steps here to be clear and specific on the part I'm
trying to learn how to do. I can do all steps here fine except step 4,
the actual send/receive of the file. Help on that would be greatly
appreciated.
I'm not even sure if those specific steps will stay like that, I only
used them to narrow down the area of my question. Further, I'm looking
to do this with the sockets directly, using socket.send and
socket.receive, (or their async methods), as I've read elseware that is
the most effecient way, as opposed to using a networkstream object.
Aside from help on the specific task I'm trying to learn, any comments
on which technique is a good choice are also welcome (socket.send
etc... networkstream object.. and I honestly just saw a
'socket.sendfile' method in VS help before finishing drafting this
post... I'm posting anyway since I learn a lot from this forum, and
even if one of the higher level classes is a better choice, I want to
learn the socket way first.)
.
- Follow-Ups:
- Re: file transfer with sockets
- From: Peter Duniho
- Re: file transfer with sockets
- References:
- file transfer with sockets
- From: David
- Re: file transfer with sockets
- From: Nicholas Paldino [.NET/C# MVP]
- Re: file transfer with sockets
- From: David
- Re: file transfer with sockets
- From: Nicholas Paldino [.NET/C# MVP]
- file transfer with sockets
- Prev by Date: Re: RGB into a picturebox
- Next by Date: Re: ComboBox AutoComplete Issue
- Previous by thread: Re: file transfer with sockets
- Next by thread: Re: file transfer with sockets
- Index(es):
Relevant Pages
|