73 lines
1.7 KiB
C++
73 lines
1.7 KiB
C++
#include <common/windows.hpp>
|
|
#include <socket/socket.hpp>
|
|
#include <socket/sockaddr.hpp>
|
|
#include <socket/ipmcast.hpp>
|
|
#include <socket/wsadata.hpp>
|
|
#include <iostream.h>
|
|
|
|
#define DEST_PORT 4567
|
|
#define SOURCE_PORT 0
|
|
#define TEST_ADDR "234.5.6.7"
|
|
|
|
void outDebug(const String &string);
|
|
|
|
void main(int argc,char **argv)
|
|
{
|
|
WSASystem wsaSystem;
|
|
SocketAddress socketAddressLocal;
|
|
SocketAddress socketAddressTo;
|
|
SocketAddress socketAddressFrom;
|
|
IPMReq ipMCast;
|
|
Socket socket;
|
|
|
|
if(!socket.create(Socket::DomainInternet,Socket::SocketDGram,Socket::ProtocolNone))
|
|
{
|
|
outDebug("[Socket creation failed]");
|
|
return;
|
|
}
|
|
if(!socket.reuseAddress(true))
|
|
{
|
|
outDebug("[Error setting option REUSEADDR.]");
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
outDebug("Socket created.");
|
|
socketAddressLocal.family(AF_INET);
|
|
socketAddressLocal.internetAddress(InternetAddress());
|
|
socketAddressLocal.port(htons(DEST_PORT));
|
|
if(!socket.bind(socketAddressLocal))
|
|
{
|
|
outDebug("Bind failed.");
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
outDebug("Socket is bound.");
|
|
|
|
if(!socket.setMulticastTTL(2))
|
|
{
|
|
outDebug("[Error setting option SET_MULTICAST_TTL.]");
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
|
|
ipMCast.multicastAddress(InternetAddress(TEST_ADDR));
|
|
ipMCast.localAddress(InternetAddress());
|
|
if(!socket.addMembership(ipMCast))
|
|
{
|
|
int error=WSAGetLastError();
|
|
outDebug(String("[Error setting option ADDMEMBERSHIP.] Code=")+String().fromInt(error));
|
|
socket.destroy();
|
|
return;
|
|
}
|
|
socket.setMulticastLoopback(false);
|
|
|
|
socket.destroy();
|
|
}
|
|
|
|
void outDebug(const String &string)
|
|
{
|
|
String outString(string+String("\n"));
|
|
cout << ((String&)string).str() << endl;
|
|
::OutputDebugString(outString.str());
|
|
}
|