Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# file socket
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()
        {
            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 28-May-11 14:49pm
Edited 28-May-11 19:34pm
TweakBird17.1K
v2
Rate this: bad
good
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[^]
  Permalink  
Rate this: bad
good
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[^]
  Permalink  
v2
Comments
eldonnadie at 29-May-11 1:39am
   
how do i send it in chunks?
Kim Togo at 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 at 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 at 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 at 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
0 OriginalGriff 190
1 PIEBALDconsult 150
2 BillWoodruff 148
3 DamithSL 140
4 Jochen Arndt 125
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 29 May 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