をストリーミングサーバー+データへのクライアントから複数のデータ型を送信する、私は接続を行うとの間で前後にいくつかのデータを送信するために管理クライアントとサーバー。 クライアントは、サーバ(文字列、int型、ファイルやリアルタイムオーディオストリーム)へのデータの多くの種類を送信し、私のサーバーは、単一のデータ入力SLOT(readyReadを())impliment以来:Qtは、私がQTcpServerとQTcpSocketを使用して<strong>クライアント/サーバ</strong>ベース<strong>のQt</strong>アプリケーションを、持っている
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
私はドン」この受信したすべてのデータを区別して、サーバー内の適切な機能をそれぞれ呼び出す方法を知っています。
Example (in the server):
- if I receive string => call function showData(QString data);
- if I receive file => call function saveFile(QFile file);
- if I receive audio stream => play audio stream
- ...
SERVER:CLIENT
void Server::newClientConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
//...
}
void Server::readyRead()
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
if (clientSocket == 0) {
return;
}
QDataStream in(clientSocket);
if (sizeMessageClient == 0)
{
if (clientSocket->bytesAvailable() < (int)sizeof(quint16)){
return;
}
in >> sizeMessageClient;
}
if (clientSocket->bytesAvailable() < sizeMessageClient) {
return;
}
sizeMessageClient = 0;
in >> data;
/*
I don't know the type of the received data !!
- if I receive string => call function showData(QString data);
- if I receive file => call function saveFile(QFile file);
- if I receive audio stream => play audio stream
- ...
*/
}
:私は完全なソリューションを捜しているわけではない
Client::Client()
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
sizeMessageServer = 0;
}
void Client::readyRead()
{
QDataStream in(socket);
if (sizeMessageServer == 0)
{
if (socket->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
in >> sizeMessageServer;
}
if (socket->bytesAvailable() < sizeMessageServer) {
return;
}
int messageReceived;
in >> messageReceived;
messageReceived = static_cast<int>(messageReceived);
sizeMessageServer = 0;
switch(messageReceived)
{
case 1:
qDebug() << "send a File";
sendFile();
break;
case 2:
qDebug() << "send a string data";
sendStringData();
break;
case 3:
qDebug() << "stream audio to the server";
streamAudioToServer();
break;
case n:
// ...
}
}
、私が探していますすべてが正しい方向にいくつかのガイダンスです。
あなたが発明する(または既存の使用)を使用すると、送信データの種類を伝えることができます*プロトコル*をする必要があるように思え。 –
私はどのようにこれが起こることができるかの例をネット上で見つけることができません。 –
最低でも、タイプと値をメッセージにパッケージ化し、タイプの受信側のスイッチでパッケージ化することができます。 –