network.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef NETWORK_H
  2. #define NETWORK_H
  3. #include <QObject>
  4. #include <QList>
  5. #include <QByteArray>
  6. #include <QTextCodec>
  7. #include <QHostAddress>
  8. #include <QTcpSocket>
  9. #include <QTcpServer>
  10. #include <QUdpSocket>
  11. class network : public QObject
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit network(QObject *parent = nullptr);
  16. // 数据缓存区
  17. QByteArray TCPServer_dataBuf;
  18. QByteArray TCPClient_dataBuf;
  19. QByteArray UDP_dataBuf;
  20. // 发送/接收字节数
  21. int TCPServer_txByte = 0;
  22. int TCPServer_rxByte = 0;
  23. int TCPClient_txByte = 0;
  24. int TCPClient_rxByte = 0;
  25. int UDP_txByte = 0;
  26. int UDP_rxByte = 0;
  27. bool TCPServer_open(QString ownIp, unsigned short ownPort);
  28. void TCPServer_close();
  29. void TCPServer_sendData(QByteArray sendData);
  30. bool TCPClient_open(QString remoteIp, unsigned short remotePort);
  31. void TCPClient_close();
  32. void TCPClient_sendData(QByteArray sendData);
  33. bool UDP_open(QString ownIp, unsigned short ownPort, QString remoteIp, unsigned short remotePort);
  34. void UDP_close();
  35. void UDP_sendData(QByteArray sendData);
  36. private:
  37. QTextCodec *tc; // 编码
  38. QTcpServer *tcpServer; // tcp服务端
  39. QList<QTcpSocket*> clientList; // 保存连接的客户端
  40. QTcpSocket *tcpClient; // tcp客户端
  41. QUdpSocket *udpSocket; // udp
  42. QHostAddress UDP_remoteIp;
  43. unsigned short UDP_remotePort;
  44. signals:
  45. void tcpServer_readSuccess(int recByte);
  46. void tcpServer_sendSuccess(int sendByte);
  47. void tcpClient_readSuccess(int recByte);
  48. void tcpClient_sendSuccess(int sendByte);
  49. void udp_readSuccess(int recByte);
  50. void udp_sendSuccess(int sendByte);
  51. private slots:
  52. void TCPServer_readData();
  53. void TCPServer_newConnect();
  54. void TCPServer_disconnect();
  55. void TCPClient_readData();
  56. void TCPClient_newConnect();
  57. void TCPClient_disconnect();
  58. void UDP_readData();
  59. };
  60. #endif // NETWORK_H