Qt Can';t使用TcpServer和TcpSocket读取数据

Qt Can';t使用TcpServer和TcpSocket读取数据,qt,Qt,我有一个摄像头,想向其发送数据,但首先我必须在摄像头软件(笔记本电脑上)和我的软件之间建立连接,因此我需要打开两个插座,从中获取数据。然后将软件发送到摄像头 sockettest.h: class SocketTest : public QObject{ Q_OBJECT public: explicit SocketTest(QObject *parent = nullptr); bool start_listen(int); QTcpSocket *

我有一个摄像头,想向其发送数据,但首先我必须在摄像头软件(笔记本电脑上)和我的软件之间建立连接,因此我需要打开两个插座,从中获取数据。然后将软件发送到摄像头

sockettest.h:

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;
    }
}
class SocketTest:公共QObject{
Q_对象
公众:
显式SocketTest(QObject*parent=nullptr);
bool start_listen(int);
QTcpSocket*blutechnix;
qtcsocket*tof;
qtcserver*服务器;
无效输入连接();
信号:
公众时段:
void Connect();
私人:
};
SocketTest::SocketTest(QObject*父对象):QObject(父对象){
服务器=新的QTcpServer(此);
连接(服务器,信号(newConnection()),此,插槽(connect());
tof=新的QTcpSocket(本);
tof->connectToHost(“192.168.0.10”,10001);
如果(tof->waitForConnected(3000))
{
qDebug()侦听(QHostAddress::LocalHost,10000))
{
qDebug()waitForReadyRead();
QByteArray数组=blutechnix->read(blutechnix->bytesavable());
qDebug()写入(数组);
tof->WaitForBytesWrite();
//确保我们写入了正确的字节
tof->waitForReadyRead(3000);
qDebug()setSocketDescriptor(服务器->socketDescriptor()){
QMessageBox::警告((QWidget*)this->parent(),tr(“错误!”),tr(“套接字
错误!);
返回;
}
}
我运行代码,结果得到:

tof连接 听。。。 QHOSTAddress10000

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

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


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

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


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

连接到
错误信号时会报告什么?将端口号更改为80或扫描自由端口
QTcpSocket*socket=new qtcsocket();qint16 port=80;while(!socket->bind(port,QAbstractSocket::DontShareAddress)){port++;}
但是我需要监听端口10000!当连接到
错误信号时会报告什么?将端口号更改为80或扫描空闲端口
qtcsocket*socket=new qtcsocket();qint16 port=80;while(!socket->->bind(port,QAbstractSocket::DontShareAddress)){port++;}
但是我需要监听端口10000!