QTcpSocket成功连接后Client收不到Server的消息

liu3235751 2017-11-06 02:34:53
如题,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");
}


...全文
1193 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liu3235751 2017-11-14
  • 打赏
  • 举报
回复
程序在PC上运行,32位程序,windows7 64位系统,单网卡。 试过UDP可以(说明网络正常?),但是和第三方软件定了协议使用TCP通信。
沐秋之阳 2017-11-09
  • 打赏
  • 举报
回复
从代码上看,没什么问题;特地试了一下,正常;不知楼主是什么环境?虚拟机?linux?网络环境,如多网卡等 readyRead()信号,如果响应不到槽函数,只能说明是网络环境造成的,同2楼,换成其他通信方式,如udp试试! 实在不行,可以试试系统地socket试试。。。
dongchangc 2017-11-07
  • 打赏
  • 举报
回复
我上次调试也有这个原因 应该是获取ip地址的方式不对 有两种获取ip地址的方式 linux下跟windows下不同 是试试 应该可以 //获取ip地址 QString MainWindow::getIP(){ #if 0 QList<QHostAddress> list = QNetworkInterface::allAddresses(); foreach (QHostAddress address, list) { if(address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress::Null && address !=QHostAddress::LocalHost) { if(address.toString().contains("127.0.")){ continue; } return address.toString(); } } #else QString localHostName = QHostInfo::localHostName();//主机名 QHostInfo info = QHostInfo::fromName(localHostName); // info.addresses(); foreach (QHostAddress address, info.addresses()) { if(address.protocol() == QAbstractSocket::IPv4Protocol ) return address.toString(); } #endif
张小飞Official 2017-11-06
  • 打赏
  • 举报
回复
试试QUdpSocket????udp可以的话说明程序没问题,因为tcp不可靠。 个人网站www.qtdoc.cn
liu3235751 2017-11-06
  • 打赏
  • 举报
回复
补充说明,Client的构造函数执行完毕时,bIsOK为true, m_bConnectStatus为true。 说明connect连接成功、connectToHost连接成功。

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧