QT学习(8)UDP通信

(1)在“UdpServer.pro”中添加如下语句:

      #include <QDialog>
      #include <QLabel>
      #include <QLineEdit>
      #include <QPushButton>
      #include <QVBoxLayout>
 
       class UdpServer : public QDialog
       {
             Q_OBJECT
 
         public:
              UdpServer(QWidget *parent = 0 ,Qt::WindowFlags f=0);
              ~UdpServer();
          private:
               QLabel *TimerLabel;
               QLineEdit *TextLineEdit;
               QPushButton *StartBtn;
               QVBoxLayout *mainLayout;
        };

(2)源文件“udpserver.cpp”的具体代码如下:
        #include "udpserver.h"
        UdpServer::UdpServer(QWidget *parent , Qt::WindowFlags f)
              : QDialog(parent,f)
         {
                setWindowTitle(tr("UDP Server"));
 
                TimerLabel = new QLabel(tr("计时器:"),this);
                TextLineEdit = new QLineEdit(this);
                StartBtn = new QPushButton(tr("开始"),this);
 
                 mainLayout = new QVBoxLayout(this);
                 mainLayout->addWidget(TimerLabel);
                 mainLayout->addWidget(TextLineEdit);
                 mainLayout->addWidget(StartBtn);
          }

(3)服务器界面运行外观如图所示。

                                  

(1)在“UdpServer.pro”中添加如下语句:
                QT += network
(2)在头文件“udpserver.h”中添加需要的槽函数,其具体代码如下:
                #include <QUdpSocket>
                #include <QTimer>
                public slots:
                       void StartBtnClicked();
                       void timeout();
                 private:
                        int port;
                        bool isStarted;
                        QUdpSocket *udpSocket;
                        QTimer *timer;
(3)在源文件“udpserver.cpp”中添加声明:
                #include <QHostAddress>

              StartBtnClicked()函数的具体代码如下:
               void UdpServer::StartBtnClicked()
               {
                     if(!isStarted)
                     {
                           StartBtn->setText(tr("停止"));
                           timer->start(1000);
                           isStarted =true;
                        }
                       else
                      {
                            StartBtn->setText(tr("开始"));
                             isStarted = false;
                             timer->stop();
                         }
                     }
 

timeout()函数完成了向端口发送广播信息的功能,其具体代码如下:
void UdpServer::timeout()
{
    QString msg = TextLineEdit->text();
    int length=0;
    if(msg=="")
    {
       return;
    }
    if((length=udpSocket->writeDatagram(msg.toLatin1(),
    msg.length(),QHostAddress::Broadcast,port))!=msg.length())
    {
        return;
    }
}

(1)在头文件“udpclient.h”中声明了需要的各种控件,其具体代码如下:
#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
 
class UdpClient : public QDialog
{
    Q_OBJECT
 
public:
    UdpClient(QWidget *parent = 0 ,Qt::WindowFlags f=0);
    ~UdpClient();
 
private:
    QTextEdit *ReceiveTextEdit;
    QPushButton *CloseBtn;
    QVBoxLayout *mainLayout;
};

(2)源文件“udpclient.cpp”的具体代码如下:
#include "udpclient.h"
UdpClient::UdpClient(QWidget *parent , Qt::WindowFlags f)
    : QDialog(parent,f)
{
    setWindowTitle(tr("UDP Client"));
 
    ReceiveTextEdit = new QTextEdit(this);
    CloseBtn = new QPushButton(tr("Close"),this);
 
    mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(ReceiveTextEdit);
    mainLayout->addWidget(CloseBtn);
}

(3)客户端界面运行外观如图所示

                                                         

以上只是完成了客户端界面的实现,下面完成它的数据接收和显示的功能。
具体操作步骤如下。
(1)在“UdpClient.pro”中添加如下语句:
QT += network
(2)在头文件“udpclient.h”中添加以下代码:
#include <QUdpSocket>
 
public slots:
        void CloseBtnClicked();
        void dataReceived();
 
private:
    int port;
        QUdpSocket *udpSocket;
(3)在源文件“udpclient.cpp”中添加如下声明:
#include <QMessageBox>
#include <QHostAddress

同时运行UdpServerUdpClient工程,首先在服务器界面文本框中输入“hello!”,然后单击“开始”按钮,按钮文本变为“停止”,客户端就开始不断地收到“hello!”字符消息并显示在文本区,当单击服务器的“停止”按钮后,按钮文本又变回“开始”,客户端也就停止了字符的显示,再次单击服务器的“开始”按钮,客户端又继续接收并显示……如此循环往复。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值