Created
September 9, 2015 18:31
-
-
Save ArpegiusWhooves/117f867dde9696d5e6b0 to your computer and use it in GitHub Desktop.
QDataStream communication.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QTcpServer> | |
#include <QTcpSocket> | |
#include <QDataStream> | |
#include <QScopedPointer> | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
std::vector< int > wektor; | |
QString typ; | |
quint16 rozmiarSampla; | |
quint16 ileSampli; | |
QTcpServer serwer; | |
serwer.listen(QHostAddress::AnyIPv4,9999); | |
if( !serwer.waitForNewConnection(30000) ) | |
return 1; | |
QScopedPointer<QTcpSocket> socket{ serwer.nextPendingConnection() }; | |
if(! socket->waitForReadyRead() ) | |
return 1; | |
QDataStream stream(socket.data()); | |
stream >> typ >> rozmiarSampla >> ileSampli; | |
if( rozmiarSampla == sizeof( wektor[0] ) ) { | |
wektor.resize(ileSampli); | |
stream.readRawData( reinterpret_cast<char*>( wektor.data() ) , rozmiarSampla * ileSampli ); | |
} else { | |
stream.skipRawData(rozmiarSampla * ileSampli); | |
} | |
qDebug() << "Typ: " << typ; | |
qDebug() << "RozmiarSampla: " << rozmiarSampla; | |
qDebug() << "IleSampli: " << ileSampli; | |
for( int v : wektor ) { | |
qDebug() << v; | |
} | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QTcpSocket> | |
#include <QDataStream> | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
std::vector< int > wektor; | |
for(int i = 0; i < 100; ++i ) wektor.push_back(i*i); | |
quint16 rozmiarSampla = sizeof( wektor[0] ); | |
quint16 ileSampli = wektor.size(); | |
QTcpSocket socket; | |
socket.connectToHost("127.0.0.1",9999); | |
if( !socket.waitForConnected() ) | |
return 1; | |
QDataStream stream(&socket); | |
QString typ("Paczka z buforem"); | |
stream << typ << rozmiarSampla << ileSampli; | |
stream.writeRawData( reinterpret_cast<const char*>( wektor.data() ) , rozmiarSampla * ileSampli ); | |
socket.flush(); | |
socket.waitForBytesWritten(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment