qt+boost::asio+tcp鏂囦欢浼犺緭
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了qt+boost::asio+tcp鏂囦欢浼犺緭相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
瀹㈡埛绔細
void qt_boost::pbSendFileClicked()
{
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;
}
璋冪敤QT鐨凲FileDialog::getOpenFileName鑾峰彇鏂囦欢鍚嶇О,淇濆瓨鍦╱i.leFileName鎺т欢涓?浣跨敤ifstream鍔犺浇鏂囦欢,鑾峰彇鏂囦欢澶у皬骞跺皢鏂囦欢鍐呭鍐欏叆鍒板姩鎬佹暟缁勪腑.鍚戞湇鍔$鍙戦€佹枃浠跺ぇ灏?璇诲彇鏈嶅姟绔洖鍙戜俊鎭悗,鍙戦€佹枃浠跺唴瀹?
鏈嶅姟绔?
void session::start()
{
//鏂囦欢浼犺緭 棣栧厛鎺ユ敹鏂囦欢澶у皬
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void session::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
if(!error)
{
//鏂囦欢浼犺緭 鎺ユ敹鏂囦欢澶у皬 骞跺洖鍙戞枃浠跺ぇ灏忎俊鎭?閫氱煡瀹㈡埛绔户缁彂閫佹枃浠跺唴瀹?br /> int *p = (int *)data_;
filesize = *p;
socket_.async_write_some(boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this, boost::asio::placeholders::error)); }
else
{
delete this;
}
}
void session::handle_write(const boost::system::error_code &error)
{
if(!error)
{
//鎺ユ敹鏂囦欢鍐呭
p = new char[filesize];
boost::asio::async_read(socket_, boost::asio::buffer(p, filesize),
boost::bind(&session::handle_readfile, this, boost::asio::placeholders::error));
}
else
{
delete this;
}
}
void session::handle_readfile(const boost::system::error_code &error)
{
if(!error)
{
//鎺ユ敹瀹屾瘯灏嗘枃浠跺唴瀹瑰瓨鍏ョ鐩?br /> std::ofstream ofile("C:\\zzz.txt");
ofile.write(p, filesize);
ofile.close();
}
else
delete this;
}
闂:鍙兘浼犺緭鏂囨湰鏂囦欢,濡傛灉瑕佷紶杈撲换浣曠被鍨嬫枃浠?鍙互淇敼ifstream鐨勬瀯閫犲嚱鏁板弬鏁?
鏀硅繘锛氫负浜嗗彲浼犺緭浠绘剰绫诲瀷鐨勬枃浠?灏嗘枃浠跺悗缂€鍚嶇О鏀惧湪鍓?0涓瓧鑺備腑鍙戦€佺粰鏈嶅姟绔?鏈嶅姟绔В鏋愬嚭鍚庣紑鎷兼帴鍒版枃浠跺悕绉颁腑.
瀹㈡埛绔嫾鍖呬唬鐮?
QString filename = ui.leFileName->text();
QFileInfo finfo(filename);
QByteArray ba = filename.toLatin1();
char * pfilename = ba.data();
//澶勭悊鍚庣紑
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];//鍒嗛厤涓€涓笌鏂囦欢澶у皬鐩稿悓鐨勬暟缁?鍓?0涓瓧鑺傛斁鏂囦欢鍚庣紑
memset(p, 0, size + 20);
memcpy(p, pfileext, baext.size());
ifs.seekg(0, ios::beg); //灏嗗綋鍓嶄綅缃疆涓?
ifs.read(p + 20, size); //灏嗘枃浠跺唴瀹硅鍏ュ埌鏁扮粍涓?寰呭彂閫?br /> ifs.close(); //鍏抽棴鏂囦欢
鏈嶅姟绔帴鏀朵唬鐮?
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 + 20, filesize);
ofile.close();
}
else
delete this;
}
鏀硅繘涓枃鏂囦欢鍚嶇殑涔辩爜闂锛?/p>
瀹㈡埛绔鍙栨枃浠舵祦閮ㄥ垎浠g爜鏀逛负浣跨敤QFile绫诲疄鐜帮細
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 + 20, size);
file.close();
http://blog.csdn.net/henreash/article/details/7534804
以上是关于qt+boost::asio+tcp鏂囦欢浼犺緭的主要内容,如果未能解决你的问题,请参考以下文章
odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢
如何接受boost :: asio :: ssl :: stream 作为boost :: asio :: ip :: tcp :: socket类型的参数