首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用TcpServer和TcpSocket读取数据

无法使用TcpServer和TcpSocket读取数据
EN

Stack Overflow用户
提问于 2017-08-02 12:07:08
回答 1查看 53关注 0票数 1

我有一个相机,我想把数据发送给它,但是首先我必须把相机的软件(它在我的笔记本电脑上)和我的软件连接起来,所以我需要打开两个插座,获取数据from.the软件,然后把它发送到相机。

sockettest.h:

代码语言:javascript
复制
class SocketTest : public QObject{

    Q_OBJECT
    public:
    explicit SocketTest(QObject *parent = nullptr);
    bool start_listen(int);
    QTcpSocket *blutechnix;
    QTcpSocket *tof;
    QTcpServer *server;
    void incomingConnection();
    signals:

    public slots:
    void Connect();
    private:
};


SocketTest::SocketTest(QObject *parent) : QObject(parent){

    server = new QTcpServer(this);
    connect(server, SIGNAL(newConnection()),this, SLOT(Connect()));
    tof = new QTcpSocket(this);
    tof->connectToHost("192.168.0.10",10001);
    if(tof->waitForConnected(3000))
     {
    qDebug() << "tof connected";


    if(server->listen(QHostAddress::LocalHost, 10000))
        {
            qDebug() << "Listening...";
            qDebug() << server->serverAddress();
            qDebug() << server->serverPort();
        }
    }
    else
        {
            qDebug() << "tof connexion failed";
        }
    tof->close();

    }


sockettest.h:

void SocketTest::Connect(){

    qDebug() << "CONNECTED";
    blutechnix = server->nextPendingConnection();
    blutechnix->waitForReadyRead();
    QByteArray array =blutechnix->read(blutechnix->bytesAvailable());
    qDebug() << array;
    //send all data to the camera
    tof->write(array);
    tof->waitForBytesWritten();
    //make sure that we wrote the right bytes
    tof->waitForReadyRead(3000);
    qDebug() << "Reading tof" << tof->bytesAvailable();
    qDebug() << tof->readAll();

}

void SocketTest::incomingConnection() {

    if( ! blutechnix->setSocketDescriptor(server->socketDescriptor()) ) {
    QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Socket 
    error!") );
    return;
    }
}

我运行了代码,结果是:

连听..。QHostAddress<"127.0.0.1"> 10000

为什么我看不见数据?为什么不执行Connect()函数?我希望你能帮我。

EN

回答 1

Stack Overflow用户

发布于 2017-08-06 12:11:29

服务器正在侦听端口10000上的本地主机的连接。您可能想要的是连接到tof上的tof信号,并使用tof->readAll()从那里读取数据,并使用tof.write(QByteArray)编写数据。

您不需要QTcpServer,除非您有另一个愿意与您建立连接的程序(即您是服务器)。这是摄像头软件的工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45460443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档