这是家庭作业所以只是一些概念性提示会很好。
在创建客户端服务器项目的时刻,客户端将向服务器发送消息(这部分工作正常),但服务器将消息发送回客户端,这是我开始遇到问题的地方,我在QT中创建了一个名为Connection Management的单独类,用于处理发送接收消息,这个类由我的Controller类使用,它处理我所有UI页面的应用程序逻辑,现在当我的控制器发送消息时到服务器发回一条消息,atm我把它作为echo,我的ConnectionManagement类用readyRead()信号处理Incomming消息,这里的问题是当有人使用我的客户端并说按下一个按钮,下一个按钮将调用slot,并等待执行NextButtonPressed()或w / e之后。直到读取就绪通知,有一些东西被发送回它,我尝试阅读我可以手动调用但它似乎没有任何建议吗?
我的一小部分代码:
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;
}
现在我的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);
}
在此函数执行后收到消息,我记得使用C-Style TCP / IP连接的表单,读取会等到有数据继续,但看起来很酷,什么都没有?
答案 0 :(得分:1)
你做你的发送消息然后立即尝试获得响应,这将无法工作,因为你需要在任何可以写入或读取任何内容之前返回事件循环
将您的Controller::openTALogin
方法拆分为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);
}
并在readyRead中调用第二部分:
void ConnectionManager::readyread()
{
QByteArray data = socket->readAll();
inMessage = data;
std::cout << data.data() << "+ 1" << std::endl;
emit recievedMessage(QString(inMessage)); // using signal here
}