QT+BOOST::ASIO+TCP File Transfer

Source: Internet
Author: User

Client:

void Qt_boost::p bsendfileclicked ()
{
QString filename = Ui.lefilename->text ();
Qbytearray ba = Filename.tolatin1 ();
char * pfilename = Ba.data ();
Std::ifstream IFS (Pfilename, ios::in|ios::binary|ios::ate);
Long size = Ifs.tellg ();
Char *p = new Char[size];
IFS.SEEKG (0, Ios::beg);
Ifs.read (p, size);
Ifs.close ();

Tcp::resolver _resolver (*ioservice);
Tcp::resolver::query query (TCP::V4 (), "127.0.0.1", "10800");
Tcp::resolver::iterator _iterator = _resolver.resolve (query);
Tcp::socket _socket = Tcp::socket (*ioservice);
Boost::asio::connect (_socket, _iterator);
Char psize[4];
int *pint = (int *) psize;
*pint = size;
Boost::asio::write (_socket, Boost::asio::buffer (psize, sizeof (psize)));
Boost::asio::read (_socket, Boost::asio::buffer (psize, sizeof (psize)));
Boost::asio::write (_socket, Boost::asio::buffer (p, size));
Delete[] p;
}

Call Qt qfiledialog::getopenfilename to get the file name and save it in the Ui.lefilename control. Use Ifstream to load the file, get the file size, and write the contents of the file to a dynamic array. send file size to server, After the service-side postback message is read, the file contents are sent.

Service side:

void Session::start ()
{
File transfers receive file size first
Socket_.async_read_some (Boost::asio::buffer (Data_, Max_length),
Boost::bind (&session::handle_read, this,
Boost::asio::p Laceholders::error,
Boost::asio::p laceholders::bytes_transferred));
}

void Session::handle_read (const boost::system::error_code& error, size_t bytes_transferred)
{
if (!error)
{
File transfer receives file size and postback file size information notifies client to continue sending file contents
int *p = (int *) Data_;
FileSize = *p;
Socket_.async_write_some (Boost::asio::buffer (Data_, bytes_transferred),
Boost::bind (&session::handle_write, this, Boost::asio::p laceholders::error)); }
Else
{
Delete this;
}
}

void Session::handle_write (const boost::system::error_code &error)
{
if (!error)
{

Receive file contents
p = new Char[filesize];
Boost::asio::async_read (socket_, Boost::asio::buffer (p, filesize),
Boost::bind (&session::handle_readfile, this, Boost::asio::p laceholders::error));
}
Else
{
Delete this;
}
}

void Session::handle_readfile (const boost::system::error_code &error)
{
if (!error)
{

The file contents are saved to disk after receiving
Std::ofstream ofile ("C:\\zzz.txt");
Ofile.write (P, filesize);
Ofile.close ();
}
Else
Delete this;
}

Issue: Only text files can be transferred, and if you want to transfer any type of file, modify the Ifstream constructor parameters.

Improved: In order to transfer any type of file, the file suffix name is placed in the first 20 bytes sent to the server, the service side resolved the suffix stitching into the file name.

Client-side Package code:

QString filename = Ui.lefilename->text ();
Qfileinfo finfo (filename);
Qbytearray ba = Filename.tolatin1 ();
char * pfilename = Ba.data ();
Processing suffixes
QString Fileext = Finfo.suffix ();
Qbytearray Baext = Fileext.tolatin1 ();
Char *pfileext = Baext.data ();
Std::ifstream IFS (Pfilename, ios::in|ios::binary|ios::ate);
Long size = Ifs.tellg ();
Char *p = new Char[size + 20];//allocates a file suffix in the first 20 bytes of the same array as the file size
memset (p, 0, size + 20);
memcpy (P, Pfileext, Baext.size ());
IFS.SEEKG (0, Ios::beg); Place the current position at 0
Ifs.read (p + A, size); Reads the contents of the file into the array, pending sending
Ifs.close (); Close File

Service-side Receive code:

void Session::handle_readfile (const boost::system::error_code &error)
{
if (!error)
{
Char fileext[20] = {0};
strcpy (Fileext, p);
Char filename[255] = "c:\\zzz.";
strcat (filename, fileext);
Std::ofstream ofile (filename, std::ios::out|std::ios::binary|std::ios::ate);
Ofile.write (P + A, filesize);
Ofile.close ();
}
Else
Delete this;
}

The problem of improving the characters of Chinese file names:

The client reads the file Stream section code instead using the Qfile class implementation:

QFile file (filename);
File.Open (qiodevice::readonly);
Long size = File.size ();
Char *p = new Char[size + 20];
memset (p, 0, size + 20);
memcpy (P, Pfileext, Baext.size ());
File.seek (0);
File.read (p + A, size);
File.close ();

http://blog.csdn.net/henreash/article/details/7534804

Qt+boost::asio+tcp File Transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.