Guest User

server.cpp

a guest
Sep 2nd, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include "server.h"
  2.  
  3. Server::Server(QObject *parent, uint port_wej) : QObject(parent)
  4. {
  5.  
  6.     if (!Serwer){
  7.     Serwer = new QTcpServer(this);
  8.  
  9.     connect (Serwer, SIGNAL(newConnection()),this,SLOT(newConnection()));
  10.     }
  11.  
  12.     if(Serwer->isListening()==false)
  13.     {
  14.         Serwer->listen((QHostAddress::Any), port_wej);
  15.     }
  16.  
  17.  
  18. }
  19.  
  20. void Server::newConnection()
  21. {
  22.     if (Serwer->hasPendingConnections()==true)
  23.     {
  24.  
  25.     socket = Serwer->nextPendingConnection();
  26.  
  27.     connect (socket, SIGNAL(connected()),this, SLOT(SocketConnected()));
  28.     connect (socket, SIGNAL(disconnected()),this,SLOT(SocketDisconnected()));
  29.  
  30.     socket->write("Something\r\n");
  31.     socket->flush();
  32.     socket->waitForBytesWritten(3000);
  33.     socket->close();
  34.     Serwer->close();
  35.    // socket->pauseMode();
  36.     }
  37.  
  38. }
  39.  
  40. void Server::SocketConnected()
  41. {
  42.     QString label_connected = "<html><head/><body><p><span style=\" font-weight:600;\">Connected</span> with IP 127.0.0.1</p></body></html>";
  43.  
  44.     emit Connected(label_connected);
  45. }
  46.  
  47. void Server::SocketDisconnected()
  48. {
  49.     QString label_disconnected = "<html><head/><body><p><span style=\" font-weight:600;\">Disconnected</span> with IP 127.0.0.1</p></body></html>";
  50.  
  51.     emit Disconnected(label_disconnected);
  52. }
  53.  
  54. void Server::Disconnect()
  55. {
  56.  
  57.     Serwer->close();
  58.     socket->close();
  59.     socket->~QTcpSocket();
  60.     Serwer->~QTcpServer();
  61.  
  62. }
  63.  
  64. uint Server::GetStatus()
  65. {
  66.     return status;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment