How to send via tcpip, with signals and slots



  • hello,

    in this class a tcp-ip server is started and listen on port 555

    @#include "tcpserver.h"

    tcpserver::tcpserver(QObject *parent) :
    QObject(parent)
    {
    server = new QTcpServer (this);
    connect (server,SIGNAL(newConnection()),this,SLOT(newconnection()));
    if(!server->listen(QHostAddress::Any,555))
    {
    qDebug() << "Server nicht gestartet";
    }
    else
    {
    qDebug() << "Server gestartet";
    }
    }

    void tcpserver::newconnection()
    {
    QTcpSocket *socket = server->nextPendingConnection();
    socket->write("hello client");
    socket->flush();
    socket->waitForBytesWritten(3000);
    // socket->close;
    }

    void tcpserver::m_auto()
    {
    socket->write("m_auto=1");
    socket->flush();
    socket->waitForBytesWritten(3000);
    }@

    now what works is the connecting client.
    but now, how can i make, that when an event occurs in the ui,
    a special method of the server is run to send a string?

    see, the last method cannot compile because it says
    @socket was not declared in this scope@

    so how can i use this already etablished connection to send?

    ps:
    there will always only be one client
    thanks a lot



  • You have created a pointer variable called socket in this method:

    @bq. void tcpserver::newconnection()
    {
    QTcpSocket *socket = server->nextPendingConnection();
    socket->write("hello client");
    socket->flush();
    socket->waitForBytesWritten(3000);
    // socket->close;
    }
    @

    and next you try to acces it from :

    @void tcpserver::m_auto()
    {
    socket->write("m_auto=1");
    socket->flush();
    socket->waitForBytesWritten(3000);
    }@

    The compiler launch an error because the scope of socket is tcpserver::newconnection() method, and it can't found it on tcpserver::m_auto().

    If you want to use the same reference, maybe you could create a private member (QTcpSocket *m_socket) in order to pass it between methods from tcpserver.h objects, and assign it a value as follow:

    @bq. void tcpserver::newconnection()
    {
    QTcpSocket *socket = server->nextPendingConnection();

       //here:
       m_socket = socket;
    
        socket->write("hello client");
        socket->flush();
        socket->waitForBytesWritten(3000);
     //   socket->close;
    }
    

    @

    Now, you can access and use your socket variable pointer from anywhere inside tcpserver class:

    @void tcpserver::m_auto()
    {
    m_socket->write("m_auto=1");
    m_socket->flush();
    m_socket->waitForBytesWritten(3000);
    }@

    I hope I help you!



  • hi thanks for the answer.

    hi yes that works but thats not what im looking for.
    i want to etablish the tcpip connection.

    and then later when its done, i want to send ascii when a signal in the ui was made,
    f.x. a checkbox is set, then via tcpip s.th. should be send.

    so how can i access a slot which sends?



  • Use the readyRead() signal to know when there is data available on the socket for reading.
    Use the bytesWritten() signal if you need to know when data written to the socket has been sent.

    Generally there is no need to use flush() or waitForBytesWritten().
    Write your code in asynchronous style to keep the program responsive.


Log in to reply
 

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