Author Topic: Download file to client from server?  (Read 5188 times)

sirrus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Download file to client from server?
« on: April 19, 2014, 10:51:51 PM »
I am trying to download a file from the server to the client.  I have searched the forums endlessly but only found three threads that remotely cover this issue and I couldn't resolve the issue.

I can successfully Save a file TO the server using this code:


  1.         if (Input.GetKey(KeyCode.UpArrow))
  2.         {
  3.  
  4.             BinaryWriter writer = TNManager.BeginSend(Packet.RequestSaveFile);
  5.             writer.Write("test.txt");
  6.             writer.Write(15);
  7.             writer.Write("Testing");
  8.  
  9.             TNManager.EndSend();
  10.  
  11.         }

But I cannot download files from the server using the following code:

  1.         if (Input.GetKey(KeyCode.DownArrow))
  2.         {
  3.            
  4.                 BinaryWriter downloadedFile = TNManager.BeginSend(Packet.RequestLoadFile);
  5.                 downloadedFile.Write("test.txt");
  6.                 downloadedFile.Write(15);
  7.                 TNManager.EndSend();
  8.  
  9.         }

What am I doing wrong?

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Download file to client from server?
« Reply #1 on: April 19, 2014, 11:33:38 PM »
What is that 15 you're sending with the request?

RequestSaveFile expects to have a string first -- which you have -- then the length of the bytes to follow (call it "X") -- which instead you have some '15' magic number -- followed by actual bytes[X] to save. You don't have X bytes. You have a string instead.

Similarly, RequestLoadFile expects to have a string first -- which you have. And that's it. Not sure what that magic 15 is doing there again. When in doubt, check the TNPacket.cs file. Every packet has a description explaining what needs to be sent.
  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,
  9.  
  10.         /// <summary>
  11.         /// Load the requested data that was saved previously.
  12.         /// string: Filename.
  13.         /// </summary>
  14.  
  15.         RequestLoadFile,

sirrus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Download file to client from server?
« Reply #2 on: April 19, 2014, 11:46:44 PM »
I arbitrarily put "15" as the length of bytes, as I don't quite understand how I know how many bytes the file actually is.  I don't really understand why you would not want to save the full length of bytes either.  Can you provide example code, perhaps one that will just use the full byte size for length and actual? 


As for RequestLoadFile, even when I remove "downloadedFile.Write(15);" it still seems to do nothing.

When I call RequestSaveFile, on the server in the folder that houses the .exe/server, "test.txt" is created.
I would expect when I call RequestLoadFile, on the client's computer, in the folder that houses the game's .exe, "test.txt" would be downloaded to.  With my current code, no file appears.  If that is not correct, how do I do that?  All I want is to download a file from the default folder on the server to the default folder on the connected client.

Thank you, I am still very new at network code.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Download file to client from server?
« Reply #3 on: April 20, 2014, 05:34:34 PM »
You need to save your file properly first:
  1. void SaveFile (string filename, byte[] bytes)
  2. {
  3.     BinaryWriter writer = TNManager.BeginSend(Packet.RequestSaveFile);
  4.     writer.Write(filename);
  5.     writer.Write(bytes.Length);
  6.     writer.Write(bytes);
  7.     TNManager.EndSend();
  8. }
Loading the file is the same, but you don't pass bytes.Length or bytes. Just the filename. The file doesn't get loaded right away. It takes some time for the packet to arrive on the server. You will get the ResponseLoadFile back. You need to register a listener for that packet.

sirrus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Download file to client from server?
« Reply #4 on: April 22, 2014, 08:41:28 PM »
I'm actually more confused with Loading than Savings.  The saving code was just as a test, but what I really need is to download a file.

What do I do after I call the request?
  1.  BinaryWriter downloadedFile = TNManager.BeginSend(Packet.RequestLoadFile);
  2.                 downloadedFile.Write("level.txt");
  3.                 TNManager.EndSend();

I looked through TNManager and TNGameServer to try to understand ResponseLoadFile but I couldn't figure it out.

Could you provide full sample code from start to finish for downloading a file from the server to the client?
After you do the above code, how do you register the requesting client as the listener and transfer the actual file?


I basically need a full example of how to download a file (in this case, level.txt) from the root folder of the Server to the root folder of the requesting Client.

Thank you!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Download file to client from server?
« Reply #5 on: April 23, 2014, 07:49:31 AM »
  1. using UnityEngine;
  2. using TNet;
  3.  
  4. public class Downloader : MonoBehaviour
  5. {
  6.     void Start()
  7.     {
  8.         TNManager.SetPacketHandler(Packet.ResponseLoadFile, OnLoadFile);
  9.     }
  10.  
  11.     void OnLoadFile (Packet response, BinaryReader reader, IPEndPoint source)
  12.     {
  13.         // 'reader' is your binary reader, same as if you actually loaded a local file.
  14.         // Proceed to read it as you would with something you loaded from disk.
  15.         // For example: Debug.Log(reader.ReadString());
  16.     }
  17.  
  18.     public void LoadFile (string filename)
  19.     {
  20.         BinaryWriter writer = TNManager.BeginSend(Packet.RequestLoadFile);
  21.         writer.Write(filename);
  22.         TNManager.EndSend();
  23.     }
  24.  
  25.     public void SaveFile (string filename, byte[] bytes)
  26.     {
  27.         BinaryWriter writer = TNManager.BeginSend(Packet.RequestSaveFile);
  28.         writer.Write(filename);
  29.         writer.Write(bytes.Length);
  30.         writer.Write(bytes);
  31.         TNManager.EndSend();
  32.     }
  33. }

sirrus

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Download file to client from server?
« Reply #6 on: April 23, 2014, 06:52:19 PM »
Thank you..

Would it be possible to include the code for the Binary Reader as well (with the subsequent Binary Writer code that would then write that from local memory to a file local, which I assume is how it works)?  I'm quite new to C#, so have not done local disk loading/saving.  I've read about BinaryReaders, but have a hard time getting the syntax right.

I'm a bit in over my head doing network programming, without handling more fundamental programming prior, but the last main thing I need from TNet is properly downloading a file from the server.  If I can get help with that, I should be fine for the rest for the type of project I'm doing.

Thanks for your patience!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Download file to client from server?
« Reply #7 on: April 24, 2014, 01:36:59 AM »
Sorry but Binary Reader is not TNet. It's basic .NET. Just google it and you will find plenty of examples to work with.