c – Qt双向客户端服务器使用QTcpSocket和QTcpServer

前端之家收集整理的这篇文章主要介绍了c – Qt双向客户端服务器使用QTcpSocket和QTcpServer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@我正在尝试实现双向的客户端 – 服务器程序,客户端和服务器可以在彼此之间传递序列化的对象.我试图使用Qt(QTcpSocket和QTcpServer)这样做.我已经在 java中实现了这样的程序,但是我不知道如何使用Qt.我已经查看了 fortune clientfortune server的例子…但是从我可以看到,客户端只是发信号通知服务器,服务器发送一些数据.我需要客户端和服务器来回发送对象.我不是在寻找一个完整的解决方案,我正在寻找的是正确方向的一些指导.

我写了一些代码,它接受一个连接,但不接受数据.

服务器

这个类是服务器;应该接受连接并输出正在发送的缓冲区的大小.但是它正在输出0

#include "comms.h"

Comms::Comms(QString hostIP,quint16 hostPort)
{
    server = new QTcpServer(this);
    hostAddress.setAddress(hostIP);
    this->hostPort = hostPort;


}

void Comms::attemptConnection(){
    connect(server,SIGNAL(newConnection()),this,SLOT(connectionAccepted()));
    //socket = server->nextPendingConnection();
    server->listen(hostAddress,hostPort);
    //receivedData = socket->readAll();
}

void Comms::connectionAccepted(){
    qDebug()<<"Connected";
    socket = new QTcpSocket(server->nextPendingConnection());

    char* rec = new char[socket->readBufferSize()];
    qDebug()<<socket->readBufferSize();
}

客户

这个类是客户端.它应该是发送字符串“你好”.它成功发送(据我所知)

#include "toplevelcomms.h"
#include "stdio.h"

TopLevelComms::TopLevelComms(QString hostIP,quint16 hostPort)
{
    tcpSocket = new QTcpSocket();
    hostAddress.setAddress(hostIP);
    this->hostPort = hostPort;
}


void TopLevelComms::connect(){
    tcpSocket->connectToHost(hostAddress,hostPort,QIODevice::ReadWrite);
    //tcpSocket->waitForConnected(1);

    QString string = "Hello";
    QByteArray array;
    array.append(string);
    qDebug()<<tcpSocket->write(array);
}

请告诉我我在做错什么,或告诉我在Qt中建立我想要的一般逻辑.

解决方法

默认情况下,QTcpSocket是异步的,因此当您调用connectToHost并在相同的上下文中写入时,将不会发送,因为套接字未连接.你应该改变你的“客户端”代码
void TopLevelComms::connect(){
    tcpSocket->connectToHost(hostAddress,QIODevice::ReadWrite);
    if(tcpSocket->waitForConnected()) // putting 1 as parameter isn't reasonable,using default 3000ms value
    {
        QString string = "Hello";
        QByteArray array;
        array.append(string);
        qDebug()<<tcpSocket->write(array);
    }
    else
    {
        qDebug() << "couldn't connect";
    }
}

注意:您也没有检查是否能够听

void Comms::attemptConnection(){
    connect(server,SLOT(connectionAccepted()));
    //socket = server->nextPendingConnection();

    if(server->listen(hostAddress,hostPort))
    {
        qDebug() << "Server listening";
    }
    else
    {
        qDebug() << "Couldn't listen to port" << server->serverPort() << ":" << server->errorString();
    }
    //receivedData = socket->readAll();
}

最后一件事.请注意,QTcpServer :: nextPendingConnection()返回QTcpSocket,因此,不要采用新的连接,您可以使用nextPendingConnection创建新的QTcpSocket作为父

void Comms::connectionAccepted(){
    qDebug()<<"Connected";
    // WRONG! it will use QTcpSocket::QTcpSocket(QObject * parent)
    //socket = new QTcpSocket(server->nextPendingConnection());
    // use simple asign
    socket = server->nextPendingConnection();
    // move reading to slot
    connect(socket,SIGNAL(readyRead()),SLOT(readSocket()));
}

现在我们将把阅读移到单独的插槽

void Comms::readSocket()
{
    // note that dynamic size array is incompatible with some compilers
    // we will use Qt data structure for that
    //char* rec = new char[socket->readBufferSize()];
    qDebug()<<socket->readBufferSize();
    // note that QByteArray can be casted to char * and const char *
    QByteArray data = socket->readAll();
}

我必须承认,这样的小代码示例是很多错误.您需要了解有关TCP / IP连接的一些知识.那些是流,并且没有一个保证,整个数据块将一次给你

猜你在找的C&C++相关文章