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.
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
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");
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();
Console.ReadLine();
Whole code at server, It is Asynchronous way
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);
}
}
Console.WriteLine("Toatal Read" + totread);
}
catch (IOException e)
{ throw; }
}