The communication of UDP protocol based on QT
#-------------------------------------------------# # Project created by Qtcreator 2014-05-22t21:39:26##------------ -------------------------------------QT + + core GUIQT + = Networkgreaterthan (Qt_major_version, 4): qt + = Widgetstarget = Udpchattemplate = appsources + main.cpp dialog.cppheaders + = Dialog.h
#ifndef dialog_h#define dialog_h#include <QDialog> #include <QLabel> #include <QPushButton> #include <QLineEdit> #include <qudpsocket>class dialog:public qdialog{ q_objectpublic: Dialog (Qwidget * parent = 0); ~dialog ();p rivate: qlabel *label1, *label2, *label3, *label4; Qlineedit *edit1, *edit2, *edit3, *EDIT4; Qpushbutton *btn1, *btn2; Qudpsocket *udpsocket;private Slots: void on_clicked1 (); void On_clicked2 (); void DataReceived ();}; #endif//Dialog_h
#include "dialog.h" #include <QMessageBox> #include <QVBoxLayout> #include <QGridLayout> #include <QByteArray> #include <qhostaddress>dialog::D ialog (Qwidget *parent): Qdialog (parent) {udpsocket = new QU Dpsocket (this); Setwindowtitle (TR ("UDP chat"));//Set window title Label1 = new Qlabel; Label1->settext (TR ("Please enter the bind port number")); Label2 = new Qlabel; Label2->settext (TR ("Please enter the destination IP address")); Label3 = new Qlabel; Label3->settext (TR ("Please enter the destination port number")); Label4 = new Qlabel; Label4->settext (TR ("Please enter the message")); Edit1 = new Qlineedit; Edit2 = new Qlineedit; Edit3 = new Qlineedit; EDIT4 = new Qlineedit; BTN1 = new Qpushbutton; Btn1->settext (TR ("bind"));//Set BTN1 title BTN2 = new Qpushbutton; Btn2->settext (TR ("send"));//Set BTN2 title Qgridlayout *layout1 = new Qgridlayout (this); Layout1->addwidget (label1, 0, 0);//Add Label1 Control Layout1->addwidget (edit1, 0, 1);//Add Edit1 control Layout1->addwidge T (btn1, 1, 0);//Add BTN1 control layout1->AddWidget (Label2, 2, 0);//Add Label2 control Layout1->addwidget (Edit2, 2, 1);//Add Edit2 control Layout1->addwidget (LABEL3, 3, 0);//Add Label3 control Layout1->addwidget (EDIT3, 3, 1);//Add Label3 control Layout1->addwidget (label4, 4, 0);//Add Label4 Control Layout1->addwidget (EDIT4, 4, 1);//Add Label4 control Layout1->addwidget (BTN2, 5, 0);//Add BTN2 Control Connect (BTN1, SIGNAL (c Licked ()), this, SLOT (On_clicked1 ())); Connect (BTN2, SIGNAL (clicked ()), this, SLOT (On_clicked2 ())); Connect (Udpsocket, SIGNAL (Readyread ()), this, SLOT (datareceived ()));} Dialog::~dialog () {}void dialog::on_clicked1 () {udpsocket->close (); Udpsocket->bind (Edit1->text (). ToInt ());} void Dialog::on_clicked2 () {qhostaddress addr; Addr.setaddress (Edit2->text ()); int port = Edit3->text (). ToInt (); Udpsocket->writedatagram (Edit4->text (). ToUtf8 (), addr, port);} void Dialog::d atareceived ()//slot function {qhostaddress addr receiving socket message; Char buf[1024]; while (Udpsocket->haspendingdatagrams ()) { memset (buf, 0, sizeof (BUF)); Udpsocket->readdatagram (buf, sizeof (BUF), &addr); Qmessagebox::information (This, TR ("message"), TR ("from") + addr.tostring () + TR ("message: \ n") + buf); }}
QT:UDP Communications, "C + + institute"