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. Server connected with client in tcp communication in Qt but not receiving data from client.
QtWS25 Last Chance

Server connected with client in tcp communication in Qt but not receiving data from client.

Scheduled Pinned Locked Moved General and Desktop
5 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.
  • N Offline
    N Offline
    Nitika
    wrote on last edited by
    #1

    I am developing an application in TCP in Qt communicating with each other. Server(receiver) gets connected with the client(sender) through "connectToHost" command and the client is writing 6 bytes of data to server but the server is not able to enter into the function reading the data from the client after getting ReadyRead() signal. Kindly suggest the improvement I should make in the code to read the data from the client.

    //client.h

    #ifndef PROJB_H
    #define PROJB_H

    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QtNetwork>
    #include <QHostAddress>

    class Client:public QObject
    {
    Q_OBJECT
    public:
    explicit Client(QObject *parent = 0);
    // void connectreq();

    signals:
    public slots:
    void connected();
    void disconnected();

    private:

    QTcpServer* server_to_rec;
    QTcpServer* server_to_send;
    QTcpSocket *socket_to_send;
    QTcpSocket* socket_to_rec;
    

    };

    #endif // PROJB_H

    //client.cpp
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>

    #include "ProjB.h"

    Client::Client(QObject *parent):QObject(parent)
    {
    socket_to_send = new QTcpSocket(this); //for sending
    qDebug()<<"socket "<<socket_to_send<<" created";

    socket_to_send->connectToHost("192.168.3.55",18006);
    
    connect(socket_to_send, SIGNAL(connected()), this, SLOT(connected())) ;
    
    connect(socket_to_send, SIGNAL(disconnected()), this, SLOT(disconnected())) ;
    

    }

    void Client::connected()
    {
    int bytes;
    QByteArray buff("client");

    qDebug()<<"Connection established";
    while(1)
    {
       bytes =socket_to_send->write(buff);
       qDebug()<<"data sent:"<<bytes<<"bytes";
        sleep(5);
    }
    

    }

    void Client::disconnected()
    {
    qDebug()<<"trying to reestablish connection";
    socket_to_send->connectToHost("192.168.3.55",18006);
    }

    //output of client
    sender output:

    Starting /root/Desktop/26062013/ProjB-build-Desktop-Debug/ProjB...
    socket QTcpSocket(0x8c6aa28) created
    Connection established
    data sent: 6 bytes
    data sent: 6 bytes
    data sent: 6 bytes
    data sent: 6 bytes

    //server.h
    #ifndef PROJA_H
    #define PROJA_H

    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QtNetwork>

    class Server: public QObject
    {
    Q_OBJECT
    public:
    explicit Server(QObject *parent = 0);

    QTcpServer* server_to_rec;
    QTcpSocket* socket_to_rec;
    

    //signals:
    public slots:

    void ReadData();
    void incomingConnection();
    

    private:
    };

    #endif // PROJA_H

    //server.cpp
    #include "ProjA.h"

    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QHostAddress>

    Server::Server(QObject *parent):QObject(parent)
    {
    server_to_rec = new QTcpServer(); //for receiving
    qDebug()<<"server "<<server_to_rec<<" created";

    if((server_to_rec->listen(QHostAddress::Any,18006))==TRUE)
        qDebug()<<"listen successful";
    else
        qDebug()<<"listen failed";
    connect(server_to_rec,SIGNAL(newConnection()),this,SLOT(incomingConnection()));
    

    }

    void Server::incomingConnection()
    {
    socket_to_rec =server_to_rec->nextPendingConnection();
    while(!socket_to_rec)
    {
    qDebug()<<"Trying to Connect"<<endl;
    socket_to_rec =server_to_rec->nextPendingConnection();
    sleep(5);
    }
    qDebug()<<"socket "<<socket_to_rec<<" created";
    connect(socket_to_rec, SIGNAL(readyRead()), this, SLOT(ReadData())) ;
    }

    void Server::ReadData()
    {
    qDebug()<<"start reading";
    while(socket_to_rec->bytesAvailable()>0)//canReadLine())

    {
        char buffer[1024]={0};
        socket_to_rec->read(buffer, socket_to_rec->bytesAvailable());
        qDebug()<<"buffer data:"<<QByteArray(buffer,70).toHex();
    }
    

    }

    //output of server
    Starting /root/Desktop/ProjA-build-Desktop-Debug/ProjA...
    server QTcpServer(0x9a238e8) created
    listen successful
    socket QTcpSocket(0x9a2d180) created

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      please, edit your post formatting code correctly

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

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

        Hi and welcome to devnet,

        Please enclose your code between coding tags. That will make your code readable.

        From what I saw, your are not connecting anything. For example:

        @connect(server_to_rec,SIGNAL),this,SLOT));@

        This line shouldn't even compile

        Have a look at the fortune example, it shows how to do what you want to achieve.

        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
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          [quote author="SGaist" date="1372318994"]Hi and welcome to devnet,

          Please enclose your code between coding tags. That will make your code readable.

          From what I saw, your are not connecting anything. For example:

          @connect(server_to_rec,SIGNAL),this,SLOT));@

          This line shouldn't even compile

          Have a look at the fortune example, it shows how to do what you want to achieve.[/quote]
          If you hover your mouse over those connect parameters you can see the actual signals and slots. Its just a formating thing because he didn't enclose his code with '@' tags.

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

            [quote author="KA51O" date="1372322638"]
            If you hover your mouse over those connect parameters you can see the actual signals and slots. Its just a formating thing because he didn't enclose his code with '@' tags.
            [/quote]

            My bad, thanks for the tip :)

            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