Advertisement
qasync

A Simple TCP Server - our way

Sep 14th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. /* Extract from server.h */
  2.  
  3. class Server : public QTcpServer
  4. {
  5.     Q_OBJECT
  6. public:
  7.     Server(QObject *parent = 0);
  8.     virtual ~Server() { }
  9. private slots:
  10.     void handleConnection();
  11. };
  12.  
  13. /* Extract from server.cpp */
  14.  
  15. Server::Server(QObject *parent) : QTcpServer(parent)
  16. {
  17.     listen(QHostAddress::Any, 5555);
  18.     connect(this, SIGNAL(newConnection()), SLOT(handleConnection()));
  19. }
  20. void Server::handleConnection()
  21. {
  22.     QTcpSocket *socket = nextPendingConnection();
  23.     forever {
  24.         char buffer[1024];
  25.         buffer[socket->readAsync(buffer, 1023)] = 0;
  26.         std::cout << buffer;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement