QTcpSocket成功连接后Client收不到Server的消息
如题,QTcpSocket成功连接后Client收不到Client消息,但Server端能收到Client端发出的消息。
Server端调用sendDatagram("Exit", "Exit");向Client发消息,Client的ReadyRead槽——processPendingDatagrams没有执行。
调试两天了,没找到问题,很着急。。。
还请大佬们帮忙看看,问题在哪? 非常感谢!
代码如下:
//Client.h
class Client : public QMainWindow
{
Q_OBJECT
public:
Client(QWidget *parent = 0);
~Client();
private Q_SLOTS:
void on_actionStart_triggered();
void processPendingDatagrams();
private:
Ui::Client ui;
QTcpSocket * m_socket;
bool m_bConnectStatus;
QString m_strIP;
unsigned int m_uiPort;
};
//Client.cpp
Client::Client(QWidget *parent)
: QMainWindow(parent), m_bConnectStatus(false), m_uiPort(58207),
m_strIP("127.0.0.1")
{
ui.setupUi(this);
this->m_socket = new QTcpSocket(this);
bool bIsOK = true;
bIsOK = QObject::connect(this->m_socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
this->m_socket->connectToHost(m_strIP, m_uiPort, QTcpSocket::ReadWrite); //IP和端口号从配置文件读取
m_bConnectStatus = this->m_socket->waitForConnected();
}
Client::~Client()
{
if (m_socket != NULL)
{
delete m_socket;
m_socket = NULL;
}
}
void Client::processPendingDatagrams() {
QByteArray arr = this->m_socket->readAll(); //读取数据
QDataStream * dst = new QDataStream(&arr, QIODevice::ReadOnly);
QString key;
QString word;
(*dst) >> key >> word;
if (key == "Exit" || word == "Exit")
{
QApplication::quit();
}
}
void Client::sendDatagram(const QString &key, const QString &word) {
QByteArray arr;
QDataStream dst(&arr, QIODevice::ReadWrite);
dst << key << word; /*这里先key后word*/
this->m_socket->write(arr);
bool bIsOK = m_socket->waitForBytesWritten(3000);
}
void Client::on_actionStart_triggered()
{
sendDatagram("Exit", "Exit");
}
//HDServer.h
class HDServer : public QMainWindow
{
Q_OBJECT
public:
HDServer(QWidget *parent = 0);
~HDServer();
bool getConnectionStatus();
void sendDatagram(const QString &key, const QString &word);
private Q_SLOTS:
void on_actionStart_triggered();
void onGetNewConnection();
void processPendingDatagrams();
private:
Ui::HDServerClass ui;
bool m_bConnectStatus;
QString m_strIP;
unsigned int m_uiPort;
QTcpServer * m_server;
QTcpSocket * m_socket;
};
//HDServer.cpp
HDServer::HDServer(QWidget *parent)
: QMainWindow(parent), m_bConnectStatus(false), m_uiPort(58207),
m_strIP("127.0.0.1")
{
ui.setupUi(this);
this->m_server = new QTcpServer(this);
this->m_server->listen(QHostAddress::Any, m_uiPort);
QObject::connect(this->m_server, SIGNAL(newConnection()), this, SLOT(onGetNewConnection()));
}
HDServer::~HDServer()
{
if (m_socket != NULL)
{
delete m_socket;
m_socket = NULL;
}
}
void HDServer::onGetNewConnection()
{
m_socket = m_server->nextPendingConnection();
bool isOK = connect(m_socket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
m_bConnectStatus = isOK;
}
void HDServer::processPendingDatagrams() {
QByteArray arr = this->m_socket->readAll(); //读取数据
QDataStream * dst = new QDataStream(&arr, QIODevice::ReadOnly);
QString key;
QString word;
(*dst) >> key >> word;
if (key == "Exit" || word == "Exit")
{
QApplication::quit();
}
}
void HDServer::sendDatagram(const QString &key, const QString &word) {
if (m_bConnectStatus == false)
return;
QByteArray arr;
QDataStream dst(&arr, QIODevice::ReadWrite);
dst << key << word; /*这里先key后word*/
this->m_socket->write(arr);
bool bIsOK = m_socket->waitForBytesWritten(3000);
}
void HDServer::on_actionStart_triggered()
{
sendDatagram("Exit", "Exit");
}