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盤根目錄 }