title: Why client dont connect to server? Qt
body: <p>Programm works, but client can't connect to the server. (i run 2 examples of programm: client and server). I can't find where is my mistake. </p>
  
  <p>I wrote the codes below.You will see what i want to do if you look at main function.</p>
  
  <pre><code>//main.cpp
  int main(int argc, char *argv[])
  {
  QCoreApplication a(argc, argv);
  //выбор клиента или сервера
  cout &lt;&lt; "1.Client\n2.Server\n";
  switch (_getch())
  {
  case 49: 
      {
      cout&lt;&lt;"client init\n";
      Client* cli = new Client("localhost",1922);
  
      string line;
      while(line!="exit") {
          cout &lt;&lt; "Message : ";
          cin &gt;&gt; line;
          cli-&gt;SendData(QString(line.c_str()));
      }
      break;
      }
  case 50:
      {
      cout&lt;&lt;"server init\n";
      Server* srv = new Server(0, 1922);
      break;
      }
  }
  
  return a.exec();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//server.h
  class Server : public QTcpServer {
    Q_OBJECT public : Server(QObject *parent = 0, quint16 port = 1922);
    virtual ~Server();
  
  private
  slots:
    void acceptConnection();
    void startRead();
    void disconnected();
  
  private:
    QTcpServer *tcpServer;
    QTcpSocket *client;
  };
  </code></pre>
  
  <hr>
  
  <pre><code>//server.cpp
  Server::Server(QObject *parent, quint16 port) : QTcpServer(parent) {
    //tcpServer = new QTcpServer(this);
    connect(this, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
  
    if (!this-&gt;listen(QHostAddress::Any, port))
      std::cout &lt;&lt; "unable to start server\n"
                &lt;&lt; this-&gt;errorString().toUtf8().constData() &lt;&lt; endl;
    else
      std::cout &lt;&lt; "server started\n";
  }
  
  Server::~Server() {
    //delete client;
    close();
  }
  
  void Server::acceptConnection() {
    std::cout &lt;&lt; "new connection!\n";
    client = nextPendingConnection();
  
    connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
    connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
  
    qDebug() &lt;&lt; "New client from:" &lt;&lt; client-&gt;peerAddress().toString();
  }
  
  void Server::startRead() {
    client = (QTcpSocket *)sender();
    while (client-&gt;canReadLine()) {
      QString line = QString::fromUtf8(client-&gt;readLine()).trimmed();
      qDebug() &lt;&lt; "Client :" &lt;&lt; line;
  
      client-&gt;write(QString("Server : I've taken your message (:\n").toUtf8());
    }
  
  }
  
  void Server::disconnected() {
  
    qDebug() &lt;&lt; "Client disconnected:" &lt;&lt; client-&gt;peerAddress().toString();
  
    client-&gt;write(QString("Server : I wish you didn't leave ):\n").toUtf8());
  
  }
  
  //} &lt;-- EDIT: THIS IS PROBABLY AN EXTRA
  </code></pre>
  
  <hr>
  
  <pre><code>//***************************************************************
  //client.h
  class Client : public QObject {
    Q_OBJECT public : Client(const QString &amp;add, int port, QObject *obj = 0);
    void SendData(QString data);
    virtual ~Client();
    int status();
    QString err;
  private
  slots:
    void ReadData();
    void slotConnected();
    void slotError(QAbstractSocket::SocketError);
  
  private:
    QTcpSocket *socket;
  
  };
  </code></pre>
  
  <hr>
  
  <pre><code>//client.cpp
  Client::Client(const QString &amp;add, int port, QObject *obj) : QObject(obj) {
    //create socket
    socket = new QTcpSocket(this);
    //connect
    socket -&gt;connectToHost(add, port);
  
    connect(socket, SIGNAL(readyRead()), SLOT(ReadData()));
    connect(socket, SIGNAL(connected()), SLOT(slotConnected()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
            SLOT(slotError(QAbstractSocket::SocketError)));
  
  }
  
  Client::~Client() {
    socket-&gt;close();
    delete socket;
  }
  
  void Client::SendData(QString data) {
    if (!data.isEmpty()) {
      QByteArray arrBlock;
      QDataStream out(&amp;arrBlock, QIODevice::WriteOnly);
      out.setVersion(QDataStream::Qt_5_1);
      out &lt;&lt; quint16(0) &lt;&lt; QTime::currentTime() &lt;&lt; data;
      out.device()-&gt;seek(0);
      out &lt;&lt; quint16(arrBlock.size() - sizeof(quint16));
  
      socket-&gt;write(arrBlock);
      socket-&gt;flush();
    }
  }
  
  void Client::ReadData() {
    QDataStream in(socket);
    in.setVersion(QDataStream::Qt_5_1);
    while (socket-&gt;canReadLine()) {
  
      QString line = QString::fromUtf8(socket-&gt;readLine()).trimmed();
      qDebug() &lt;&lt; line;
    }
  }
  
  void Client::slotConnected() {
    socket-&gt;write(QString("Client : Server connection has been made (: \n")
                      .toUtf8());
  }
  
  void Client::slotError(QAbstractSocket::SocketError err) {
    QString strError =
        "Error: " + (err == QAbstractSocket::HostNotFoundError
                         ? "The host was not found."
                         : err == QAbstractSocket::RemoteHostClosedError
                         ? "The remote host is closed."
                         : err == QAbstractSocket::ConnectionRefusedError
                         ? "The connection was refused."
                         : QString(socket-&gt;errorString()));
    std::cout &lt;&lt; strError.toUtf8().constData() &lt;&lt; endl;
  }
  
  int Client::status() { return socket-&gt;state(); }
  </code></pre>
  
  <p>help me pls!</p>