Author Topic: How to save and load files from the server?  (Read 4284 times)

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
How to save and load files from the server?
« on: December 24, 2013, 09:11:22 AM »
Hi,

In the documentation I saw I would have to use RequestSaveFile . But how do I use this?

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #1 on: December 24, 2013, 09:57:05 AM »
Okay, I found out. I used:
  1. BinaryWriter saveFile = TNManager.BeginSend(Packet.RequestSaveFile);
  2.                 saveFile.Write("test.txt");
  3.  
  4.                 saveFile.Write( (int)new FileStream("test.txt" , FileMode.Open).Length);
  5.                 TNManager.EndSend();
But now the file arrives really strange, the original content of the file was just a simple message, but it arrives in all kinds of strange characters on the server.

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #2 on: December 24, 2013, 10:00:16 AM »

And I have another question, how do I download a file from the server?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to save and load files from the server?
« Reply #3 on: December 24, 2013, 01:49:56 PM »
You forgot to include the actual data. From the packet's description:
  1.         /// <summary>
  2.         /// Save the specified data.
  3.         /// string: Filename.
  4.         /// int32: Size of the data in bytes.
  5.         /// Arbitrary amount of data follows.
  6.         /// </summary>
  7.  
  8.         RequestSaveFile,
It expects a string for the filename, which you have... then the length of the file, which you have... then it expects bytes to follow it -- byte[] with the length you specified. You never pass this information.

Furthermore, as it expects a byte array, you need to pass it just that -- byte array.

Here is an example of doing it properly:
  1.                 FileStream fs = new FileStream("test.txt", FileMode.Open);
  2.                 long end = fs.Seek(0, SeekOrigin.End);
  3.                 fs.Seek(0, SeekOrigin.Begin);
  4.                 byte[] bytes = new byte[end];
  5.                 fs.Read(bytes, 0, (int)end);
  6.                 fs.Close();
  7.                
  8.                 BinaryWriter writer = TNManager.BeginSend(Packet.RequestSaveFile);
  9.                 writer.Write("test.txt");
  10.                 writer.Write(bytes.Length);
  11.                 writer.Write(bytes);
  12.                 TNManager.EndSend();

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #4 on: December 24, 2013, 07:29:03 PM »
Thanks alot for the help!

The test file now successfully arrives at the server! But I have another problem, when I try to download the file it does create the file properly, but the file is filled with all kinds of random characters, things like Euro signs, question marks and even stuff that's not on the keyboard! This my download code is :

  1. void loadFile(Packet response, BinaryReader reader, IPEndPoint source){
  2.                 if(response == Packet.ResponseLoadFile){
  3.  
  4.                         string FileName = reader.ReadString();
  5.                         int length = reader.ReadInt32();
  6.                         byte[] data = reader.ReadBytes(length);
  7.  
  8.                         string d = System.Text.Encoding.UTF8.GetString(data);
  9.                         Debug.Log("Downloading file: " + FileName);
  10.  
  11.                         Debug.Log(length);
  12.                         Debug.Log(d);
  13.                        
  14.  
  15.                         File.WriteAllBytes(FileName, data);
  16.                 }
  17.         }

And in the Awake function I have this:
  1. TNManager.SetPacketHandler(Packet.ResponseLoadFile, loadFile);

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to save and load files from the server?
« Reply #5 on: December 24, 2013, 11:44:36 PM »
UTF8.GetString assumes that 'data' contains a UTF-8 encoded string. The example I posted doesn't do this. It simply writes the binary file as-is. You need to use UTF-8 on both sides for this to work.

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #6 on: December 25, 2013, 07:29:28 AM »
The file is saved as UTF 8 at at the server

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #7 on: December 25, 2013, 07:44:46 AM »
I now created  a debug message that display's the length property for the packet. When I send the file, it says 17. But when I try to download the same file, reader.ReadInt32() gives me 29.

I also noticed that that number is always 29, even when I change the contents of the text file.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to save and load files from the server?
« Reply #8 on: December 25, 2013, 03:37:55 PM »
RequestSaveFile and RequestLoadFile are handled in TNGameServer.cs line 876 and 883, respectively. They're also handled in TNTcpLobbyServer.cs line 294 and 301, depending on whether you're storing the data on the lobby server or the game server. I advise checking to see what actually arrives on the server.

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #9 on: December 25, 2013, 03:50:56 PM »
The file arrives properly at the server. I can open it and the contents are the same as on the original version. But as soon as I try to download it again I get the problems.

Raj1v

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: How to save and load files from the server?
« Reply #10 on: December 25, 2013, 04:11:45 PM »
Okay, I fixed it. Just had to restart the server.