1
新建一个项目:TCPServer.pro
A
修改TCPServer.pro,注意:如果是想使用网络库,需要加上network
SOURCES TcpServer.cpp TcpClient.cpp HEADERS TcpServer.h TcpClient.h QT CONFIG |
B
新建如下文件,因为要用到网络库,所以加上network
C
编写IP选择下拉选,头文件ChooseInterface.h
#ifndef #define #include #include class { Q_OBJECT public: explicit QComboBox* QString signals: public void }; #endif |
编写ChooseInterface.cpp
#include #include #include ChooseInterface::ChooseInterface(QWidget QDialog(parent) { /* QList<QHostAddress> #if QList<QNetworkInterface> QList<QNetworkAddressEntry> entryList).broadcast() #endif //编写一个下拉选 _comboBox QVBoxLayout* lay->addWidget(_comboBox); foreach(QHostAddress { //将地址都转换成为ipv4的地址 quint32 //的ip if(ipaddr continue; //在comboBox中添加下拉选 _comboBox->addItem(QHostAddress(ipaddr).toString()); } //当下拉选发生变化时的操作 connect(_comboBox, this, } void { this->_strSelect } |
上面的运行结果是:
编写TcpServer.h |
#ifndef #define #include #include #include #include class { Q_OBJECT public: explicit QTcpServer* QTcpSocket* QTextBrowser* signals: public void void }; #endif |
编写TcpServer.cpp |
#include #include #include #include #include TcpServer::TcpServer(QWidget QWidget(parent) { // _server ChooseInterface dlg.exec(); //消息提示框 QMessageBox::information(NULL,"you _server->listen(QHostAddress(dlg._strSelect), // connect(_server, this, _show QHBoxLayout* lay->addWidget(_show); } void { // while(_server->hasPendingConnections()) { // _socket _show->append("New // connect(_socket, this, } } void { // while(_socket->bytesAvailable() { _show->append("Data QByteArray _show->append(buf); } } |
编写TcpClient.h |
#ifndef #define #include #include #include class { Q_OBJECT public: explicit QTcpSocket* QLineEdit* signals: public void }; #endif |
编写TcpClient.cpp |
#include #include #include TcpClient::TcpClient(QWidget QWidget(parent) { _socket _socket->connectToHost("127.0.0.1", _lineEdit QHBoxLayout* lay->addWidget(_lineEdit); QPushButton* lay->addWidget(button); connect(button, connect(_lineEdit, } void { QString if(strText.isEmpty()) return; _socket->write(strText.toUtf8()); _lineEdit->clear(); } |
MyWidget.h |
#ifndef #define #include class { Q_OBJECT public: explicit signals: public }; #endif |
MyWidget.cpp |
#include #include #include #include MyWidget::MyWidget(QWidget QWidget(parent) { } int { QApplication TcpServer TcpClient s.setWindowTitle("server"); c.setWindowTitle("client"); return } |
运行结果:
2
编写UDP程序
UDPServer.pro |
QT CONFIG HEADERS Udp1.h Udp2.h MyWidget.h SOURCES Udp1.cpp Udp2.cpp MyWidget.cpp |
Udp1.h |
#ifndef #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp1.cpp |
#include #include #include Udp1::Udp1(QWidget QWidget(parent) { // _udp _udp->bind(10001); connect(_udp, this, // QTimer* timer->setInterval(1000); timer->start(); connect(timer, quint64 QString #if // _udp->writeDatagram(str.toUtf8(), #else // // // _udp->writeDatagram(str.toUtf8(), #endif }); } void { while(_udp->hasPendingDatagrams()) { quint32 QByteArray _udp->readDatagram(buf.data(), qDebug() } } |
Udp2.h |
#ifndef #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp2.cpp |
#include #include #include #include Udp2::Udp2(QWidget QWidget(parent) { _udp // _udp->bind(QHostAddress::AnyIPv4, // _udp->joinMulticastGroup(QHostAddress("224.0.0.131")); connect(_udp, this, QTimer* timer->setInterval(1000); timer->start(); connect(timer, quint64 QString _udp->writeDatagram(str.toUtf8(), }); } void { while(_udp->hasPendingDatagrams()) { quint32 QByteArray _udp->readDatagram(buf.data(), qDebug() } } |
运行结果: 控制台输出结果如下: |