Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How yo connect realterm TCP to qt console
Forum Updated to NodeBB v4.3 + New Features

How yo connect realterm TCP to qt console

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishalreddy
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vishalreddy
      wrote on last edited by
      #2

      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";

      }

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved