??
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 CHOOSEINTERFACE_H #define #include #include class { Q_OBJECT public: explicit QComboBox* QString signals: public void }; #endif |
编写ChooseInterface.cpp
#include "ChooseInterface.h" #include #include ChooseInterface::ChooseInterface(QWidget QDialog(parent) { /* QList<QHostAddress> #if QList<QNetworkInterface> QList<QNetworkAddressEntry> entryList.at(0).broadcast() #endif //编写一个下拉选 _comboBox QVBoxLayout* lay->addWidget(_comboBox); foreach(QHostAddress { //将地址都转换成为ipv4的地址 quint32 //去掉0的ip if(ipaddr continue; //在comboBox中加入下拉选 _comboBox->addItem(QHostAddress(ipaddr).toString()); } //当下拉选发生变化时的操作 connect(_comboBox, this, } void { this->_strSelect } |
上面的执行结果是:
编写TcpServer.h |
#ifndef TCPSERVER_H #define #include #include #include #include class { Q_OBJECT public: explicit QTcpServer* QTcpSocket* QTextBrowser* signals: public void void }; #endif |
编写TcpServer.cpp |
#include "TcpServer.h" #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 TCPCLIENT_H #define #include #include #include class { Q_OBJECT public: explicit QTcpSocket* QLineEdit* signals: public void }; #endif |
编写TcpClient.cpp |
#include "TcpClient.h" #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 MYWIDGET_H #define #include class { Q_OBJECT public: explicit signals: public }; #endif |
MyWidget.cpp |
#include "MyWidget.h" #include #include #include MyWidget::MyWidget(QWidget QWidget(parent) { } int { QApplication TcpServer TcpClient s.setWindowTitle("server"); c.setWindowTitle("client"); return } |
执行结果:
2
编写UDP程序
UDPServer.pro |
QT += gui widgets network CONFIG HEADERS Udp1.h Udp2.h MyWidget.h SOURCES Udp1.cpp Udp2.cpp MyWidget.cpp |
Udp1.h |
#ifndef UDP1_H #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp1.cpp |
#include "udp1.h" #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 UDP2_H #define #include #include class { Q_OBJECT public: explicit QUdpSocket* signals: public void }; #endif |
Udp2.cpp |
#include "udp2.h" #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() } } |
执行结果: 控制台输出结果例如以下: |