I have two projects, one for the server and the other for the client:

Server:

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication a(argc, argv);
  4.  
  5. Server s;
  6. s.start(9999);
  7.  
  8. return a.exec();
  9. }
  10.  
  11. class Server : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Server(QObject *parent = 0);
  16.  
  17. signals:
  18.  
  19. public slots:
  20. void start(int port);
  21. void newConnection();
  22. void sendMsg();
  23.  
  24. private:
  25. QTcpServer m_server;
  26. QList<QTcpSocket*> m_clientList;
  27. QTimer m_timer;
  28.  
  29. };
  30.  
  31. Server::Server(QObject *parent) :
  32. QObject(parent)
  33. {
  34. QObject::connect(&m_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
  35. QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(sendMsg()));
  36. m_timer.setInterval(1000);
  37. m_timer.start();
  38. }
  39.  
  40. void Server::start(int port)
  41. {
  42. m_server.listen(QHostAddress::Any,port);
  43. }
  44.  
  45. void Server::newConnection()
  46. {
  47. qDebug() << "NEW CONNECTION!";
  48. m_clientList.append(m_server.nextPendingConnection());
  49. qDebug() << m_clientList.count();
  50. }
  51.  
  52. void Server::sendMsg()
  53. {
  54. foreach(QTcpSocket* socket, m_clientList)
  55. {
  56. socket->write("test");
  57. qDebug() << "send: " << socket->peerAddress();
  58. }
  59. }
To copy to clipboard, switch view to plain text mode 

Client:
Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QCoreApplication a(argc, argv);
  4.  
  5. Client c;
  6. c.connectToHost("localhost",9999);
  7.  
  8. return a.exec();
  9. }
  10.  
  11. class Client : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Client(QObject *parent = 0);
  16.  
  17. signals:
  18.  
  19. public slots:
  20. void connectToHost(QString hostName, int port);
  21. void read();
  22.  
  23. private:
  24. QTcpSocket m_client;
  25.  
  26. };
  27.  
  28. Client::Client(QObject *parent) :
  29. QObject(parent)
  30. {
  31. QObject::connect(&m_client,SIGNAL(readyRead()),this,SLOT(read()));
  32. }
  33.  
  34. void Client::connectToHost(QString hostName, int port)
  35. {
  36. m_client.connectToHost(hostName,port);
  37. }
  38.  
  39. void Client::read()
  40. {
  41. QDataStream in(&m_client);
  42. in.setVersion(QDataStream::Qt_4_0);
  43. QString msg;
  44. in >> msg;
  45. qDebug() << msg;
  46. }
To copy to clipboard, switch view to plain text mode 

It should print 'test' on the client, but it prints an empty string. What am I missing?