Click here to Skip to main content
15,888,111 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to get DataGridView Checkbox Checked or not ! Pin
Ashrafuddin7-Jun-11 19:15
Ashrafuddin7-Jun-11 19:15 
Questionkeeping sql connection open Pin
teknolog1236-Apr-10 0:31
teknolog1236-Apr-10 0:31 
AnswerRe: keeping sql connection open Pin
Pete O'Hanlon6-Apr-10 1:10
mvePete O'Hanlon6-Apr-10 1:10 
GeneralRe: keeping sql connection open Pin
teknolog1236-Apr-10 1:56
teknolog1236-Apr-10 1:56 
GeneralRe: keeping sql connection open Pin
Not Active6-Apr-10 2:31
mentorNot Active6-Apr-10 2:31 
GeneralRe: keeping sql connection open Pin
Pete O'Hanlon6-Apr-10 3:22
mvePete O'Hanlon6-Apr-10 3:22 
AnswerRe: keeping sql connection open Pin
PIEBALDconsult6-Apr-10 4:35
mvePIEBALDconsult6-Apr-10 4:35 
QuestionSending Large Files in C# using TCP Client/Server [modified] Pin
EvanSaunders5-Apr-10 23:43
EvanSaunders5-Apr-10 23:43 
Hi All

I've been struggling for a while now trying to solve this issue of sending larger files across TCP client/server program in c#. Been Googling for days with partial solutions to the problem. Every time I think that I have found a solution, something bombs out. Frown | :(

Brief Explanation:
The Client sends a requests, the Server performs an operation on a file, and then sends the file to Client, which then receives this file.

For smaller files, the normal method of changing file into byte array works only for smaller files.
I have also tried converting the file to base64string and then send it that way, but also doesn't work.

Could someone please help me solve the problem of sending larger files using TCP?

These are the 3 main techniques that seemed promising:
i.
inFile = new System.IO.FileStream(theFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
   binaryData = new Byte[inFile.Length];
   long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
   inFile.Close();
   //Convert the binary input into Base64 UUEncoded output.
   string base64String;
   base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
   byte[] buffer = encoder.GetBytes(base64String);
   serverStream = tcpClient.GetStream();
   serverStream.Write(buffer, 0, buffer.Length);
   serverStream.Flush();


ii.
byte[] fileNameByte = Encoding.ASCII.GetBytes(theFileName);
      if (fileNameByte.Length >; 850 * 1024)
      {
         Console.Out.WriteLine("File is too big");
         return;
      }
      byte[] fileData = File.ReadAllBytes(theFileName);
      byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
      byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
      fileNameLen.CopyTo(clientData, 0);
      fileNameByte.CopyTo(clientData, 4);
      fileData.CopyTo(clientData, 4 + fileNameByte.Length);
      tcpClient.Client.Send(clientData);


iii.
const int BufferSize = 1024;
     byte[] SendingBuffer = null;
     FileStream Fs = new FileStream(theFileName, FileMode.Open, FileAccess.Read);
     int NoOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(Fs.Length)/Convert.ToDouble(BufferSize)));
     int TotalLength = (int)Fs.Length, CurrentPacketLength, counter = 0;
     for (int i = 0; i < NoOfPackets; i++)
     {
        if (TotalLength > BufferSize)
        {
           CurrentPacketLength = BufferSize;
           TotalLength = TotalLength - CurrentPacketLength;
        }
        else
        {
           CurrentPacketLength = TotalLength;
           SendingBuffer = new byte[CurrentPacketLength];
           Fs.Read(SendingBuffer, 0, CurrentPacketLength);
           serverStream.Write(SendingBuffer, 0, (int)SendingBuffer.Length);
        }
        Fs.Close();
     }   

__________________________________

I am not sure if the problem might lie on the client side when receiving the file, so I have listed that too (in the same order as above):
i.
//get the file
     byte[] getFile = new byte[4096];
     bytesRead = 0;      
     bytesRead = clientStream.Read(message, 0, 4096);
     string base64String = encoder.GetString(message, 0, bytesRead);
     byte[] binaryData = Convert.FromBase64String(base64String);
     FileStream fs = new FileStream(theFileName, FileMode.Create, FileAccess.ReadWrite);
     BinaryWriter bw = new BinaryWriter(fs);
     bw.Write(binaryData);
     bw.Close();


ii.
byte[] clientData = new byte[1024 * 22000];
     int receivedBytesLen = m_tcpclient.Client.Receive(clientData);
     int fileNameLen = BitConverter.ToInt32(clientData, 0);
     string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
     BinaryWriter bWrite = new BinaryWriter(File.Open(theFileName, FileMode.Append)); ;
     bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);


iii.
string SaveFileName = string.Empty;
     SaveFileName = theFileName;
     int RecBytes;
     const int BufferSize = 1024;
     byte[] RecData = new byte[BufferSize];
     if (SaveFileName != string.Empty)
     {
        int totalrecbytes = 0;
        FileStream Fs = new FileStream(SaveFileName, FileMode.OpenOrCreate, FileAccess.Write);
        while ((RecBytes = m_networkstream.Read(RecData, 0, RecData.Length)) > 0)
        {
           Fs.Write(RecData, 0, RecBytes);
           totalrecbytes += RecBytes;
        }
        Fs.Close();
     }
________________________

Thanks for the help in advance! Laugh | :laugh: Thumbs Up | :thumbsup:
modified on Tuesday, April 6, 2010 8:39 AM

AnswerRe: Sending Large Files in C# using TCP Client/Server Pin
Garth J Lancaster6-Apr-10 0:43
professionalGarth J Lancaster6-Apr-10 0:43 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 1:26
EvanSaunders6-Apr-10 1:26 
AnswerRe: Sending Large Files in C# using TCP Client/Server Pin
Garth J Lancaster6-Apr-10 0:55
professionalGarth J Lancaster6-Apr-10 0:55 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 1:43
EvanSaunders6-Apr-10 1:43 
AnswerRe: Sending Large Files in C# using TCP Client/Server Pin
Rod Kemp6-Apr-10 1:41
Rod Kemp6-Apr-10 1:41 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 1:56
EvanSaunders6-Apr-10 1:56 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
Rod Kemp6-Apr-10 2:14
Rod Kemp6-Apr-10 2:14 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 2:20
EvanSaunders6-Apr-10 2:20 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
Rod Kemp6-Apr-10 2:27
Rod Kemp6-Apr-10 2:27 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 2:34
EvanSaunders6-Apr-10 2:34 
GeneralRe: Sending Large Files in C# using TCP Client/Server [modified] Pin
Rod Kemp6-Apr-10 2:48
Rod Kemp6-Apr-10 2:48 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 3:03
EvanSaunders6-Apr-10 3:03 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
Rod Kemp6-Apr-10 3:14
Rod Kemp6-Apr-10 3:14 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 3:27
EvanSaunders6-Apr-10 3:27 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
Rod Kemp6-Apr-10 4:32
Rod Kemp6-Apr-10 4:32 
GeneralRe: Sending Large Files in C# using TCP Client/Server Pin
EvanSaunders6-Apr-10 20:30
EvanSaunders6-Apr-10 20:30 
AnswerRe: Sending Large Files in C# using TCP Client/Server Pin
Luc Pattyn6-Apr-10 2:31
sitebuilderLuc Pattyn6-Apr-10 2:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.