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. Socket recvied and signal and slot

Socket recvied and signal and slot

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 4.6k 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.
  • M Offline
    M Offline
    m_pahlevanzadeh
    wrote on last edited by
    #1

    Dear all,

    I wrote the following signal that it can specify socket has data or not:
    @bool NetworkSocket::isDataReady()
    {

    /// Got here because iSelectReturn > 0 thus data available on at least one descriptor
    // Is our socket in the return list of readable sockets
    bool             res;
    fd_set          sready;
    struct timeval  nowait;
    
    FD_ZERO(&sready);
    FD_SET((unsigned int)this->socketFD,&sready);
    //bzero((char *)&nowait,sizeof(nowait));
    memset((char *)&nowait,0,sizeof(nowait));
    
    res = select(this->socketFD+1,&sready,NULL,NULL,&nowait);
    if( FD_ISSET(this->socketFD,&sready) )
        res = true;
    else
        res = false;
    
    if (res)
        emit createThread( );
    return res;
    

    }@

    Then i wrote a slot that it work standalone and create thread:
    @bool NetworkSocket::createThread()
    {

    bool returnValue = true ;
    void * args = NULL;
    pthread_t tempID;
    if (!firstTime)
    {   /*(void*(*)(void*))*/
    

    /* if (pthread_create(&tempID, NULL,(void * ()(void )) &threadProcedure , (void )this) != 0)
    {
    //error
    }
    this->threadCounter++;
    this->threadVector.push_back(tempID);
    this->firstTime = false;
    /
    }
    else
    {
    /
    (void
    ()(void))/
    if (pthread_create(&tempID, NULL, (void
    ()(void)) &NetworkSocket::threadProcedure , reinterpret_cast<void *>(this)) != 0)
    {
    //error
    }

        this->threadVector.push_back(tempID);
        ++this->threadCounter;
    
    }
    
        returnValue = false;
    return returnValue;
    

    }@
    I put in theradProcudre a cout, and the following line in contructor:
    @QObject::connect(this,SIGNAL(isDataReady()),this,SLOT(createThread( )));@
    But my slot doesn't run.. what's happen?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Neutron Stein
      wrote on last edited by
      #2

      Did you subclass QObject?
      And you should call the method isDataReady().

      Never Seen !

      1 Reply Last reply
      0
      • M Offline
        M Offline
        m_pahlevanzadeh
        wrote on last edited by
        #3

        my class definition:
        @class NetworkSocket : public QObject{
        Q_OBject
        signals:
        inline bool isDataReady();
        public slots:
        bool createThread();

        };
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          guziemic
          wrote on last edited by
          #4

          Hi,

          You should use Q_OBJECT not Q_OBject (but this error for sure is catched at compilation time).
          Next, I do not see in the code where you call

          @
          emit isDataReady();
          @

          Maybe this is a problem?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mario84
            wrote on last edited by
            #5

            ...and Signals shouldn't have an implementation or return value!
            As the name says: its just a signal, that somebody else can react to...

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Neutron Stein
              wrote on last edited by
              #6

              in your code you are emiting createThread( ) signal using @emit createThread( );@
              This doesn't call the method createThread( ) but tries to emit a signal named createThread( ) which you don't have. That is why it not working.I thin you should have this
              @
              class NetworkSocket : public QObject{
              Q_OBject
              public:
              bool isDataReady();
              signals:
              void dataReady();
              public slots:
              bool createThread();

              };
              

              @

              @
              bool NetworkSocket::isDataReady()
              {

                  /// Got here because iSelectReturn > 0 thus data available on at least one descriptor
                  // Is our socket in the return list of readable sockets
                  bool             res;
                  fd_set          sready;
                  struct timeval  nowait;
               
                  FD_ZERO(&sready);
                  FD_SET((unsigned int)this->socketFD,&sready);
                  //bzero((char *)&nowait,sizeof(nowait));
                  memset((char *)&nowait,0,sizeof(nowait));
               
                  res = select(this->socketFD+1,&sready,NULL,NULL,&nowait);
                  if( FD_ISSET(this->socketFD,&sready) )
                      res = true;
                  else
                      res = false;
               
                  if (res)
                      emit dataReady( );
                  return res;
               
              }
              

              @

              @
              QObject::connect(this,SIGNAL(dataReady()),this,SLOT(createThread( )));
              @

              Never Seen !

              1 Reply Last reply
              0
              • F Offline
                F Offline
                franku
                wrote on last edited by
                #7

                What about using QTcpSocket, QTcpServer or similar?

                This, Jen, is the internet.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  Neutron Stein
                  wrote on last edited by
                  #8

                  what do you mean ?
                  In QTcpSocket there is a signal readyRead() emited every time that data is written in the socket. Are talking about that?

                  Never Seen !

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    franku
                    wrote on last edited by
                    #9

                    Well, there's very much functionality already built in Qt, like waiting on a socket to connect or data to be ready on a peer. In the code above I saw Qt signals and slot mechanism being wrapped around native posix socket code. This is probalbly not that much portabel in comparison to using the Qt-built-in socket functionality, however, coding yourself again may lead to bugs.

                    This, Jen, is the internet.

                    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