Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm reading a huge byte of around 3824726 bytes. I've tried a lot of functions for reading the whole bytes. It is reading exactly 3816534 bytes and the while loop is going away some where. Remaining 8192 bytes are not being read and also there is no exception. It reads till exactly 3816534 and then when on while loop goes away somewhere. Please some one help and tell what may be the problem here. I have tried a lot but the same thing is happening in different functions.
C#
public static void ReadFully(NetworkStream stream, int initialLength)
    {
        if (initialLength < 1)
        {
            initialLength = 32768;
        }

        byte[] buffer = new byte[initialLength];
        int chunk;
        try
        {
            int read = -1;
            int totread = 0;

            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                totread += read;
                Console.WriteLine("Toatal Read" + totread);
                string filePath = "C:\\Foo.txt";
                StreamWriter objWriter;
                using (objWriter = File.AppendText(filePath))
                {
                    objWriter.WriteLine(totread);
                    objWriter.Flush();
                    objWriter.Close();
                }
            }
            Console.WriteLine("Toatal Read" + totread);
        }
        catch (Exception ex)
        { throw ex; }
    }

Client Side Sending bytes to server
C#
byte[] fileA;


        IPAddress ipAd = IPAddress.Parse("IpAddress");
        TcpClient client = new TcpClient(ipAd.ToString(), Port);
        NetworkStream stream = client.GetStream();

        StreamReader reader = new StreamReader("D:/Users/Public/Pictures/Sample        Pictures/06250_2.jpg");
        //StreamReader reader = new StreamReader("D:/Users/703132799/Desktop/Foo.txt");
        string data = reader.ReadToEnd();
        reader.Close();
        fileA = System.Text.Encoding.ASCII.GetBytes(data);
        int length = 0;
        length = fileA.Length;
        Console.WriteLine("Sending Buffer Length-" + length);
        stream.Write(fileA, 0, fileA.Length);
        stream.Flush();
        //Thread.Sleep(10000);
        Console.ReadLine();

Whole code at server, It is Asynchronous way
C#
static void Main(string[] args)
    {
        StartServer();
    }

    private static TcpListener _listener;
    public static void StartServer()
    {

        IPAddress localIPAddress = IPAddress.Parse("IPAddress");
        IPEndPoint ipLocal = new IPEndPoint(localIPAddress, Port);
        _listener = new TcpListener(ipLocal);
        _listener.Start();
        WaitForClientConnect();
    }

    private static void WaitForClientConnect()
    {
        object obj = new object();
        _listener.BeginAcceptTcpClient(new System.AsyncCallback(OnClientConnect), obj);
        Console.ReadLine();
    }

    private static void OnClientConnect(IAsyncResult asyn)
    {
        try
        {
            TcpClient clientSocket = default(TcpClient);
            clientSocket = _listener.EndAcceptTcpClient(asyn);
            HandleClientRequest clientReq = new HandleClientRequest(clientSocket);
            clientReq.StartClient();
        }
        catch (Exception ex)
        {
            throw ex;
        }

        WaitForClientConnect();
    }
}

public class HandleClientRequest
{
    TcpClient _clientSocket;
    NetworkStream _networkStream = null;

    public HandleClientRequest(TcpClient clientConnected)
    {
        this._clientSocket = clientConnected;
    }

    public void StartClient()
    {
        _networkStream = _clientSocket.GetStream();
        WaitForRequest();
    }

    public void WaitForRequest()
    {
        byte[] buffer = new byte[_clientSocket.ReceiveBufferSize];

        _networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
    }

    private void ReadCallback(IAsyncResult result)
    {
        string sRecMsgAsciiWithHex = string.Empty;
        NetworkStream networkStream = _clientSocket.GetStream();
        ReadFully(networkStream, 65536);
    }

    public static void ReadFully(NetworkStream stream, int initialLength)
    {
        if (initialLength < 1)
        {
            initialLength = 32768;
        }

        byte[] buffer = new byte[initialLength];
        int chunk;
        try
        {
            int read = -1;
            int totread = 0;
            using (var fileStream = File.OpenWrite("C:\\Foo.txt"))
            {

                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    totread += read;
                    Console.WriteLine("Toatal Read" + totread);
                    fileStream.Write(buffer, 0, buffer.Length);
                    //string filePath = "C:\\Foo.txt";
                    //StreamWriter objWriter;
                    //using (objWriter = File.AppendText(filePath))
                    //{
                    //    objWriter.WriteLine(totread);
                    //    objWriter.Flush();
                    //    objWriter.Close();
                    //}
                }
            }
            Console.WriteLine("Toatal Read" + totread);
        }
        catch (IOException e)
        { throw; }
    }
Posted
Updated 9-Jun-14 1:20am
v2
Comments
Priyatam Upadrasta 9-Jun-14 6:23am    
While sending the data from client I'm using reader but i've changed it to stream.Write now. But then also exactly while reading the last 8192 bytes it is going away or I can say just lost from the while loop.No exception I'm getting. Just it trips away while reading the last set of bytes.
gggustafson 9-Jun-14 8:06am    
Must you send all of the data at one time? Or can you stream the client to server transfer?
Priyatam Upadrasta 9-Jun-14 8:45am    
No need to send the data totally at once.
gggustafson 10-Jun-14 9:24am    
are you still active on this thread?

1 solution


Remember, the server has no idea how much data is being sent. And, if the client disconnects after sending the last message but before the server has read the last message, the connection will be lost and it will appear that you have lost data.



Here's a thought on what you can do:



  • Use a synchronous client/server.
  • Set up a message protocol:

    • Have the client send a "total bytes" message like
      <TOTAL>total-number-of-bytes-to-be-sent</TOTAL>
    • Wait for the server to respond with a message that contains total-number-of-bytes-to-be-sent. This indicates that the server is ready to receive bytes.
    • When the server has responded, start reading the input file in, say, 1024 bytes, at a time. Encapsulate each "chunk" of data in a "data" message like:
      <DATA>chunk-of-data</DATA>
      and send the data to the server. You do not need to wait for the server.
    • After you have sent the last chunk of data, even if it's only a partial message, send a "finished" message like
      <FINISHED>total-number-of-bytes-to-be-sent</FINISHED>
    • Wait for the server to respond with a message that contains total-number-of-bytes-to-be-sent. This indicates that the server received all the bytes.
    • Now you can shut down the connection on the client side.



You have very little control over the size of the buffers, so you may need to insure that two or more data buffers are properly identified in a message.



I can supply code that uses this method.



Hope that helps.

 
Share this answer
 

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