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. QTCPSocket bind and listen forever for incomming data from a specific port
Forum Updated to NodeBB v4.3 + New Features

QTCPSocket bind and listen forever for incomming data from a specific port

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 4.8k Views 3 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
    mrbitmap
    wrote on last edited by
    #1

    Hello,
    I am trying to make a QTCPConnection from a port 1234 for example. My interface is as simple as this:

          s->tcp = new QTcpSocket(s);
    
           connect(s->tcp, SIGNAL(connected()),
                   s, SLOT(tcpConnected()));
           connect(s->tcp, SIGNAL(disconnected()),
                   s, SLOT(tcpDisconnected()));
           connect(s->tcp, SIGNAL(readyRead()),
                   s, SLOT(readyReadTcp()));
    
           s->tcp->bind(QHostAddress::Any, 1234);
    
    

    The behaviour I expect is that to the UDP bind. But I don't want to know the address of the sender. So is that possible, or some kind of a handshake must be done before? I just want to stick to 1234 port and collect it's data via TCP.

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

      Hi,

      What do you mean by I don't want to know the address of the sender ?

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

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What do you mean by I don't want to know the address of the sender ?

        M Offline
        M Offline
        mrbitmap
        wrote on last edited by
        #3

        @SGaist
        Hello, sorry for the late reply. Here is what I am trying to achieve with Qt socket API:

         Server* s = &Server::Instance();
            p_server = s;
        
        
            uint8_t* buffer = nullptr;
            udp_data_t frame = {0, {0}, {{0}}};
            int client, newsocketfd;
            size_t len;
            struct sockaddr_in serv_addr, client_addr;
        
            if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                perror("Error opening socket!");
                exit(1);
            }
        
            memset((char*)&serv_addr, 0, sizeof(serv_addr));
        
            serv_addr.sin_family = AF_INET;
            serv_addr.sin_addr.s_addr = INADDR_ANY;
            serv_addr.sin_port = htons(1234);
        
            if (bind(socket_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
                perror("BIND failed");
                exit(1);
            }
        
            listen(socket_fd, 5);
            client = sizeof(client_addr);
        
            static char msg[128] = {0};
            for(;;) {
        
                newsocketfd = accept(socket_fd, (struct sockaddr*)&client_addr, (socklen_t*)&client);
                if (newsocketfd < 0) {
                    perror("Error on accept");
                    exit(1);
                }
        
                for(;;) {
                    for(buffer = (uint8_t*)&frame, len = 0; len < sizeof(frame);) {
                        int nn = read(newsocketfd, buffer, sizeof(frame) - len);
                        len += nn;
                        buffer += nn;
                    }
                    printf("%lu\n", (long unsigned int) frame.counter);
        
                    if (++s->m_conn_info.paketCounter != frame.counter) {
                        s->m_conn_info.paketCounter = frame.counter;
                        snprintf(msg, sizeof(msg), "Missed: %lu\n",
                                 (long unsigned int) s->m_conn_info.paketCounter+1);
        
                        utils::IPC::Instance().sendMessage(msg);
                    } else {
                        QList<sample_data_t> ls;
                        // copy all the data then send it to the plugins
                        for(int i=0; i < 32; ++i) {
                            sample_data_t pkt = {0, 0};
                            short smpls[16] ={0};
                            pkt.samples = smpls;
                            pkt.size = 16;
                            // fill the list to be passed to other plugins
                            for(int j=0; j < 16; ++j) {
                                pkt.samples[j] = frame.data[i][j];
                            }
                            ls.append(pkt);
                        }
                        // finally send it
                        s->put_data((QList<sample_data_t>*) &ls);
                    }
                }
        
                close(newsocketfd);
            }
        
            close(socket_fd);
        

        Don't mind the Server singleton, just see the C socket API.
        Thanks.

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

          Shouldn't you rather be using QUdpSocket ?

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

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Shouldn't you rather be using QUdpSocket ?

            M Offline
            M Offline
            mrbitmap
            wrote on last edited by
            #5

            @SGaist
            Hello, unfortunate not, I have to support both UDP/TCP, I've done UDP but I am having troubles with the tcp.

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

              Can you describe how your application is supposed to operate ?

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

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                Can you describe how your application is supposed to operate ?

                M Offline
                M Offline
                mrbitmap
                wrote on last edited by
                #7

                @SGaist
                It's simple: collecting tcp/udp streams and organize them later ( pass them in the plugin tree ). Here is the playground for the udp/tcp streamer plugin:
                https://github.com/heatblazer/recd2/tree/master/udp-streamer

                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