|
1Qter豆
在网上(http://blog.csdn.net/hulifangjiayou/article/details/47785763)找到一个类似的demo 想自己改成服务器端——在收到新的连接后通过以下代码,点击YES后再继续传输
void Widget::acceptConnection()
{
QMessageBox::StandardButton choice;
choice = QMessageBox::question(this, tr("hint"),tr("content"),QMessageBox::Yes | QMessageBox::No);
if(choice == QMessageBox::Yes)
{
receivedSocket = server->nextPendingConnection();
connect(receivedSocket, SIGNAL(readyRead()), this, SLOT(readClient()));
}
}
但总是不对,文件传输会在点击之前完成,想向大家请教一下如何解决?
他的大致思路是这样的
class Widget : public QWidget
{
...
QTcpServer *server;
QTcpSocket *receivedSocket;
private slots:
void acceptConnection();
void readClient();
void on_pushButton_clicked();
};
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
...
}
void Widget::acceptConnection()
{
...@
receivedSocket = server->nextPendingConnection();
connect(receivedSocket, SIGNAL(readyRead()), this, SLOT(readClient()));
}
void Widget::readClient()
{
...
if(byteReceived == 0) //才刚开始接收数据,此数据为文件信息
...
else //正式读取文件内容
...
}
void Widget:n_pushButton_clicked()
{
...
server = new QTcpServer(this);
server->listen(QHostAddress("172.19.198.43"), 10000);
connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
...
}
|
|