#include <qvariant.h>#include <qtextedit.h>#include <qlayout.h>#include "listener.h"listener::listener(QWidget *parent, const char *name) : QDialog(parent, name), socketDevice(QSocketDevice::Datagram) { socketDevice.setBlocking(false); socketDevice.bind(QHostAddress("192.168.1.1"), 9090); socketNotifier = new QSocketNotifier(socketDevice.socket(),QSocketNotifier::Read,this); connect(socketNotifier, SIGNAL(activated(int)),this, SLOT(dataReceived())); setCaption("Listener"); textedit1 = new QTextEdit(this); QHBoxLayout *mainLayout = new QHBoxLayout(this); mainLayout->setMargin(11); mainLayout->setSpacing(6); mainLayout->addWidget(textedit1);}void listener::dataReceived() { textedit1->setText(""); char *data2; QByteArray datagram(socketDevice.bytesAvailable()); socketDevice.readBlock(data2, datagram.size()); textedit1->setText(data2);}
#ifndef LISTENER_H #define LISTENER_H #include <qdialog.h>#include <qsocketdevice.h>#include <qsocketnotifier.h> class QTextEdit;class listener : public QDialog{ Q_OBJECTpublic: listener(QWidget *parent = 0, const char *name = 0);signals:private slots: void dataReceived();private: QSocketDevice socketDevice; QSocketNotifier *socketNotifier; QTextEdit *textedit1;};#endif
char data[MAX_LEN]; int len = sock->readBlock( data, MAX_LEN-1 ); if ( len <= 0 ) return; data[len] = 0; QString message( data );
#include <qapplication.h>#include "listener.h"#include <qtextcodec.h> int main(int argc, char *argv[]){ QApplication app(argc, argv); QTextCodec::setCodecForCStrings( QTextCodec::codecForName("UTF8")); QTextCodec::setCodecForTr( QTextCodec::codecForName("UTF8")); QTextCodec::setCodecForLocale( QTextCodec::codecForName("UTF8")); listener list1; list1.ip="192.168.1.1"; list1.port=9090; list1.on(); app.setMainWidget(&list1); list1.show(); return app.exec();}
#ifndef LISTENER_H #define LISTENER_H #include <qdialog.h>#include <qsocketdevice.h>#include <qsocketnotifier.h> #include <qstring.h>class QTextEdit;class listener : public QDialog{ Q_OBJECTpublic: listener(QWidget *parent = 0, const char *name = 0); QString ip,text; int port; void on();signals:private slots: void dataReceived();private: QSocketDevice socketDevice; QSocketNotifier *socketNotifier; QTextEdit *textedit1;};#endif
#include <qvariant.h>#include <qtextedit.h>#include <qlayout.h>#include <qstring.h>#include "listener.h"listener::listener(QWidget *parent, const char *name) : QDialog(parent, name), socketDevice(QSocketDevice::Datagram) {// ip="192.168.1.1";// port=9090; setCaption("Listener"); textedit1 = new QTextEdit(this); QHBoxLayout *mainLayout = new QHBoxLayout(this); mainLayout->setMargin(11); mainLayout->setSpacing(6); mainLayout->addWidget(textedit1);}void listener::dataReceived() {int MAX_LEN = 255;char data[MAX_LEN];int len = socketDevice.readBlock( data, MAX_LEN-1 );if ( len <= 0 ) return;data[len] = 0;text = data;textedit1->append(data);}void listener::on(){ socketDevice.setBlocking(true); socketDevice.bind(QHostAddress(ip), port); socketNotifier = new QSocketNotifier(socketDevice.socket(),QSocketNotifier::Read,this);connect(socketNotifier, SIGNAL(activated(int)),this, SLOT(dataReceived()));}