求助:这个程序究竟哪里错了

AAA20090987 2011-02-22 12:33:38
下面这个程序的功能是:
客户端先向服务器发送“1”,然后服务器向客户端发出询问("Are you a boy"),
用户如果选close,就结束通信
选yes,则向服务器发送2,然后在服务器界面上显示"He is a boy",服务器向客户端发送"Oh, you are a boy"
选no,则向服务器发送3,然后在服务器界面上显示"She is a girl",服务器向客户端发送"Oh, you are a girl"
最后,客户端弹出一个消息窗口,显示"Oh, you are a boy"或"Oh, you are a girl",结束。

但不知为什么,最后并没有弹出那个消息窗口,客户端似乎没有收到信息。
这是为什么呢?应该怎么改正?

客户端:
//trycli.h

#ifndef TRYCLI_H_
#define TRYCLI_H_

#include <QtGui/QWidget>
#include <QtNetwork/QTcpSocket>
#include <QtGui/QPushButton>
#include <QtCore/QString>
#include <QtCore/QVector>
#include <QtCore/QTimer>
#include "ui_client.h"

class client : public QWidget
{
Q_OBJECT
private:
QPushButton *start;
QTcpSocket *tcpclient;
quint16 blockSize;
QString sendString;
QString readString;

public:
client();
~client();
void Question(const QString &str);

public slots:
void displayError(QAbstractSocket::SocketError socketError);
void newConnect();
void clickedStartButton();
void readMessage();
void sendMessage();

};

#endif


//trycli.cpp

#include "trycli.h"
#include <QtGui/QMessageBox>
#include <QtGui/QHBoxLayout>
#include <QtEvents>

client::client()
{
setWindowTitle("client");
resize(100, 100);

start = new QPushButton("start");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(start);
setLayout(layout);

tcpclient = new QTcpSocket(this);
connect(start, SIGNAL(clicked()), this, SLOT(clickedStartButton()));
connect(tcpclient, SIGNAL(connected()), this, SLOT(sendMessage()));
connect(tcpclient, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpclient, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}

client::~client()
{

}

void client::newConnect()
{
blockSize = 0;
tcpclient->abort();
tcpclient->connectToHost("127.0.0.1", 8888);
tcpclient->waitForReadyRead();
}

void client::sendMessage()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);

out << (quint16)0;
out << sendString;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

tcpclient->write(block);
}

void client::readMessage()
{
QDataStream in(tcpclient);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0)
{
if (tcpclient->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpclient->bytesAvailable() < blockSize)
return;

in >> readString;
if(readString[0] == 'A')
Question(readString);
else
QMessageBox::about(NULL, "AA", readString);
tcpclient->disconnectFromHost();
}

void client::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The host was not found. Please check the "
"host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
QMessageBox::information(this, tr("Fortune Client"),
tr("The connection was refused by the peer. "
"Make sure the fortune server is running, "
"and check that the host name and port "
"settings are correct."));
break;
default:
QMessageBox::information(this, tr("Fortune Client"),
tr("The following error occurred: %1.")
.arg(tcpclient->errorString()));
}
}

void client::clickedStartButton()
{
sendString = "1";
newConnect();
}

void client::Question(const QString &str)
{
QMessageBox::StandardButton rb = QMessageBox::question(NULL,
"question", str,
QMessageBox::Yes | QMessageBox::No | QMessageBox::Close,
QMessageBox::Yes);
if(QMessageBox::Yes == rb)
{
sendString = "2";
newConnect();
}
else if(QMessageBox::No == rb)
{
sendString = "3";
newConnect();
}
}


//main.cpp

#include "trycli.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
client w;
w.show();
return app.exec();
}
...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AAA20090987 2011-02-22
  • 打赏
  • 举报
回复
怎么C++的代码标签不能用了,搞到我要用JAVA的标签。。。

服务器端:
//tryser.h

#ifndef TRYSER_H_
#define TRYSER_H_

#include <QtGui/QWidget>
#include <QtGui/QLabel>
#include <QtCore/QString>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QtCore/QTimer>
#include <QtCore/QVector>

class server : public QWidget
{
Q_OBJECT
private:
QLabel *label;
QString nowWord;
QTcpServer *tcpserver;
QTcpSocket *tcpsocket;
quint16 blockSize;
QString sendString;
public:
server();
~server();
void sendMessage();
public slots:
void readMessage();
void acceptConnection();
};

#endif


//tryser.cpp

#include "tryser.h"
#include <QtGui/QMessageBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>

server::server()
{
label = new QLabel;
setWindowTitle("server");
label->setText("emtpy");
resize(200, 100);

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(label);
setLayout(mainLayout);

tcpserver = new QTcpServer;
tcpsocket = new QTcpSocket;
if( !tcpserver->listen(QHostAddress::Any, 8888) )
{
QMessageBox::warning(NULL, "error", "can not connect");
close();
return;
}
blockSize = 0;
connect(tcpserver, SIGNAL(newConnection()), this, SLOT(acceptConnection()));

}

server::~server()
{

}

void server::acceptConnection()
{
tcpsocket = tcpserver->nextPendingConnection();
connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}

void server::readMessage()
{
QDataStream in(tcpsocket);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0)
{
if (tcpsocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpsocket->bytesAvailable() < blockSize)
return;
QString word;
in >> word;

if("1" == word)
{
sendString = "Are you a boy?";
sendMessage();
}
else if("2" == word)
{
label->setText("He is a boy");
sendString = "Oh, you are a boy!";
sendMessage();
}
else if("3" == word)
{
label->setText("She is a girl");
sendString = "Oh, you are a girl!";
sendMessage();
}
blockSize = 0;
}

void server::sendMessage()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);

out << (quint16)0;
out << sendString;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

tcpsocket->write(block);
}



//main.cpp

#include "tryser.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
server w;
w.show();
return a.exec();
}
AAA20090987 2011-02-22
  • 打赏
  • 举报
回复
解决了,非常感谢你的帮助
  • 打赏
  • 举报
回复
估计问题出在客户端,看看这个函数:
[code=C]/C++
void client::newConnect()
{
blockSize = 0;
tcpclient->abort();
tcpclient->connectToHost("127.0.0.1", 8888);//3
tcpclient->waitForReadyRead(); //4
}
[/code]

第3句连接成功后会产生一个connected信号,但这个信号的产生并不一定是立即的,假设这个信号还没产生,你的发送动作也必然没有产生,第4句紧接着阻塞等待读取,会有东西读到吗?
这个阻塞是必须的,但放的位置不对,应该放到啥位置? 应该放到你成功发送了数据后的地方,这时候阻塞等待服务器返回数据就应该是正确的了
成功发送数据后的地方在哪?
应该在sendMessage函数的最后,是不?
建议楼主试试,把客户端的newConnect()中的最后一条语句tcpclient->waitForReadyRead();移动到sendMessage()函数的最后.

16,216

社区成员

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

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