udp通信
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了udp通信相关的知识,希望对你有一定的参考价值。
UDP(User Data Protocol)用户数据报协议,是一种不可靠,面向数据报的无连接的传输层协议。
UDP客户端向UDP服务器发送一定长度的请求报文,UDP服务器同样以报文的形式作出响应,如果服务器为收到此请求,客户端不会进行重发,故此报文的传输是不可靠的;
发送需要知道:对方IP地址,端口号
接收方:端口号
主要是针对局域网内一对一的传输,两台PC可以进行通信;
操作界面如下: 其中远程端IP和端口号可更改;
本端发送消息,本端接收端会显示发送的内容,同时发送的内容会被清空;
下图显示的内容是 本端先发送内容“你好”给远程端,远程端操作进行回复,图片显示的结果说明两端通信正常;
#ifndef SERVER_H
#define SERVER_H
#include<QApplication>
#include <QWidget>
#include<QLabel>
#include<QLineEdit>
#include<QTextEdit>
#include<QPushButton>
#include<QGridLayout>
#include<QHBoxLayout>
#include<QMessageBox>
#include<QtNetwork/QUdpSocket>
#include<QtNetwork/QHostAddress>
#include<QtNetwork/QNetworkInterface>
#include<QtNetwork/QHostInfo>
#include<QTime>
class server : public QWidget
{
Q_OBJECT
public:
server(QWidget *parent = 0);
~server();
QHostAddress localHostAddr;
QHostAddress remoteHostAddr;
QTime time;
QString timeStr;
QUdpSocket *sendSocket;
QUdpSocket *receiveSocket;
int port;
void autoScroll(QTextEdit& Edit);
QString getIP();
QString localIP;
private slots:
void send();
void receive();
void InitState();
void close();
private:
QLabel *RemoteIpLabel;
QLineEdit *RemoteIpLineEdit;
QLabel *MyselfIpLabel;
QLineEdit *MyselfIpLineEdit;
QLabel *PortLabel;
QLineEdit *PortLineEdit;
QTextEdit *RecvTextEdit;
QTextEdit *SendTextEdit;
QLabel *SendLabel;
QLabel *RecvLabel;
QPushButton *ClearBtn;
QPushButton *ExitBtn;
QPushButton *SendBtn;
QGridLayout *mainLayout;
QGridLayout *TopLayout;
QVBoxLayout *SendLayout;
QVBoxLayout *RecvLayout;
};
#endif // SERVER_H
#include "server.h" #include<QCoreApplication> #include<QList> #include<QByteArray> #include<QtNetwork/QHostAddress> #include<QtNetwork/QUdpSocket> #include<QtNetwork/QAbstractSocket> #include<QtNetwork/QNetworkInterface> #include<QtNetwork/QHostInfo> #include<QMessageBox> #include<QByteArray> #include<QTextCursor> server::server(QWidget *parent) : QWidget(parent) { RemoteIpLabel=new QLabel(tr("远程端IP:")); RemoteIpLineEdit=new QLineEdit("192.168.40.101"); MyselfIpLabel=new QLabel("本端IP"); MyselfIpLineEdit=new QLineEdit; MyselfIpLineEdit->setText(getIP()); MyselfIpLineEdit->setEnabled(false); PortLabel=new QLabel(tr("端口号:")); PortLineEdit=new QLineEdit("5555"); RecvLabel=new QLabel(tr("信息交互")); RecvTextEdit=new QTextEdit; SendLabel=new QLabel(tr("发送信息")); SendTextEdit=new QTextEdit; ClearBtn=new QPushButton(tr("clear")); ExitBtn=new QPushButton(tr("exit")); SendBtn=new QPushButton(tr("发送")); TopLayout=new QGridLayout; TopLayout->addWidget(MyselfIpLabel,0,0); TopLayout->addWidget(MyselfIpLineEdit,0,1); TopLayout->addWidget(RemoteIpLabel,1,0); TopLayout->addWidget(RemoteIpLineEdit,1,1); TopLayout->addWidget(PortLabel,1,2); TopLayout->addWidget(PortLineEdit,1,3); SendLayout=new QVBoxLayout; SendLayout->addWidget(SendLabel); SendLayout->addWidget(SendTextEdit); RecvLayout=new QVBoxLayout; RecvLayout->addWidget(RecvLabel); RecvLayout->addWidget(RecvTextEdit); mainLayout=new QGridLayout(this); mainLayout->addLayout(TopLayout,0,0); mainLayout->addLayout(RecvLayout,1,0); mainLayout->addLayout(SendLayout,2,0); mainLayout->addWidget(SendBtn,3,1,2,2); mainLayout->addWidget(ExitBtn,3,3,2,2); // int port=0; sendSocket=new QUdpSocket(this); QTime time=QTime::currentTime(); timeStr=time.toString("hh:mm:ss"); InitState(); connect(SendBtn,SIGNAL(clicked()),this,SLOT(send())); connect(ExitBtn,SIGNAL(clicked()),this,SLOT(close())); } server::~server() { } void server::send() { sendSocket->open(QIODevice::Append); qDebug()<<"发送信息"; bool ok; autoScroll(*RecvTextEdit);//滚屏 localIP=getIP();//获取本地IP地址 remoteHostAddr=QHostAddress(RemoteIpLineEdit->text()); port=PortLineEdit->text().toInt(&ok); sendSocket->bind(QHostAddress::AnyIPv4,port); QByteArray datagram =localIP.toUtf8()+" \\t"+"\\n"+SendTextEdit->toPlainText().toUtf8(); if(SendTextEdit->toPlainText().toUtf8()!="") { qDebug()<<sendSocket->writeDatagram(datagram.data(),datagram.size(),QHostAddress(remoteHostAddr),5555); RecvTextEdit->append(datagram+"\\t"); SendTextEdit->clear(); } else { qDebug()<<"输入为空"; QMessageBox::about(this,"消息提示框","发送内容不能为空!"); } } void server::receive() { while(sendSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(sendSocket->pendingDatagramSize()); // QDateTime timerecv=QDateTime::currentDateTime(); //QString recvtimeStr=timerecv.toString("yyyy-MM-dd hh:mm:ss"); QHostAddress sender; quint16 sendPort; autoScroll(*RecvTextEdit); //自动滚屏 sendSocket->readDatagram(datagram.data(),datagram.size(),&sender,&sendPort); if(datagram!=NULL) { RecvTextEdit->append(datagram+"\\t"); } } } void server::InitState() { sendSocket->open(QIODevice::Append); SendTextEdit->clear(); qDebug()<<"接收信息"; sendSocket->bind(5555); connect(sendSocket,SIGNAL(readyRead()),this,SLOT(receive())); } void server::autoScroll(QTextEdit& Edit) { QTextCursor cursor= Edit.textCursor(); cursor.movePosition(QTextCursor::End); Edit.setTextCursor(cursor); } QString server::getIP() { QString ip; QHostInfo info =QHostInfo::fromName(QHostInfo::localHostName()); info.addresses(); foreach(QHostAddress address,info.addresses()) { if(address.protocol()==QAbstractSocket::IPv4Protocol) { qDebug()<<address.toString(); ip=address.toString(); } } return ip; } void server ::close() { QApplication *serverApp; serverApp->quit(); }
以上是关于udp通信的主要内容,如果未能解决你的问题,请参考以下文章