void MulticastSocketTest::testReceiveWhileClosed()
{
	syscommon::InetSocketAddress networkIface( INADDR_ANY, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.3"), 3033 );

	// Create the socket, and immediately close it
	syscommon::MulticastSocket receiver( networkIface );
	receiver.close();

	// Construct the send buffer and packet
	char receiveBuffer[1024];
	syscommon::DatagramPacket receivePacket( receiveBuffer,
											 1024 );

	// Attempting to receive should fail
	try
	{
		receiver.receive( receivePacket );
		failTestMissingException( "SocketException", "receiving on a closed socket" );
	}
	catch( syscommon::SocketException& )
	{
		// SUCCESS!
	}
	catch( std::exception& e )
	{
		failTestWrongException( "SocketException", e, "receiving on a closed socket" );
	}
}
void MulticastSocketTest::testSendReceive()
{
	syscommon::InetSocketAddress networkIface( INADDR_ANY, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.3"), 3033 );

	syscommon::MulticastSocket sender( networkIface );
	syscommon::MulticastSocket receiver( networkIface );
	try
	{
		// Join the multicast group
		sender.joinGroup( multicastAddress.getAddress() );
		receiver.joinGroup( multicastAddress.getAddress() );

		// Construct the send buffer and packet
		char sendBuffer[1024];
		const char* sendString = "Hello World";
		size_t stringLength = ::strlen( sendString );
		size_t sendLength = sizeof(size_t) + stringLength;
		::memcpy( sendBuffer, &stringLength, sizeof(size_t) );
		::memcpy( sendBuffer + sizeof(size_t), sendString, stringLength );

		syscommon::DatagramPacket sendPacket( sendBuffer,
											  0,
											  sendLength,
											  multicastAddress );

		// Construct the receive buffer and packet
		char receiveBuffer[1024];
		::memset( receiveBuffer, 0, sizeof(receiveBuffer) );

		syscommon::DatagramPacket receivePacket( receiveBuffer, sizeof(receiveBuffer) );

		// Send and Receive
		sender.send( sendPacket );
		receiver.receive( receivePacket );

		// Is the received data the same as what was sent?
		CPPUNIT_ASSERT( (size_t)receivePacket.getLength() == sendLength );
		CPPUNIT_ASSERT( ::memcmp(sendBuffer, receiveBuffer, sendLength) == 0 );

		sender.leaveGroup( multicastAddress.getAddress() );
		receiver.leaveGroup( multicastAddress.getAddress() );
	}
	catch( std::exception& e )
	{
		failTest( "Unexpected exception while sending a datagram. Reported error %s\n",
				  e.what() );
	}

	sender.close();
	receiver.close();
}
Esempio n. 3
0
File: main.cpp Progetto: xtuer/Qt
int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QHostAddress multicastAddress("225.20.40.20"); // Multicast 组的地址
    QUdpSocket *udpSocket = new QUdpSocket();
    int messageCount = 10;

    for (int i = 0; i < messageCount; ++i) {
        QString data = generateMessage(i, '*');
        // 使用 Unicast 发送消息
        udpSocket->writeDatagram(data.toUtf8(), multicastAddress, 13930);
        qDebug() << data;
    }

    return a.exec();
}
void MulticastSocketTest::testJoinLeaveGroup()
{
	syscommon::InetSocketAddress networkIface( INADDR_LOOPBACK, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.90"), 3033 );

	syscommon::MulticastSocket mcastSocket( networkIface );

	try
	{
		mcastSocket.joinGroup( multicastAddress.getAddress() );
		mcastSocket.leaveGroup( multicastAddress.getAddress() );
	}
	catch( std::exception& e )
	{
		failTest( "Received exception while joining a multicast group. Error: %s",
				  e.what() );
	}

	mcastSocket.close();
}
void MulticastSocketTest::testJoinOnClosedSocket()
{
	syscommon::InetSocketAddress networkIface( INADDR_LOOPBACK, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.90"), 3033 );

	syscommon::MulticastSocket mcastSocket( networkIface );
	mcastSocket.close();

	try
	{
		mcastSocket.joinGroup( multicastAddress.getAddress() );
		failTestMissingException( "SocketException", "joining on a closed socket" );
	}
	catch( syscommon::SocketException& )
	{
		// SUCCESS!
	}
	catch( std::exception& e )
	{
		failTestWrongException( "SocketException", e, "joining on a closed socket" );
	}
}
void MulticastSocketTest::testLeaveGroupWithoutJoin()
{
	syscommon::InetSocketAddress networkIface( INADDR_LOOPBACK, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.90"), 3033 );

	syscommon::MulticastSocket mcastSocket( networkIface );

	try
	{
		mcastSocket.leaveGroup( multicastAddress.getAddress() );
		failTestMissingException( "SocketException", "leaving a group without joining first" );
	}
	catch( syscommon::SocketException& )
	{
		// SUCCESS!
	}
	catch( std::exception& e )
	{
		failTestWrongException( "SocketException", e, "leaving a group without joining first" );
	}

	mcastSocket.close();
}
void MulticastSocketTest::testSendNoAddress()
{
	syscommon::InetSocketAddress networkIface( INADDR_ANY, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.3"), 3033 );

	// Create the sender socket
	syscommon::MulticastSocket sender( networkIface );
	sender.joinGroup( multicastAddress.getAddress() );

	// Construct the send buffer
	char sendBuffer[1024];
	const char* sendString = "Hello World";
	size_t stringLength = ::strlen( sendString );
	size_t sendLength = sizeof(size_t) + stringLength;
	::memcpy( sendBuffer, &stringLength, sizeof(size_t) );
	::memcpy( sendBuffer + sizeof(size_t), sendString, stringLength );

	// Create a packet with no address
	syscommon::DatagramPacket sendPacket( sendBuffer, sendLength );

	// Attempting to send should fail
	try
	{
		sender.send( sendPacket );
		failTestMissingException( "SocketException", "sending without a destination address" );
	}
	catch( syscommon::SocketException& )
	{
		// SUCCESS!
	}
	catch( std::exception& e )
	{
		failTestWrongException( "SocketException", e, "sending without a destination address" );
	}

	sender.close();
}
void MulticastSocketTest::testSendWhileClosed()
{
	syscommon::InetSocketAddress networkIface( INADDR_ANY, 3033 );
	syscommon::InetSocketAddress multicastAddress( TEXT("226.0.1.3"), 3033 );

	// Create the socket, and immediately close it
	syscommon::MulticastSocket sender( networkIface );
	sender.close();

	// Construct the send buffer and packet
	char sendBuffer[1024];
	const char* sendString = "Hello World";
	size_t stringLength = ::strlen( sendString );
	size_t sendLength = sizeof(size_t) + stringLength;
	::memcpy( sendBuffer, &stringLength, sizeof(size_t) );
	::memcpy( sendBuffer + sizeof(size_t), sendString, stringLength );

	syscommon::DatagramPacket sendPacket( sendBuffer,
										  0,
										  sendLength,
										  multicastAddress );

	// Attempting to send should fail
	try
	{
		sender.send( sendPacket );
		failTestMissingException( "SocketException", "sending on a closed socket" );
	}
	catch( syscommon::SocketException& )
	{
		// SUCCESS!
	}
	catch( std::exception& e )
	{
		failTestWrongException( "SocketException", e, "sending on a closed socket" );
	}
}