Re: file transfer with sockets



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.)










.



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Is this the correct way to send a Bitmap over sockets?
    ... clickpoints) from the server to the client. ... indicating that the Socket was closed. ... "logical" stream ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I stop a Winsock from buffering characters?
    ... it's applied at the OS level to the socket. ... Stream s = client.GetStream; ... from the client code and have the server see it right away. ... first character of the client send. ...
    (microsoft.public.windowsce.embedded)
  • Re: file transfer with sockets
    ... confirmation from client for each chunk before sending the next? ... to server, server interprets command, executes it, then simply returns ... files (hence the reading in chunks). ... sending the bytes over the stream. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)

Loading