選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test.cpp 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "test.h"
  2. Test::Test(QObject *parent) : QObject(parent)
  3. {
  4. m_serv = new PTServer(this);
  5. connect(m_serv, SIGNAL(newConnection()), this, SLOT(m_newConnection()));
  6. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient(PTSocket*)));
  7. }
  8. void Test::testIntConvert()
  9. {
  10. QList<int> tests;
  11. tests << 0 << 1 << 255 << 256 << 65535 << 65536 << -1 << -65535 << -65536 << 123456789 << -123456789;
  12. for(int i = 0; i < tests.size(); ++i)
  13. Q_ASSERT(PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i])) == tests[i]);
  14. qDebug()<<"Int convert OK";
  15. }
  16. void Test::testListen(quint16 p)
  17. {
  18. Q_ASSERT(m_serv->listen(QHostAddress::Any, p));
  19. }
  20. void Test::testNewConnection()
  21. {
  22. ASSERT_LISTEN;
  23. PTSocket* sock = new PTSocket(this);
  24. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
  25. m_clients.append(sock);
  26. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  27. }
  28. void Test::m_newConnection()
  29. {
  30. qDebug()<<"New socket";
  31. }
  32. void Test::m_newClient(PTSocket* s)
  33. {
  34. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
  35. qDebug()<<"New client:"<<s->peerAddress()<<s->peerPort();
  36. }
  37. void Test::m_clientStateChanged(PTSocket::State s)
  38. {
  39. PTSocket* sock = qobject_cast<PTSocket*>(sender());
  40. if(sock == 0)
  41. return;
  42. qDebug()<<sock<<sock->isServerSide()<<s;
  43. if(s == PTSocket::Error)
  44. qDebug()<<sock->errorString();
  45. }