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

  • 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.

  • Lifetime Qt Champion


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

  • @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!");
        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");
        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");
            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);
                } 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];
                    // finally send it
                    s->put_data((QList<sample_data_t>*) &ls);

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

  • Lifetime Qt Champion

    Shouldn't you rather be using QUdpSocket ?

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

  • Lifetime Qt Champion

    Can you describe how your application is supposed to operate ?

  • @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:

Log in to reply