QT实现局域网通信(QUdpSocket)

界面


 

QT实现局域网通信(QUdpSocket)_第1张图片

 

 启动


  • vs2010 + qt4.7.0
  • 如果提示缺少qtCore4.dll,需要将 .../qt4.7.0/lib/目录加入到环境变量
  • 机子必须在同一局域网下,使用ipconfig命令查找本机ipv4地址
  • 端口默认是8888,需要改的话将udp_socket->bind(8888) 这个改了
  1. 多个机子同时运行这个程序
  2. 运行成功后,一台机子在输入栏输入对方的ip和端口
  3. 在文本编辑框里写入要发送的字符串,点击发送,对方就会收到

 

代码


ui_widget.h

/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created: Wed Jun 24 13:31:41 2020
**      by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

QT_BEGIN_NAMESPACE

class Ui_WidgetClass
{
public:
    QGridLayout *gridLayout;
    QLabel *label;
    QLabel *label_2;
    QLineEdit *lineEditPort;
    QTextEdit *textEdit;
    QPushButton *ButtonSend;
    QSpacerItem *horizontalSpacer;
    QPushButton *ButtonClose;
    QLineEdit *lineEditIp;

    void setupUi(QWidget *WidgetClass)
    {
        if (WidgetClass->objectName().isEmpty())
            WidgetClass->setObjectName(QString::fromUtf8("WidgetClass"));
        WidgetClass->resize(410, 425);
        gridLayout = new QGridLayout(WidgetClass);
        gridLayout->setSpacing(6);
        gridLayout->setContentsMargins(11, 11, 11, 11);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        label = new QLabel(WidgetClass);
        label->setObjectName(QString::fromUtf8("label"));

        gridLayout->addWidget(label, 0, 0, 1, 1);

        label_2 = new QLabel(WidgetClass);
        label_2->setObjectName(QString::fromUtf8("label_2"));

        gridLayout->addWidget(label_2, 1, 0, 1, 1);

        lineEditPort = new QLineEdit(WidgetClass);
        lineEditPort->setObjectName(QString::fromUtf8("lineEditPort"));

        gridLayout->addWidget(lineEditPort, 1, 1, 1, 2);

        textEdit = new QTextEdit(WidgetClass);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));

        gridLayout->addWidget(textEdit, 2, 0, 1, 3);

        ButtonSend = new QPushButton(WidgetClass);
        ButtonSend->setObjectName(QString::fromUtf8("ButtonSend"));

        gridLayout->addWidget(ButtonSend, 3, 0, 1, 1);

        horizontalSpacer = new QSpacerItem(227, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        gridLayout->addItem(horizontalSpacer, 3, 1, 1, 1);

        ButtonClose = new QPushButton(WidgetClass);
        ButtonClose->setObjectName(QString::fromUtf8("ButtonClose"));

        gridLayout->addWidget(ButtonClose, 3, 2, 1, 1);

        lineEditIp = new QLineEdit(WidgetClass);
        lineEditIp->setObjectName(QString::fromUtf8("lineEditIp"));

        gridLayout->addWidget(lineEditIp, 0, 1, 1, 2);


        retranslateUi(WidgetClass);
        QObject::connect(ButtonSend, SIGNAL(clicked()), WidgetClass, SLOT(sendMessage()));
        QObject::connect(ButtonClose, SIGNAL(clicked()), WidgetClass, SLOT(closeSocket()));

        QMetaObject::connectSlotsByName(WidgetClass);
    } // setupUi

    void retranslateUi(QWidget *WidgetClass)
    {
        WidgetClass->setWindowTitle(QApplication::translate("WidgetClass", "Widget", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("WidgetClass", " \345\257\271\346\226\271\347\232\204IP", 0, QApplication::UnicodeUTF8));
        label_2->setText(QApplication::translate("WidgetClass", " \345\257\271\346\226\271\347\232\204\347\253\257\345\217\243", 0, QApplication::UnicodeUTF8));
        lineEditPort->setText(QApplication::translate("WidgetClass", "8888", 0, QApplication::UnicodeUTF8));
        ButtonSend->setText(QApplication::translate("WidgetClass", "send", 0, QApplication::UnicodeUTF8));
        ButtonClose->setText(QApplication::translate("WidgetClass", "close", 0, QApplication::UnicodeUTF8));
        lineEditIp->setText(QString());
    } // retranslateUi

};

namespace Ui {
    class WidgetClass: public Ui_WidgetClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

 

widget.h

#pragma execution_character_set("utf-8")
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include "ui_widget.h"

class Widget : public QWidget
{
	Q_OBJECT

public:
	Widget(QWidget *parent = 0, Qt::WFlags flags = 0);
	~Widget();

private:
	Ui::WidgetClass ui;

	QUdpSocket *udp_socket;

	public slots:
		void getMessage() ;
		void sendMessage() ;
		void closeSocket();
};

#endif // WIDGET_H

 

widgert.cpp

#include "widget.h"

Widget::Widget(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	ui.setupUi(this);

	setWindowTitle(QString::fromUtf8("端口:8888")) ;

	udp_socket = new QUdpSocket(this) ;

	//绑定
	udp_socket->bind(8888) ;

	//当对方成功发送数据过来
	//自动触发readyRead()
	connect(udp_socket, SIGNAL(readyRead()), this, SLOT(getMessage())) ;

}

Widget::~Widget()
{

}

void Widget::getMessage()
{
	char buf[1024] ={0};
	QHostAddress client_address;
	quint16 client_port;
	//读取对方发送的内容
	qint64 len = udp_socket->readDatagram(buf, sizeof(buf), &client_address, &client_port) ;

	if(len > 0)
	{
		QString str = QString("[%1:%2] %3").arg(client_address.toString())
		.arg(client_port).arg(buf) ;

		ui.textEdit->setText(str);
	}
}

void Widget::sendMessage()
{
	QString ip = ui.lineEditIp->text();
	QString port = ui.lineEditPort->text();

	QString str = ui.textEdit->toPlainText();

	udp_socket->writeDatagram(str.toUtf8(), QHostAddress(ip), (quint16)port.toInt());
}

void Widget::closeSocket()
{
	udp_socket->disconnect();
	udp_socket->close();
}

 

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	Widget w;
	w.show();
	return a.exec();
}

 

你可能感兴趣的:(QT)