Reputation: 334
This is Homework so just some conceptual tips woudl be nice.
At the Moment im creating a client server project, the client will send a message to the server(this part works great), but then the server sends a message back to the client and this is where I begin to have issues, I have created a separate class in QT called Connection Management in my client that deals with sending a receiving messages, this class is used by my Controller class, which handle the Application logic of all my UI Pages, now when my controller, sends a message to the server a message is sent back, atm I have it as an echo, my ConnectionManagement class handles Incomming messages with the readyRead() signal, the problem here is when someone is using my client and say press the next button, the next button slot will be called, and wait until after execution of NextButtonPressed() or w/e. until the read ready notices, there is something being sent back to it, i tried having a read i can call manually but it doesn't appear to work any suggestions?
a small bit of my code:
ConnectionManager.cpp
ConnectionManager::ConnectionManager(QObject *parent)
: QTcpServer(parent)
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyread()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
socket -> connectToHost("192.168.0.119",60000);
}
QString ConnectionManager::getMessage()
{
//sleep(1);
while (inMessage == "")
{
//my attempt at calling read() manually it will read nothing
QByteArray data = socket->readAll();
//just checkign what i am getting my server will immediately return a trivial
//string i.e "carly, santos, andrew, bob"
std::cout << data.data() << "+ 8" << std::endl;
inMessage = data;
}
return inMessage;
}
void ConnectionManager::sendMessage(QString &message)
{
socket -> write(message.toUtf8());
}
//my socket for reading whenever something is sent?
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
}
ok now my controller.cpp
void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)
//sleep(7);
std::cout << "1234" << std::endl;
QString in = myConnection -> getMessage(); //<- gets nothing -.-
std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();
Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");
delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);
}
the message is received here after this function executes, i remember form using C-Style TCP/IP connection the read would wait until there is data to continue, but it seems for be cool with getting nothing here?
Upvotes: 0
Views: 2790
Reputation: 48216
you do your send message and then immediately try to get the responce, this won't work because you need to return to the event loop before anything can be written or read
split up your Controller::openTALogin
method into 2
void Controller::openTALogin()
{
//TODO send message to Server
QString buffer = Fill::fillBufferState("Client", "TA");
//std::cout << buffer.toUtf8().data() << std::endl;
myConnection->sendMessage(buffer); // <- this works great =)
}
void Controller::gotResponce(QString in)
{
std::cout << in.toUtf8().data() << "+ 12" << std::endl;
//TODO recieve List from server
//ForNow
QLinkedList<QString> *testList = new QLinkedList<QString>();
Fill::fillLinkedList(testList, in);
//testList -> push_front("jabba:5:hut:1");
//testList -> push_front("blind:3:mice:2");
//testList -> push_front("eat:5:day:3");
//testList -> push_front("hello:4:goodbye:4");
delete userWindow;
taLoginWindow = new TALoginWindow(this, testList);
taLoginWindow -> show();
testList -> clear();
delete(testList);
}
and have the second part called in your readyRead:
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
emit recievedMessage(QString(inMessage)); // using signal here
}
Upvotes: 1