基于QT的UDP协议的通信小程序
2009-09-07 13:07
507 查看
////////1端/////////////////////////////////////////////
////////////////main1.cpp///////////////////
#include <QApplication>
#include "udp1.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
Dialog dlg;
dlg.show();
return app.exec();
}
///////////////udp1.h///////////////////////
#ifndef _UDP1_H_
#define _UDP1_H_
#include <QDialog.h>
#include <QTextEdit>
#include <QPushButton>
#include <UdpSocket>
#include <QGridLayout>
class Dialog :public QDialog
{
Q_OBJECT
public :
Dialog();
private:
QTextEdit *textEdit;
QPushButton *sendButton;
QPushButton *exitButton;
QUdpSocket *udpSocket;
QGridLayout *layout;
public slots:
void send_slot();
void recv_slot();
};
#endif
//////////////////udp1.cpp///////////////////////////////////////
#include "udp1.h"
#include <QtDebug>
Dialog::Dialog()
{
sendButton = new QPushButton("send");
exitButton = new QPushButton("exit");
textEdit = new QTextEdit();
layout = new QGridLayout;
layout -> addWidget(textEdit,0,0,4,6);
layout -> addWidget(sendButton,5,0,1,2);
layout -> addWidget(exitButton,5,4,1,2);
setLayout(layout);
udpSocket = new QUdpSocket;
udpSocket -> bind(QHostAddress::LocalHost,9000);
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()))
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
}
void Dialog::send_slot()
{
qDebug()<<"in send_slot";
QString str = textEdit -> toPlainText();
udpSocket -> writeDatagram(qPrintable(str),4096,QHostAddress::LocalHost,8000);
}
void Dialog::recv_slot()
{
qDebug()<<"in recv_slot()";
char buffer[4096];
udpSocker -> readDatagram(buffer,4096,0,0);
QString str(buffer);
textEdit -> setText(str);
}
/////////////2端////////////////////////////////////////////////////
////////////////////////////main2.cpp///////////////////////////////
#include <QApplication>
#include "udp2.h"
int main(int argc,char **argv)
{
QApplication app(argv,argv);
Dialog dlg;
dlg.show();
return app.exec();
}
//////////////////////////udp2.h/////////////////////////////////////
#ifndef _UDP_H_
#define _UDP_H_
#include <QDialog>
#include <QPushButton>
#include <QTextEdit>
#include <QUdpSocket>
#inlcude <QGridLayout>
class Dialog:public QDialog
{
Q_OBJECT
public:
Dialog();
private:
QPushButton *sendButton;
QPushButton *exitButton;
QTextEdit *textEdit;
QUdpSocket *udpSocket;
QGridLayout *layout;
public slots:
void send_slot();
void recv_slot();
};
#endif
//////////////////////////udp.cpp////////////////////////////////////
#include "udp2.h"
#include <QtDebug>
Dialog::Dialog()
{
sendButton = new QPushButton("send");
exitButton = new QPushButton("exit");
textEdit = new QTextEdit();
layout = new QGridLayout;
layout -> addWidget(textEdit,0,0,4,6);
layout -> addWidget(sendButton,5,0,1,2);
layout -> addWidget(exitButton,5,4,1,2);
setLayout(layout);
udpSocket = new QUdpSocket;
udpSocket -> bind(QHostAddress::LocalHost,8000);
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
}
void Dialog::send_slot()
{
qDebug()<<"in send_slot";
QString str = textEdit -> toPlainText();
udpSocket -> writeDatagram(qPrintable(str),4096,QHostAddress::LocalHost, 9000);
// udpSocket -> writeDatagram(qPrintable(textEdit -> toPlainText()), 256, QHostAddress::LocalHost, 9000);
}
void Dialog::recv_slot()
{
qDebug()<<"in recv_slot";
char buffer[4096];
udpSocket ->readDatagram(buffer,4096,0,0);
QString str(buffer);
textEdit -> setText(str);
}
////////////////main1.cpp///////////////////
#include <QApplication>
#include "udp1.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
Dialog dlg;
dlg.show();
return app.exec();
}
///////////////udp1.h///////////////////////
#ifndef _UDP1_H_
#define _UDP1_H_
#include <QDialog.h>
#include <QTextEdit>
#include <QPushButton>
#include <UdpSocket>
#include <QGridLayout>
class Dialog :public QDialog
{
Q_OBJECT
public :
Dialog();
private:
QTextEdit *textEdit;
QPushButton *sendButton;
QPushButton *exitButton;
QUdpSocket *udpSocket;
QGridLayout *layout;
public slots:
void send_slot();
void recv_slot();
};
#endif
//////////////////udp1.cpp///////////////////////////////////////
#include "udp1.h"
#include <QtDebug>
Dialog::Dialog()
{
sendButton = new QPushButton("send");
exitButton = new QPushButton("exit");
textEdit = new QTextEdit();
layout = new QGridLayout;
layout -> addWidget(textEdit,0,0,4,6);
layout -> addWidget(sendButton,5,0,1,2);
layout -> addWidget(exitButton,5,4,1,2);
setLayout(layout);
udpSocket = new QUdpSocket;
udpSocket -> bind(QHostAddress::LocalHost,9000);
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()))
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
}
void Dialog::send_slot()
{
qDebug()<<"in send_slot";
QString str = textEdit -> toPlainText();
udpSocket -> writeDatagram(qPrintable(str),4096,QHostAddress::LocalHost,8000);
}
void Dialog::recv_slot()
{
qDebug()<<"in recv_slot()";
char buffer[4096];
udpSocker -> readDatagram(buffer,4096,0,0);
QString str(buffer);
textEdit -> setText(str);
}
/////////////2端////////////////////////////////////////////////////
////////////////////////////main2.cpp///////////////////////////////
#include <QApplication>
#include "udp2.h"
int main(int argc,char **argv)
{
QApplication app(argv,argv);
Dialog dlg;
dlg.show();
return app.exec();
}
//////////////////////////udp2.h/////////////////////////////////////
#ifndef _UDP_H_
#define _UDP_H_
#include <QDialog>
#include <QPushButton>
#include <QTextEdit>
#include <QUdpSocket>
#inlcude <QGridLayout>
class Dialog:public QDialog
{
Q_OBJECT
public:
Dialog();
private:
QPushButton *sendButton;
QPushButton *exitButton;
QTextEdit *textEdit;
QUdpSocket *udpSocket;
QGridLayout *layout;
public slots:
void send_slot();
void recv_slot();
};
#endif
//////////////////////////udp.cpp////////////////////////////////////
#include "udp2.h"
#include <QtDebug>
Dialog::Dialog()
{
sendButton = new QPushButton("send");
exitButton = new QPushButton("exit");
textEdit = new QTextEdit();
layout = new QGridLayout;
layout -> addWidget(textEdit,0,0,4,6);
layout -> addWidget(sendButton,5,0,1,2);
layout -> addWidget(exitButton,5,4,1,2);
setLayout(layout);
udpSocket = new QUdpSocket;
udpSocket -> bind(QHostAddress::LocalHost,8000);
connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
}
void Dialog::send_slot()
{
qDebug()<<"in send_slot";
QString str = textEdit -> toPlainText();
udpSocket -> writeDatagram(qPrintable(str),4096,QHostAddress::LocalHost, 9000);
// udpSocket -> writeDatagram(qPrintable(textEdit -> toPlainText()), 256, QHostAddress::LocalHost, 9000);
}
void Dialog::recv_slot()
{
qDebug()<<"in recv_slot";
char buffer[4096];
udpSocket ->readDatagram(buffer,4096,0,0);
QString str(buffer);
textEdit -> setText(str);
}
相关文章推荐
- 基于QT的UDP协议的通信小程序
- 基于QT的UDP协议的通信
- C语言编写基于TCP和UDP协议的Socket通信程序示例
- 基于qt的一个udp通信小程序
- C语言编写基于TCP和UDP协议的Socket通信程序示例
- 使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
- 在QT下的基于UDP协议的消息收发程序
- [置顶] linux网络编程之socket(十四):基于UDP协议的网络程序
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Linux中使用C语言实现基于UDP协议的Socket通信示例
- vs2015基于UDP协议的简单通信例程
- 基于UDP协议的双端通信
- Java学习系列(十八)Java面向对象之基于UDP协议的网络通信
- Java学习系列(十八)Java面向对象之基于UDP协议的网络通信
- 初识用UDP协议做的windows窗口聊天程序---此处是双向的"异步"多线程对话框通信.
- Linux下基于QT的串口通信程序(Tiny6410)
- Qt---基于UDP的网络广播程序
- linux网络编程之socket(十四):基于UDP协议的网络程序
- 基于UDP协议的简单基本视频传输程序的编写
- QT基于UDP通信的多线程编程问题