Advertisement
qasync

A simple TCP Server - the old way to do it

Sep 13th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 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.     void handleRead();
  12. };
  13.  
  14. /* Extract from server.cpp */
  15.  
  16. Server::Server(QObject *parent) : QTcpServer(parent)
  17. {
  18.     listen(QHostAddress::Any, 5555);
  19.     connect(this, SIGNAL(newConnection()), SLOT(handleConnection()));
  20. }
  21. void Server::handleConnection()
  22. {
  23.     QTcpSocket *socket = nextPendingConnection();
  24.     connect(socket, SIGNAL(readyRead()), SLOT(handleRead()));
  25. }
  26. void Server::handleRead()
  27. {
  28.     char buffer[1024];
  29.     buffer[qobject_cast<QTCPSocket *>(QObject::sender())->read(buffer, 1023)] = 0;
  30.     std::cout << buffer;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement