How yo connect realterm TCP to qt console



  • Hi,
    i have a doubt that how can i connect using portnumber to realterm TCP in qt console.all i have to do is i have to give a connection from qt console to real term TCP and we have to type some strings in realterm TCP should appear on the qt



  • how to write this code in qt console
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QMessageBox>
    #include <QDebug>

    static int PORT_NUMBER=23;
    static int WAIT_FOR_DATA_MS = 200;
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_server(new QTcpServer(this)),
    m_socket(nullptr)

    {

    ui->setupUi(this);
    ui->btnstopserver->setEnabled(false);
    connect(m_server, QTcpServer::newConnection,this, &MainWindow::ExchangeData);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_btnstartserver_clicked()
    {
    if( startserver())
    {
    ui->btnstartserver->setEnabled(false);
    ui->btnstopserver->setEnabled(true);
    }
    }
    void MainWindow::on_btnstopserver_clicked()
    {
    stopserver();
    ui->btnstartserver->setEnabled(true);
    ui->btnstopserver->setEnabled(false);
    }
    bool MainWindow::startserver()
    {
    bool result = m_server->listen(QHostAddress::Any, PORT_NUMBER);
    if (!result)
    {
    QMessageBox:: critical(this,tr("EchoServer"),tr("unable to start server:%1").arg(m_server->errorString()));
    return false;
    }
    return true;
    }
    void MainWindow::stopserver()
    {
    m_server->close();
    if((m_socket!= nullptr) &&m_socket->isOpen())
    {
    m_socket->close();
    }
    }
    void MainWindow ::ExchangeData()
    {
    m_socket = m_server->nextPendingConnection();
    if (m_socket->isOpen())
    {
    connect(m_socket, &QTcpSocket::readyRead,this, &MainWindow::EchoReadData);
    }

    }
    void MainWindow ::EchoReadData()
    {
    m_socket->write("<echoserver>\n");
    QString result;
    while(!m_socket->atEnd())
    {
    result.append(m_socket->readAll());
    m_socket->waitForReadyRead(WAIT_FOR_DATA_MS);

    }
    

    m_socket->write(qPrintable(result));
    m_socket->write("\n</echoserver>\n");
    qDebug() << "hey";

    }


  • Lifetime Qt Champion

    Hi,

    You should take a look a the network examples.

    You are also triggering possibly blocking loop that will not allow to do anything else in your application.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.