#include "udptwo.h"
#include <QtGui/QMessageBox>
udptwo::udptwo(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
m_udpSocket1 = new QUdpSocket(this);
m_udpSocket2 = new QUdpSocket(this);
m_addr1 = new QHostAddress("127.0.0.1");
m_addr2 = new QHostAddress("127.0.0.1");
bool bConn = m_udpSocket1->bind(*m_addr1, 5556, QUdpSocket::ShareAddress);
if(!bConn)
{
QMessageBox box;
box.setText("link error");
box.exec();
}
else
{
connect(m_udpSocket1, SIGNAL(readyRead()), this, SLOT(receive()));
}
}
udptwo::~udptwo()
{
}
void udptwo::on_ptn_send_clicked()
{
QMessageBox box;
QString text= ui.te_send->toPlainText();
if(text.length() == 0)
{
box.setText(tr("请输入发送内容"));
box.exec();
}
m_udpSocket2->writeDatagram(text.toUtf8(),text.length(), *m_addr2,5555);
}
void udptwo::receive()
{
while(m_udpSocket1->hasPendingDatagrams())
{
QByteArray data;
data.resize(m_udpSocket1->pendingDatagramSize());
m_udpSocket1->readDatagram(data.data(),data.size());
QString str = data.data();
ui.te_receive->insertPlainText(str+"\n");
}
}