QSslSocketsを使用して通信するクライアントプログラムとサーバープログラムがあります。サーバープログラムでは、クライアントが正常に接続された場合に文字列"Snameadded"
を送信します。サーバー上のユーザーのリストも送信しますが、別のメッセージとして、2つの異なるブロックを作成し、別々に書き込みますが、クライアントによって1つとして読み取られます
これはサーバー上のコードです:
void server::processMess(QString message)
{
//Message type legend
// [number]*** [name]*** [message]
// number = 1, joining server for first time, rest is name
// number = 2, split by "***", retrive name, receiving name, and rest is message
// number = 3, disconnecting from server, resy is name
bool successfullyConnected = false;
QStringList temp = message.split("***");
int num = temp.at(0).toInt();
qDebug() << num;
qDebug() << "size of socket list:" << myClientSockets.size();
QString name = temp.at(1);
if(num == 1)
{
QString name = temp.at(1);
if(clientList.contains(name))
{
QByteArray block;
block.append("Enameinvalid");
QSslSocket *clientConnection = myClientSockets.at(myClientSockets.size() - 1);
myClientSockets.pop_back();
clientConnection->write(block);
\
}
else
{
clientList.append(name);
QByteArray block;
block.append("Snameadded");
successfullyConnected = true;
QSslSocket *clientConnection = myClientSockets.at(myClientSockets.size() - 1);
qDebug() << "Message created to send back to client: " << QString(block);
clientConnection->write(block);
/*
ClientThread *thisClientThread = new ClientThread();
thisClientThread->setClientConnection(clientConnection);
myClientThreads.push_back(thisClientThread);
connect(thisClientThread, SIGNAL(messageReceived(QString)), this, SLOT(processMess(QString)));
thisClientThread->start();*/
}
}
else if(num == 2)
{
//Direct message correctly
QString receiver = temp.at(2);
QSslSocket* receiverConnection = myClientSockets.at(clientList.indexOf(receiver));
QByteArray block;
block.append(message);
receiverConnection->write(block);
}
else if(num == 3)
{
//Disconnect client
updateServer(name + "disconnected");
int position = clientList.indexOf(name);
clientList.removeAt(position);
myClientSockets.removeAt(position);
}
else
{
qDebug() << "Should never reach here";
}
if(successfullyConnected)
{
QByteArray block;
block.append("UsersOnServer:");
for(int i = 0; i < clientList.size(); ++i)
{
block.append(clientList.at(i));
if(i < clientList.size() - 1)
{
block.append("***");
}
}
for(int i = 0; i < myClientSockets.size(); ++i)
{
QSslSocket *receiverConnection = myClientSockets.at(i);
receiverConnection->write(block);
}
}
qDebug() << "Messaged received from client: " + message;
updateServer("Messaged received from client: " + message);
/*QSslSocket* thisConnection = myClientSockets.pop_back();
QByteArray incoming = thisConnection->readAll();
QString message = QString(incoming);
qDebug() << "Message received from client: " + message;
return;*/
/*QByteArray in = tempSocket->readAll();
QString message = QString(in);
qDebug() << "Messaged received from client: " + message;
updateServer(message);
*/
}
これが私のクライアント側のコードです:secureSocket-> write(block);
if(secureSocket->waitForReadyRead(1000))
{
QByteArray in = secureSocket->readAll();
QString serverMess = QString(in);
if(QString::compare(serverMess, "Snameadded", Qt::CaseSensitive) == 0)
{
returnVal = true;
qDebug() << "added to server";
connect(secureSocket, SIGNAL(readyRead()), this, SLOT(receiveMess()));
}
else if(QString::compare(serverMess, "Enameinvalid", Qt::CaseSensitive) == 0)
{
returnVal = false;
qDebug() << "username already taken";
}
qDebug() << "*************Message from server:************** 1" << serverMess;
}