Click here to Skip to main content
13,045,657 members (53,359 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello, i'm sending a file using tcpListener, It was all going great until i sent a 3MB file, this is basically the code i use

public frmPrincipal()
            CheckForIllegalCrossThreadCalls = false;           
        private void btnEnviar_Click(object sender, EventArgs e)
            TcpClient clienteTCP = new TcpClient(AddressFamily.InterNetwork);
            IPAddress[] listaHost = Dns.GetHostAddresses("");//my IPadress
            clienteTCP.BeginConnect(listaHost, 3000, new AsyncCallback(IniciarConexion), clienteTCP);    
        public void IniciarConexion(IAsyncResult piasResultado)
            TcpClient cliente = piasResultado.AsyncState as TcpClient;
            using (NetworkStream clienteStream = cliente.GetStream())
                ASCIIEncoding encoder = new ASCIIEncoding();
                string arch = "song.mp3";
                byte[] nombre = encoder.GetBytes(arch);
                byte[] ArregloDeArchivo = File.ReadAllBytes(@"C:\Users\Hector\Desktop\" + arch);
                    clienteStream.Write(jeje, 0, jeje.Length);

When i write it sends the file. But, when it is received, the size of the file is 8KB, why this happens ??? =(
Posted 28-May-11 13:49pm
Updated 28-May-11 18:34pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You're treating a binary file as a text file. Youre using the ASCII encoding which will alter the data you're reading and sending. You have to use the BinaryStream class to read those files and get the bytes.

How to read a binary file[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I can see that you are reading all the file in at once. But were do you create the array jeje ?
How do you combine nombre and ArregloDeArchivo to jeje before you call clienteStream.Write ?

On the receive side code, you have to call NetworkStream.Read until you have received all data. Data it not send in one TCP packet. A 3MB file is send in chunks, and you have to read all chunks in the receive side code.

Regards receiving side. Check out TCP/IP Protocol Design: Message Framing[^]
eldonnadie 29-May-11 1:39am
how do i send it in chunks?
Kim Togo 29-May-11 1:58am
You do not send in chunks. If you send a 3MB file, all 3MB cannot be send in one round. Then the TCP driver on Windows split up the data and send it in chunks.
eldonnadie 29-May-11 1:41am
the problem is when i do this line
clienteStream.Write(ArregloDeArchivo, 0,ArregloDeArchivo.Length);
i debugged it and it seemes to send it ok but on the reading side it seams to only read 8kb
Kim Togo 29-May-11 1:59am
You are sending all 3MB to the receiver side. But on the receiver side you have to keep reading on the socket till there is no more data.
Think about NetworkStream as a pipe, you do not know when all data is received. You as the programmer has to build up a protocol that can tell how much data you are sending.
Kim Togo 29-May-11 8:27am
See updated answer.
TCP/IP Protocol Design: Message Framing

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.170713.1 | Last Updated 29 May 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