C++ (Qt)#ifndef UDPMRECEIVER_H#define UDPMRECEIVER_H #include <QObject>#include <QtNetwork/QUdpSocket>#include <winsock2.h>#include <ws2tcpip.h>class UDPMReceiver : public QObject{ Q_OBJECTpublic: explicit UDPMReceiver(QObject *parent = 0); struct sockaddr_in TivaSinStruct; int TivaSocketId; int TivaPort; ip_mreq TivaMreq; QUdpSocket TivaUdpSocket;signals: public slots: void tiva_readTivaNetPacket();}; #endif // UDPMRECEIVER_H
C++ (Qt)#include "udpmreceiver.h"#include <stdio.h>#include <QtGui/QMessageBox>UDPMReceiver::UDPMReceiver(QObject *parent) : QObject(parent){ TivaPort=3838; TivaUdpSocket.bind(TivaPort,QUdpSocket::ShareAddress); int TivaSendingFd = TivaUdpSocket.socketDescriptor(); if(TivaSendingFd != -1) { ip_mreq mreq; memset(&mreq,0,sizeof(ip_mreq)); mreq.imr_multiaddr.s_addr = inet_addr("238.38.38.38"); // group addr mreq.imr_interface.s_addr = htons(INADDR_ANY); // use default //Make this a member of the Multicast Group if(setsockopt(TivaSendingFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char far *)&mreq,sizeof(mreq)) < 0) { printf("TivaNet Memship Error!"); printf("\n"); } // set TTL (Time To Live) unsigned int ttl = 38; // restricted to 38 hops if (setsockopt(TivaSendingFd, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&ttl, sizeof(ttl) ) < 0) { printf("TimeTo Live Error!"); printf("\n"); } connect(&TivaUdpSocket, SIGNAL(readyRead()), this, SLOT(tiva_readTivaNetPacket())); }}void UDPMReceiver::tiva_readTivaNetPacket(){ printf("in tiva_readTivaNetPacket()"); printf("\n"); QString OInMessage; QHostAddress PeerIp; PeerIp.setAddress(PeerIp.Any); quint16 por=TivaPort; while (TivaUdpSocket.hasPendingDatagrams()) { QByteArray datagram; datagram.resize(TivaUdpSocket.pendingDatagramSize( )); TivaUdpSocket.readDatagram(datagram.data(), datagram.size(),&PeerIp,&por); OInMessage=datagram.data(); printf(OInMessage.toStdString().c_str()); printf("\n"); }}
C++ (Qt)#include <ws2tcpip.h>#include <winsock2.h>
C++ (Qt) udpSocket.bind(QHostAddress("0.0.0.0"),4321,QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); struct ip_mreq mcast; int opt = 1; // 0, если на собственный адрес отправлять не нужно int ttl = 1; int sd = udpSocket.socketDescriptor(); memset(&mcast, 0, sizeof(struct ip_mreq)); mcast.imr_multiaddr.s_addr = inet_addr("225.0.0.0"); mcast.imr_interface.s_addr = inet_addr("0.0.0.0"); setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (const char *)&opt, sizeof(int)); setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&ttl, sizeof(ttl)); setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mcast.imr_interface.s_addr, sizeof(mcast.imr_interface.s_addr)); setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mcast, sizeof(mcast)); connect(&udpSocket,SIGNAL(readyRead()),this,SLOT(readUdp()));
C++ (Qt) udpSocket.writeDatagram(msg->text().toAscii(),QHostAddress("225.0.0.0"),4321);
C++ (Qt)void MainWindow::readUdp(){ quint64 size=udpSocket.pendingDatagramSize(); QByteArray buffer; buffer.resize(size); udpSocket.readDatagram(buffer.data(),size); QListWidgetItem* lwi = new QListWidgetItem(buffer); listWidget->addItem(lwi);}
QT += coreQT -= guiTARGET = UDPReceiverCONFIG += consoleCONFIG -= app_bundleTEMPLATE = appLIBS += "C:\QtSDK\mingw\lib\libws2_32.a"LIBS += "C:\QtSDK\Desktop\Qt\4.7.2\mingw\lib\libQtGui4.a"LIBS += "C:\QtSDK\Desktop\Qt\4.7.2\mingw\lib\libQtNetwork4.a"SOURCES += main.cpp \ cmessage.cpp \ udpmulticastsocket.cpp \ io.cpp \ chat.cppHEADERS += \ cmessage.h \ udpmulticastsocket.h \ io.h \ chat.h