Ska

Ska

  • NA
  • 5
  • 0

Socket: Send and receive zipped files

Jan 26 2009 5:57 AM
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:
  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.         }

  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.  
Like I said before, this code works fine for text files. But it doesn't work for non-text files. What should I change to make it so that it works for both text and non-text files?
Let me know if you need more of the code. Thank you for taking your time to look at this.


Answers (5)