Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# file Networking TCP/IP
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 1-Jun-11 10:05am
Comments
SAKryukov at 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

Rate this: bad
good
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[^].
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Jun 2011
Copyright © CodeProject, 1999-2014
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