Qt Socket通信(UDP&TCP)

socket简介

Linux下进行网络编程,我们可以使用LINUX提供的统一的套接字接口。但是这种方法牵涉到太多的结构体,比如IP地址,端口转换等,不熟练的人往往容易犯这样那样的错误。QT中提供的SOCKET完全使用了类的封装机制,使用户不需要接触底层的各种结构体操作。而且它采用QT本身的signal-slot机制,使编写的程序更容易理解。

这是文档。个人觉得,QT的文档除了缺少一些例子,其他还是不错的。

QT5中相比于QT4应该更新了一些socket的应用,QT4相比于QT3也更新了不少,而且还改了很多的类名,大家在网上找资料的时候一定要注意。

UDP通信

UDP没有特定的server端和client端,简单来说就是向特定的ip发送报文,因此我把它分为发送端和接收端。

注意:在.pro文件中要添加QT += network,否则无法使用Qt的网络功能。

发送端

#include <QtNetwork>
QUdpSocket *sender;
sender = new QUdpSocket(this);

QByteArray datagram = “hello world!”;

//UDP广播
sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);

//向特定IP发送
QHostAddress serverAddress = QHostAddress("10.21.11.66");
sender->writeDatagram(datagram.data(), datagram.size(),serverAddress, 6665);

/* writeDatagram函数原型,发送成功返回字节数,否则-1
qint64 writeDatagram(const char *data,qint64 size,const QHostAddress &address,quint16 port)
qint64 writeDatagram(const QByteArray &datagram,const QHostAddress &host,quint16 port)
*/

UDP接收端

#include <QtNetwork>
QUdpSocket *receiver;

//信号槽
private slots:  
    void readPendingDatagrams(); 

receiver = new QUdpSocket(this);
receiver->bind(QHostAddress::LocalHost, 6665);
connect(receiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));

void readPendingDatagrams()
 {
     while (receiver->hasPendingDatagrams()) {
         QByteArray datagram;
         datagram.resize(receiver->pendingDatagramSize());
         receiver->readDatagram(datagram.data(), datagram.size());
         //数据接收在datagram里
/* readDatagram 函数原型
qint64 readDatagram(char *data,qint64 maxSize,QHostAddress *address=0,quint16 *port=0)
*/
     }
 }

TCP通信

TCP的话要复杂点,必须先建立连接才能传输数据,分为server端和client端。

TCP client端

#include <QtNetwork>
QTcpSocket *client;
char *data="hello qt!";
client = new QTcpSocket(this);
client->connectToHost(QHostAddress("10.21.11.66"), 6665);
client->write(data);

TCP server端


#include <QtNetwork>
QTcpServer *server;
QTcpSocket *clientConnection;
server = new QTcpServer();
server->listen(QHostAddress::Any, 6665);
connect(server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
void acceptConnection()
{
    clientConnection = server->nextPendingConnection();
    connect(clientConnection, SIGNAL(readyRead()), this, SLOT(readClient()));
}
void readClient()
{
    QString str = clientConnection->readAll();
    //或者
    char buf[1024];
    clientConnection->read(buf,1024);
}

原文:http://blog.csdn.net/u013007900/article/details/50411796

Qt中进行UDPTCP通信的流程和API如下: UDP通信流程: 1. 创建QUdpSocket对象 2. 绑定本地IP地址和端口号 3. 发送数据:使用QHostAddress和quint16参数创建目标地址,使用QUdpSocket的writeDatagram()函数发送数据 4. 接收数据:使用QUdpSocket的readyRead()信号和readDatagram()函数接收数据 UDP通信API: 1. 创建QUdpSocket对象:QUdpSocket *udpSocket = new QUdpSocket(this); 2. 绑定本地IP地址和端口号:udpSocket->bind(QHostAddress::AnyIPv4, port); 3. 发送数据:udpSocket->writeDatagram(data, QHostAddress(targetIP), targetPort); 4. 接收数据:connect(udpSocket, &QUdpSocket::readyRead, this, &MyClass::processPendingDatagrams); void MyClass::processPendingDatagrams() { while (udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size()); // 处理接收到的数据 } } TCP通信流程: 1. 创建QTcpServer对象 2. 监听端口号:使用QTcpServer的listen()函数监听端口号 3. 接受连接:使用QTcpServer的newConnection()信号和nextPendingConnection()函数接受连接请求,创建QTcpSocket对象 4. 发送数据:使用QTcpSocket的write()函数发送数据 5. 接收数据:使用QTcpSocket的readyRead()信号和read()函数接收数据 TCP通信API: 1. 创建QTcpServer对象:QTcpServer *tcpServer = new QTcpServer(this); 2. 监听端口号:tcpServer->listen(QHostAddress::AnyIPv4, port); 3. 接受连接: connect(tcpServer, &QTcpServer::newConnection, this, &MyClass::processNewConnection); void MyClass::processNewConnection() { QTcpSocket *clientSocket = tcpServer->nextPendingConnection(); connect(clientSocket, &QTcpSocket::readyRead, this, &MyClass::processReadyRead); } 4. 发送数据:clientSocket->write(data); 5. 接收数据: void MyClass::processReadyRead() { QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender()); QByteArray data = clientSocket->readAll(); // 处理接收到的数据 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值