Qt对几种不同协议的socket进行了封装,均继承自QAbstractSocket
QUdpSocket
: 此类提供对UDP的socket
QTcpSocket
: 此类提供对TCP的socket QTcpServer
: 此类提供TCP服务1 qint64 QUdpSocket::writeDatagram(
2 const char *data, qint64 size, //数据
3 const QHostAddress &address, quint16 port) //地址端口
1 qint64 QUdpSocket::readDatagram(
2 char * data, qint64 maxSize, //数据
3 QHostAddress *address=0, quint16 *port=0) //地址端口
1 bool QAbstractSocket::bind(
2 const QHostAddress &address, //地址
3 quint16 port = 0, //端口
4 BindMode mode = DefaultForPlatform) //绑定模式
1 void QIODevice::readyRead() //当绑定的端口接受到数据时运行
因为不能绑定端口,功能比 QUdpSocket
更简单
1 qint64 QIODevice::read(char * data, qint64 maxSize)
2 qint64 QIODevice::write(const char * data, qint64 maxSize)
1 void QAbstractSocket::connectToHost(
2 const QString & hostName, //服务器地址
3 quint16 port, //服务器端口
4 OpenMode openMode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol)
此类用于架设TCP服务器,优点是非阻塞
1 bool QTcpServer::listen(
2 const QHostAddress & address = QHostAddress::Any,
3 quint16 port = 0)
1 void QTcpServer::newConnection() // [signal]
1 QTcpSocket* QTcpServer::nextPendingConnection()
1 class Thread : public QThread {
2 Q_OBJECT
3 public:
4 Thread(QObject* parent=0):QThread(parent){}
5 void run() {
6 //你的代码
7 }
8 };
1 void QThread::start()
1 void QThread::finished() // [signal]
QObject
继承而来的类,生成moc代码实际上并不是什么保留字,而是一个宏定义,用于给qmake做语法分析
1 //from qobjectdefs.h
2 #define slots
3 #define signals public
4 #define emit
在moc文件中定义了
1 //from qobjectdefs.h
2 #define SLOT(a) qFlagLocation("1"#a QLOCATION)
3 #define SIGNAL(a) qFlagLocation("2"#a QLOCATION)
用于进行信号槽的匹配
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |