qt客户端怎么接收服务器端
⑴ 鍒╃敤qt tcpsocket鎺ュ弹澶ф暟鎹鍖咃纴浣嗘槸鏁版嵁鍙鑳芥帴鍙楄呖5k锛屼箣钖庣殑鏁版嵁涓㈠け涓ラ吨锛屾眰鎸囨暀锛
瀵逛簬澶ф暟鎹鍖 寤鸿杩樻槸鍒嗘典紶阃 涔熷氨鏄浣犺佽嚜宸卞啓涓涓鍏充簬鍙戦佹帴鏀剁殑鍗忚 涓娆℃ц诲彇鎴栬呭彂阃佹槸涓涓涓嶅お濂界殑锅氭硶 铏界劧TCP鍦ㄥ彂阃佽繃绋嬩腑浼氭湁鏁版嵁镄勯獙璇佷粈涔堢殑 浣嗘槸濡傛灉缂揿啿鍖轰竴涓嫔瓙灏辨弧浜 璧峰嬭缮鏄浼氢涪澶遍儴鍒嗘暟鎹镄
寤鸿鍒嗘靛彂阃佽窡鍒嗘垫帴鏀
镞㈢敱瀹㈡埛绔𨱒ュ喅瀹氩彂阃佺殑棰戠巼 (锅囧畾1M镄勬暟鎹鍖 鍒嗘电О8K镄勪竴涓灏忓寘 闾d箞灏卞彉鎴 鍙戦128娆 鍦ㄥ㈡埛绔鎺ユ敹鍒板叾涓镄勪竴涓鍖呭亣瀹氭槸绗琋涓 鍦ㄦ帴鏀跺畬浜 骞朵笖楠岃瘉鏁版嵁鏄姝g‘镄勬椂鍊 闾d箞灏卞湪瀹㈡埛绔鍙戦佺粰链嶅姟绔涓涓璇锋眰镄勬暟鎹鍖 璁╂湇锷″櫒鍙戦佺琋+1涓鏁版嵁鍖 渚濇″勭悊)
⑵ qt socket编程问题,不能接收数据,懂的帮忙解答一下,谢谢了。
接收函数内,一开始就readAll(),已经把数据都读取了,所以后面就没有可读数据了,所以后面不用判断bytesAvaliables()。
接收的时候,不要直接接收到QString中。应该先读取一个长度值(quint16),再根据长度值去读取QByteArray,再把读取到的QByteArray放到QString中。然后再判断bytesAvaliables是否还有剩余。有剩余,就再接收长度值+QByteArray。。。。
⑶ Qt Socket网络编程 服务器端提示QIODevice::read (QTcpSocket): device not open,但是与客户端连接成功
这是因为文件路径不对造成的,解决方法如下:
1、首先pro文件配置:Qt网络功能需要在pro文件增加网络库。
⑷ QT tcp 瀹㈡埛绔鏀朵笉鍒版暟鎹 QTcpSocket 璁板綍
浣跨敤鏂规硶1 缁ф圹QT thread 閲嶅啓run
void DataUnpacket::run( )
{
m_pSocket = new QTcpSocket;
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
QString ip("192.168.112.100");
ushort portData = 5001;
m_pSocket->connectToHost( ip, portData );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//鍒ゆ柇鏄钖﹁繛鎺ユ垚锷
return;
while( m_bRunning )
{
if( m_pSocket->bytesAvailable() > 0)
{
QByteArray arr = m_pSocket->readAll(); //杩欎笉杩涳纴
qDebug() << "recv len =" << arr.length() ;
}
else
{
m_pSocket->write("test",4);
m_pSocket->flush();// 鍙浠ュ彂阃佸嚭铡
QThread::usleep(1000);
}
}
}
鏂规硶浜 movethred 鏁堟灉 涓镙
鏂规硶涓 SocketQThread涓浣跨敤 QtConcurrent::run鍒涘缓绾跨▼锛 杩欎釜浣跨敤QTCreater 璋冭瘯娌℃湁闂棰桡纴杩欎釜涔熸槸澶у潙
镓揿寘浼氩嚭鐜颁笉鑳芥帴鏀跺埌鏁版嵁鎴栬呮病鍙戝嚭铡伙纴 wireshark妫娴嬫湁镞朵细鍑虹幇鐜拌薄鎺ユ敹鍒版暟鎹浣嗘病鍙戝嚭铡
int SocketQThread::InitTest( )
{
int re = 0;
QString ip("192.168.112.5");
ushort portData = 5001;
m_pDataPackage.resize(1);
m_pDataPackage[0] = new DataUnpacket;
if( !m_pDataPackage[0]->InitSocket(ip,portData ) )
re = -1;
return re;
}
void SocketQThread::stratThred()
{
m_running = true;
for(int i=0;i<1;i++)//娴嬭瘯
{
QtConcurrent::run(this, &SocketQThread::recvDataThread, i );
}
}
void SocketQThread::stopThred()
{
m_running = false;
for(int i=0;i<1;i++)//娴嬭瘯
{
m_pDataPackage[0]->CloseFile( );
m_pDataPackage[0]->tcpDisconnect( );
}
}
void SocketQThread::recvDataThread( int ch )//𨱌傚畾
{
DataUnpacket* pPackage = m_pDataPackage[ch];
while( m_running )
{
if( pPackage->SocketBytesAvailable() > 0)
pPackage->RecvData();
else
QThread::usleep(1);
}
}
绫籇ataUnpacket 缁ф圹 Cpackage 濡备笅:
m_Socket涓篊package镄勬垚锻桦彉閲
int DataUnpacket::RecvData( )
{
int re = 0;
QByteArray reArr;
QByteArray arr = m_Socket.readAll();
qDebug() << "recv len =" << arr.length() ;
#if 0
m_Socket.write(arr );
m_Socket.flush();//涓嶅姞鍙戜笉鍑哄幓
#else
QMetaObject::invokeMethod( &m_Socket, std::bind( static_cast< qint64(QTcpSocket::*)(const QByteArray &) >( &QTcpSocket::write ),&m_Socket, arr ));//, Qt::DirectConnection
//鑻ュ姞 Qt::DirectConnection 锛岄渶瑕佹坊锷爉_Socket.flush();/
return re;
}
bool Cpackage::InitSocket(QString& ip, ushort& port )
{
bool tf = true;
m_Socket.connectToHost( ip, port );
m_Socket.waitForConnected();
if( QAbstractSocket::ConnectedState != m_Socket.state() )//鍒ゆ柇鏄钖﹁繛鎺ユ垚锷
tf = false;
return tf;
}
void Cpackage::tcpDisconnect( )
{
if( m_Socket.state() == QAbstractSocket::ConnectedState) //鍏抽棴镞讹纴纭淇濅笌链嶅姟鍣ㄦ柇寮杩炴帴
m_Socket.disconnectFromHost();
}
鏂规硶锲 QtConcurrent::run(this, &DataUnpacket::RecvDataTest); 杩欎釜
void testData::RecvDataTest()
{
m_pSocket = new QTcpSocket(this);
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
QString ip("192.168.112.100");
ushort portData = 5001;
m_pSocket->connectToHost( ip, portData );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//鍒ゆ柇鏄钖﹁繛鎺ユ垚锷
return;
while( m_bRunning )
{
if( m_pSocket->bytesAvailable() > 0)
{
QByteArray arr = m_pSocket->readAll();//杩欎笉杩涳纴
qDebug() << "recv len =" << arr.length() ;
}
else
{
m_pSocket->write("test",4);
m_pSocket->flush();//鍙浠ュ彂阃佸嚭铡
QThread::usleep(1000);
}
}
}
濡备笅渚嫔瓙 鏄鍙浠ョ殑
void testData::RecvDataTest()
{
qDebug() << QThread::currentThread();
QByteArray arr = m_pSocket->readAll();//杩欎笉杩涳纴
qDebug() << "recv len =" << arr.length() ;
m_pSocket->write("test",4);
}
void testData::stratThred( bool tf )
{
if( tf )
start();
else
exit();
}
void testData::run( )
{
qDebug() << "stratThred" << QThread::currentThread();
if( nullptr == m_pSocket )
{
m_pSocket = new QTcpSocket;
connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(RecvDataTest()), Qt::DirectConnection);
//m_pSocket->socketOption(QAbstractSocket::LowDelayOption);
}
m_pSocket->connectToHost( QString("192.168.112.100"), 5001 );
m_pSocket->waitForConnected();
if( QAbstractSocket::ConnectedState != m_pSocket->state() )//鍒ゆ柇鏄钖﹁繛鎺ユ垚锷
return;
//m_pSocket->waitForReadyRead(1000);
exec();
m_pSocket->disconnectFromHost();
}
⑸ qt怎么把windows自带的ftp
配置好服务器之后,便可以使用QT或者其他工具实现文件上传和下载了。
关键代码如下:
void ftp_upload::upload() { ftp = new QFtp(this); ftp->connectToHost("192.168.0.50",21); //连接到服务器,填写你的ip地址,这里默认端口号为21 ftp->login("test","testpasswd"); //登录,填写响应的用户名和密码 //windows自带的ftp服务器,客户端上传文件到服务器 QFile *uploadFile = new QFile("D:/2014.txt");//要上传到服务器的的文件位置:D盘下的2014.txt文档,该文档存在于客户端 QString des = "2014_uploadToServer.txt";//ftp服务器“收到”客户端上传的文档位置:物理路径根目录下的文档2014_upload.txt uploadFile->open(QIODevice::ReadWrite); ftp->put(uploadFile,des); //windows自带的ftp服务器,从服务器下载文件到客户端 QFile *download = new QFile("D:/downloadFromServer.txt"); download->open(QIODevice::ReadWrite); ftp->get("download.txt",download);//把服务器物理路径根目录下的download.txt下载到本地D盘根目录 }