qt的udp通讯

admin2024-05-15  1

QString mylocalip;
const QList interfaces = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface ip, interfaces)
{
if (ip.humanReadableName() == QStringLiteral(“以太网”))
{
//if (ip.type() == QNetworkInterface::Ethernet)
{
const QList iplist = ip.addressEntries();
foreach(QNetworkAddressEntry net, iplist)
{
mylocalip = net.ip().toString();
}
}
}
}
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress(mylocalip), 10004/, QUdpSocket::ShareAddress/);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(VoiceReadyReadSlot()));
void VideoForm::VoiceReadyReadSlot()
{
video vp1;
int type = 0;
memset(&vp1, 0, sizeof(vp1));
while (udpSocket->hasPendingDatagrams()) {

	//int len = udpSocket->pendingDatagramSize();   //获取字节数
	//char* buf = new char[len];
	udpSocket->readDatagram((char*)&vp1, sizeof(vp1));
	}
	if(udpSocket!=NULL)
	udpSocket->writeDatagram((const char*)&vp, sizeof(vp), QHostAddress(friendip), 1004);   //给对方发送

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!