Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi,
I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#.

There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works.

-Server requests the file.
-Client tells the server the size of the file, and then sends the first 2KB of it.
-The server asks for another chunk of the file -like, send 2KB data, starting from 42649-
-Client sends the chunk
-Server asks for more, and client sends it...
-When the end of file is reached, the client acknowledges the server that transfer is complete.

Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data.

Regards
Posted
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 15:27pm    
Why without looking at code? Don't post your code. Create a small sample which only does essential thing, only what you described above. Use it for testing yourself and post it. It should be really short -- no UI, nothing extra.
--SA

1 solution

If you really want to keep to pure client-server paradigm, server cannot request anything. Server is purely passive. If you want to implement some inversion of control, it's fine but don't call it client and server. You application does not need it.

— Client: I'm sending a file: name is N, size is X bytes;
— Server: confirmed; (starts reading from a network stream, this is blocking operation);
— Client: sends chunk;
— Server: confirmed; (previous chuck is appended to file stream);

— Client: sends chunk; (no need to tell its the last one, server knows that from the size);
— Server: last chunk confirmed; (writes last chunk, closes file stream).

I would advice you use TcpListener/TcpClient.
You can use other levels of networking, see my past answers:
how i can send byte[] to other pc[^],
Communication b/w two Windows applications on LAN.[^].

Most important aspect here is threading. Normally, server part need two separate threads: one is listening for new connections, another one traverses all connected client and perform some application-level protocol by reading/writing from/to each network stream.
See my design sketch in my past answer here:
Multple clients from same port Number[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900