By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Socket: Send and receive zipped files

P: 3
Hi all,

I have just inherited some code that sends a file from server to client. However, the code only works for text files, and I'm trying to modify it so that it can send and receive zipped files too. But I'm stuck, and don't know how to go about changing it.

The current code looks like this:

Expand|Select|Wrap|Line Numbers
  1. //SERVER
  2.  
  3.         private void SendFileToClient(SocketPacket state, string PFilename)
  4.         {
  5.  
  6.             if (File.Exists(PFilename))
  7.             {
  8.                 FileStream fileStream = File.Open(PFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
  9.                 StreamReader reader = new StreamReader(fileStream);
  10.                 byte[] sendBytes = Encoding.ASCII.GetBytes(reader.ReadToEnd());
  11.                 MessageBox.Show(sendBytes.Length + "");
  12.                 state.m_currentSocket.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
  13.                 fileStream.Close();
  14.                 reader.Close();
  15.             }
  16.         }
  17.  
Expand|Select|Wrap|Line Numbers
  1. //CLIENT
  2.  
  3.         public void OnDataReceived(IAsyncResult asyn)
  4.         {
  5.             try
  6.             {
  7.                 SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
  8.                 int iRx = theSockId.thisSocket.EndReceive(asyn);
  9.                 char[] chars = new char[iRx + 1];
  10.                 System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
  11.                 int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
  12.                 System.String szData = new System.String(chars);
  13.                 theSockId.message += szData;
  14.                 if ((theSockId.message.Length > 0) && ((theSockId.message[0] != '<') ||
  15.                        ((theSockId.message[0] == '<') && (theSockId.message.IndexOf("</message>") > -1))))
  16.                 {
  17.                     DealWithResponse(theSockId);
  18.                 }
  19.                 else
  20.                     WaitForData(theSockId);
  21.             }
  22.             catch (ObjectDisposedException)
  23.             {
  24.                 System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
  25.             }
  26.             catch (SocketException se)
  27.             {
  28.                 MessageBox.Show(se.Message);
  29.             }
  30.         }
  31.  
  32.         public void DealWithResponse(SocketPacket theSockId)
  33.         {
  34.             if (theSockId.message != null)
  35.             {
  36.                 FileStream fileStream = File.Open(theSockId.theGame.KeyFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  37.                 StreamWriter writer = new StreamWriter(fileStream);
  38.                 writer.Write(theSockId.message);
  39.                 writer.Close();
  40.                 fileStream.Close();
  41.             }
  42.         }
  43.  
Let me know if you need more of the code. Thank you for taking your time to look at this.
Jan 9 '09 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
You are trying to use a TEXT decoder on the received side. This will scramble binary files.
Consider altering code to remove that section
Jan 9 '09 #2

P: 3
Thanks for your reply. Can you please give me a few hints as to how to start?
Jan 10 '09 #3

Plater
Expert 5K+
P: 7,872
Revmoe everything related to converting the byte[] from the Sockets to strings
(the decoder and etc)
See what breaks, and patch it up
Jan 12 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.