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. 2 QTcpSockets (1 reads another writes) in 2 applications doesn't work

2 QTcpSockets (1 reads another writes) in 2 applications doesn't work

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 3.8k Views
  • 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.
  • _ Offline
    _ Offline
    _ttt
    wrote on last edited by
    #1

    Hi, i wanna send data from program no.1 with socket A to program no.2 with socket B with 2 QTcpSockets:
    In first program i have:
    @
    //in class QTcpSocket *dCtrl
    DataAdderItem::DataAdderItem(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::DataAdderItem)
    {
    //(...)
    connect(ui->addNew, SIGNAL(clicked()), this, SLOT(addNewData()));
    //(...)
    }
    void ProxyAdderItem::addNewData(){

    QByteArray aa = "aa";
    QString str(ui->data->text().toUtf8());
    
            dCtrl->open(QIODevice::ReadWrite); 
            dCtrl->write(str.toAscii());
            //dCtrl->write("aa");
    qDebug() << str.toAscii() << "aa";// write(ui->address->text().toAscii());
    

    }
    @
    and it works. It prints me a text on a terminal's window.
    In second program i have:
    @
    //in class - QTcpSocket* dCtrl;

    NetworkManager::NetworkManager(Manager* c,QObject *parent) :
    QObject(parent)
    {
    //(...)
    dCtrl = new QTcpSocket;
    connect(dCtrl,SIGNAL(readyRead()),
    this, SLOT(addProxy()));
    //(...)
    //connect(dCtrl, SIGNAL(error(QAbstractSocket::SocketError)),
    // this, SLOT(error(QAbstractSocket::SocketError))); // not neccesary
    }

    void ManagerControl::addData(){
    //qDebug("sss");
    //QDataStream in(dCtrl);
    dCtrl->open(QIODevice::ReadWrite);
    QByteArray ar(dCtrl->readAll());
    QString newData(ar);
    //QString newData = dCtrl->readLine();
    qDebug() << "A";
    qDebug() << newProxy;
    int port = 1238;
    }

    @
    And it doesn't write anything on output (looks like socket is not ready).
    What am i doing wrong?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Welcome to the forum

      Either this part has been excluded or the socket in the NetworkManager does not "connectToHost.":http://doc.qt.nokia.com/4.7/qabstractsocket.html#connectToHost

      If you are trying to learn how to handle TcpServer/Client with Qt I would recommend starting with the Fortune "Server":http://doc.qt.nokia.com/4.7/network-fortuneserver.html / "Client":http://doc.qt.nokia.com/4.7/network-fortuneclient.html example code

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        arianoo
        wrote on last edited by
        #3

        seems like you havent openned a new connection.
        u need to connect the sockets first and then they'll transfer data.
        as i remember u need to use connecttohost() for this purpose

        1 Reply Last reply
        0
        • _ Offline
          _ Offline
          _ttt
          wrote on last edited by
          #4

          I wanna connect two clients.
          So i need to use connectToHost or not?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            That is not the way it is supposed to work.
            A server waits for a client to connect.
            A client connects to the server.
            At the end you will have two sockets communicating.

            You should start with the fortune server / client example. It is very simple and it will show you also the way of establishing the communication. (see my previous post)

            You will see also that the term server refers to something less sophisticated as you might initially expect.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              Yes, you need to use connectToHost, but by just using connectToHost your example will not work.

              Once again, for learning how to handle TcpServer/Client you should look at the Fortune "Server":http://doc.qt.nokia.com/4.7/network-fortuneserver.html / "Client":http://doc.qt.nokia.com/4.7/network-fortuneclient.html example code

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • _ Offline
                _ Offline
                _ttt
                wrote on last edited by
                #7

                so if I wanna exchange info between two programs then both of them must have server and socket, right?
                There is no other way and it's bad, ba, bad to think that two sockets can be connected.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  No, typically one side has a server implementation and the other only a client implementation.

                  The server application is just opening a port and listening until some client application is trying to connect. If you look into the example it will show you the basic concepts and you will understand.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on last edited by
                    #9

                    Server and Client.--

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      arianoo
                      wrote on last edited by
                      #10

                      u can also have one server and 2 clients if u strictly wish to use 2 clients
                      its really better to first understand the fortune examples very well

                      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