Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. Nessuna risposta da socket server
Forum Updated to NodeBB v4.3 + New Features

Nessuna risposta da socket server

Scheduled Pinned Locked Moved Unsolved Italian
4 Posts 3 Posters 731 Views 2 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.
  • F Offline
    F Offline
    fermatqt
    wrote on last edited by
    #1

    ciao!

    sto testando le socket in qt, e ho messo su questo codice:

    #include "socketserver.h"
    
    SocketServer::SocketServer(QObject *parent) : QTcpServer(parent) {
    }
    
    void SocketServer::startServer() {
        if(!this->listen(QHostAddress::Any, 1234)) {
            qDebug() << "Impossibile avviare il server";
        } else {
            qDebug() << "Server avviato...";
        }
    }
    
    void SocketServer::incomingConnection(int socketDescriptor) {
        qDebug() << socketDescriptor << " Connessione...";
        MThread *thread = new MThread(socketDescriptor, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();
    }
    

    MThread è una classe per la gestione di più thread.
    se volete la posto.

    cmq , quando avvio il programma, in console appare Server avviato...
    ho provato poi ad avviare una connessione tramite telnet:

    telnet> open localhost 1234
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    

    e rimane così.
    non capisco dove sia il problema.

    Pablo J. RoginaP 1 Reply Last reply
    0
    • F fermatqt

      ciao!

      sto testando le socket in qt, e ho messo su questo codice:

      #include "socketserver.h"
      
      SocketServer::SocketServer(QObject *parent) : QTcpServer(parent) {
      }
      
      void SocketServer::startServer() {
          if(!this->listen(QHostAddress::Any, 1234)) {
              qDebug() << "Impossibile avviare il server";
          } else {
              qDebug() << "Server avviato...";
          }
      }
      
      void SocketServer::incomingConnection(int socketDescriptor) {
          qDebug() << socketDescriptor << " Connessione...";
          MThread *thread = new MThread(socketDescriptor, this);
          connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          thread->start();
      }
      

      MThread è una classe per la gestione di più thread.
      se volete la posto.

      cmq , quando avvio il programma, in console appare Server avviato...
      ho provato poi ad avviare una connessione tramite telnet:

      telnet> open localhost 1234
      Trying 127.0.0.1...
      Connected to localhost.
      Escape character is '^]'.
      

      e rimane così.
      non capisco dove sia il problema.

      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by Pablo J. Rogina
      #2

      @fermatqt said in Nessuna risposta da socket server:

      Connected to localhost.

      è chiaro che il cliente è efficace se presente

      e rimane così.

      e che cosa dovrebbe inviare il server?

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fermatqt
        wrote on last edited by
        #3

        ciao!

        sostanzialmente mi aspettavo di visualizzare quello che mando in output dal MThread:

        #include "mthread.h"
        
        MThread::MThread(int id, QObject *parent) : QThread(parent) {
            this->socketDescriptor = id;
        }
        
        void MThread::run() {
            qDebug() << socketDescriptor << " - Inizio thread";
            socket = new QTcpSocket();
            if(!socket->setSocketDescriptor(this->socketDescriptor)) {
                emit error(socket->error());
                return;
            }
        
            connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
            connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()),Qt::DirectConnection);
        
            qDebug() << socketDescriptor << " - Connessione client";
        
            exec();
        }
        
        void MThread::readyRead() {
            QByteArray Data = socket->readAll();
        
            qDebug() << socketDescriptor << " Data in: " << Data;
        
            socket->write(Data);
        }
        
        void MThread::disconnected() {
            qDebug() << socketDescriptor << "Disconnesione";
            socket->deleteLater();
            exit(0);
        }
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          QThread non e' il thread, e' un contenitore del thread che vive sul thread principale. Gli slot non verranno eseguiti sul thread secondario ma su quello principale. Il mutlithreading qui e' inutile e dannoso, riscrivi il programma in single thread.
          Puoi usare questo esempio come punto di partenza se ti trovi arenato: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          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