Click here to Skip to main content
15,896,369 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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

C#
public frmPrincipal()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;           
        }
        private void btnEnviar_Click(object sender, EventArgs e)
        {
            TcpClient clienteTCP = new TcpClient(AddressFamily.InterNetwork);
            IPAddress[] listaHost = Dns.GetHostAddresses("xxx.xxx.xxx.xxx");//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);
                    clienteStream.Flush();
...


When i write it sends the file. But, when it is received, the size of the file is 8KB, why this happens ??? =(
Posted
Updated 28-May-11 18:34pm
v2

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[^]
 
Share this answer
 
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[^]
 
Share this answer
 
v2
Comments
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.
http://en.wikipedia.org/wiki/Ethernet_frame
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)



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