I made a Client and Server program based on the code found in this thread.

Here's the code of my program

Client (Sender of file)

//Before calling this function,
        //I send the file size to the server first
        private void fileToServer(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                TcpClient cli = tcpServer; // i use tcpServer to connect to the server, hence the name
                NetworkStream ns = cli.GetStream();

                CopyStreamToStream(fs, ns, null);
                fs.CopyTo(ns);
                ns.Flush();                  
            }
            catch
            {
                MessageBox.Show("An error occurred while uploading the file to the server");
            }
        }


        public static void CopyStreamToStream(Stream source, Stream destination,
                                   Action<Stream, Stream, Exception> completed)
        {
            byte[] buffer = new byte[0x1000];
            int read;
            try
            {
                while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    destination.Write(buffer, 0, read);
                }
                if (completed != null) completed(source, destination, null);
            }
            catch (Exception exc)
            {
                if (completed != null) completed(source, destination, exc);
            }
        }

Server (File receiver)

//Code fragment from the server's function that receives the file
                FileStream ms = new FileStream(tmpFile, FileMode.Create, FileAccess.Write);
                do
                {
                    int szToRead = s.Available;
                    byte[] buffer = new byte[szToRead];

                    int szRead = s.Receive(buffer, szToRead, SocketFlags.None);
                    if (szRead > 0)
                    {
                        ms.Write(buffer, 0, szRead);
                    }
                }
                while (ms.length < fileSize); //fileSize is an int32 variable which contains the file's size in bytes (the value is sent by the client program)

The loop above doesn't terminate (gets stuck at line 8) so I experimented with the code. Using the code below, I found out that not all the bytes sent by the client program is being received by the server program.

Client

//Before calling this function,
        //I send the file size to the server first
        private void fileToServer(string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                TcpClient cli = tcpServer; // i use tcpServer to connect to the server, hence the name
                NetworkStream ns = cli.GetStream();

                CopyStreamToStream(fs, ns, null);
                fs.CopyTo(ns);
                ns.Flush();
                ns.Close(); //I don't close the network stream in my actual program because I need the client program to stay connected to the server.           
            }
            catch
            {
                MessageBox.Show("An error occurred while uploading the file to the server");
            }
        }


        public static void CopyStreamToStream(Stream source, Stream destination,
                             Action<Stream, Stream, Exception> completed)
        {
            byte[] buffer = new byte[0x1000];
            int read;
            try
            {
                while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    destination.Write(buffer, 0, read);
                }
                if (completed != null) completed(source, destination, null);
            }
            catch (Exception exc)
            {
                if (completed != null) completed(source, destination, exc);
            }
        }

Server

FileStream ms = new FileStream(tmpFile, FileMode.Create, FileAccess.Write);
                do
                {
                    int szToRead = s.Available;
                    byte[] buffer = new byte[szToRead];

                    int szRead = s.Receive(buffer, szToRead, SocketFlags.None);
                    if (szRead > 0)
                    {
                        ms.Write(buffer, 0, szRead);
                    }
                }
                while (SocketConnected(s));

                long lengthOfFile = ms.Length;
                ms.Close();

                if (lengthOfFile == filesize)
                {
                   MessageBox.Show( "File received successfully." );
                }
                else
                {
                    File.Delete(tmpFile);
                    MessageBox.Show( "Failed to receive file." );
                }

I wonder what's wrong. I am using the code of the application from the thread I mentioned above. The client program from that application sends files successfully and the server program receives it successfully.

UPDATE:
I have found out something new. Please take a look at this.

Here's how the client program sends a file to the server program

while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
destination.Write(buffer, 0, read);
Console.WriteLine(read.ToString());
}

//read is an int
//source is a FileStream
//buffer is a byte[]
//destination is a NetworkStream

Here's how the server program receives the file from the client

do
{
szRead = s.Receive(buffer, buffer.Length, SocketFlags.None);
Console.WriteLine(szRead.ToString());
if (szRead > 0)
{
ms.Write(buffer, 0, szRead);
}
}
while (ms.Length < fileSize);

// szRead is an int
// s is a Socket
// buffer is a byte[]
// ms is a FileStream
// fileSize is an int

Here's the console output when I run my program:

(Client side)

4096
4096
4096
4096
4096
4096
4096
2560

(Server side)

4096
4096
4096
4096
4096
3079
4096
2560

What is the problem here?

This article has been dead for over six months. Start a new discussion instead.