2uzhan.com
Advertisement
Now Place:2uzhan.com » (LAN) Server doesn't receive last byte from client program

(LAN) Server doesn't receive last byte from client program

Software Development @ February 17, 2012   Views:0

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.

--------------Solutions-------------

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?

Please ZIP your project and attach it here. There is code involved aside from what you posted.

Looks like it could be a packet loss issue.

thanks for the responses, but i solved the problem already. :D

here it is:

http://stackoverflow.com/questions/9345721/socket-issue-some-bytes-sent-become-incomplete-when-received

What I did was send a string to the client program from the server program before sending bytes from the client to the server using streamWriter.writeLine() and streamWriter.flush()

Yes, WPF. The learning curve is steep, but what amazing things you can do with it!
Look at this hexagon window for instance. Serves no purpose in a business environment ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...

Hey there,

I am trying to create a register code. So basicly im making a quiz and every time a user starts up my program he/she needs to register (making ...

Tags:lan, socket
© 2018 2uzhan.com Contact