Click here to Skip to main content
12,997,038 members (128,987 online)
Rate this:
Please Sign up or sign in to vote.
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.

Posted 1-Jun-11 9:05am
SAKryukov 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.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^].


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 1 Jun 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100