Jump to content
  • Advertisement
Sign in to follow this  
kaiousama

Sending/Receiving Files (C++)

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a setup for an MMORPG right now. The code has been coming together for about 18 months now, and I've already got a fairly well working client and server. However, I have come to one spot I have never really worked with, sending actual files, rather than tiny strings of text. My client is running in Windows, using WinSock2. The server is running on Linux. I see no problem in simply creating an updater.exe, which compares the current client version with the newest version, and simply running through the list of files that have been updated since the version the client is running, downloading them, and running the actual program. However, I have never actually done code to send a file, and was wondering if anybody could point me in the right direction?

Share this post


Link to post
Share on other sites
Advertisement
Imagine a file simply as a stream of packets. In other words, have a binary packet of a small enough size (16K?) and use those to send the file via your basic packet structure (which i assume got laid down already).

If your updater is separated from your game you can just send a length (32 bit should be enough) and then the data (in smaller packets, as you can't just stuff a 2MB buffer down sockets throat).
On the other side glue the packets together again. You may want to include crc checking and/or version data as well. Probably a file packet index comes at hand so you could resume a interrupted download.

Share this post


Link to post
Share on other sites
Exactly. Some dumbwit decided to set the parameter type to char* which makes a lot of people think you can only put text there. It is a pointer type, you can put any data there, binary, text, doesn't matter. If the compiler complains, put a cast to char* there.

Important is that you package it somehow so you can decipher the format on the other end.

For simple file transfer, write the length of the file, and then just put packet after packet (16 kB for example) until all the data is sent. On the other end, you read the length first, so you know how much data to expect. Upon arriving you just write the data to a file.

Share this post


Link to post
Share on other sites
In case you didn't know, that "*char" isnt a string of text, but rather a pointer to an array of bytes. These bytes can hold any data, be it a string of text or raw data like an array of bytes packed with anything you wanted (using memcpy()).

About the patcher, I strongly recommend not using Python. In Python, it is literally 3 or 4 lines of code to download a file off a web site. I learned Python and wrote the autopatcher for my game really easily, in fact the entire Autopatcher program I don't think is more then 50 lines of code. If you insist on using C++ check out hplus's HTTP-GET.

Share this post


Link to post
Share on other sites
So basically, the server would fopen() the file and roughly have:

while (!sendcomplete) {
send(sock,sendbuf,1024);
}

on the server and

while (!recvcomplete) {
recv(sock,recvbuf,1024);
}

With the server in that loop constantly reading in the next 1 kB chunk, and the client writing the 1 kB chunks to a new file?

Share this post


Link to post
Share on other sites
And obviously the size would be larger than 1 kB, but that's just easy for the example. And on the last chunk it would only send the remainder of the file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!