Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void sendfile(FileInfo src,NetworkStream ns)
        {
           
            
            
            network = ns;
            this.data = new byte[packetsize];
            fs = new FileStream(src.FullName, FileMode.Open, FileAccess.Read);
            filesize = fs.Length;
            while (sum < filesize)
            {
                if ((filesize - sum) < packetsize)
                {
                    count = fs.Read(data, 0, (int)(filesize - sum));
                    network.Write(data, 0, (int)(filesize - sum));
                }
                else
                {
                    count = fs.Read(data, 0, data.Length);
                    network.Write(data, 0, data.Length);
                }
                fs.Seek(sum, SeekOrigin.Begin);
                sum += count;
            }
            network.Flush();
           
            
        }



 public void receive(string src, NetworkStream ns,long filesz)
        {
            network = ns;

            filesize = filesz;
         
            fs = new FileStream(src, FileMode.Create, FileAccess.Write);

            
            this.data = new byte[packetsize];
            while (sum < filesize)
                {
                    if ((filesize - sum) < packetsize)
                    {
                        count = network.Read(data, 0, (int)(filesize - sum));
                        fs.Write(data, 0, (int)(filesize - sum));
                    }
                    else
                    {
                        count = network.Read(data, 0, data.Length);
                        fs.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                }
           
            
            }
Posted
Updated 29-Sep-14 6:02am
v2
Comments
Richard MacCutchan 29-Sep-14 13:05pm    
You forgot to initialise sum to some value before starting each loop.
Member 11118232 30-Sep-14 0:37am    
now i have initialized sum=0 before starting of loops.but still facing same problem..and also i have noticed that everytime i send file than the file received is 8 kb less than orignal file..and 8 kb is size of my packet.so i think last packet is lost somewhere

1 solution

Try the following modifications
C++
public void sendfile(FileInfo src,NetworkStream ns)
{
    network = ns;
    this.data = new byte[packetsize];
    fs = new FileStream(src.FullName, FileMode.Open, FileAccess.Read);
    filesize = fs.Length;
    int sum = 0;
    while (sum < filesize)
    {
        int count = filesize - sum;
        if (count > packetsize)
            count = packetsize;
        count = fs.Read(data, 0, count); // read the bytes
        network.Write(data, 0, count); // write only the number of bytes read
        sum += count;
    }
    network.Flush();
}



public void receive(string src, NetworkStream ns,long filesz)
{
    network = ns;
    
    filesize = filesz;
    
    fs = new FileStream(src, FileMode.Create, FileAccess.Write);
    
    
    this.data = new byte[packetsize];
    int sum = 0;
    while (sum < filesize)
    {
        int count = filesize - sum;
        if (count > packetsize)
            count = packetsize;
        count = network.Read(data, 0, count);
        fs.Write(data, 0, count);
        sum += count;
    }
}
 
Share this answer
 
Comments
Member 11118232 30-Sep-14 4:43am    
it worked ..thnku soo much
Richard MacCutchan 30-Sep-14 5:10am    
Happy to help.

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