コード例 #1
0
void MulticastClientUDP()
{
    UdpClient client;
    IP4Address::List hosts;
    client.GetHostAddresses(hosts);
    cout << "IP Address for Host Machine\n";
    for(int i = 0; i < (int)hosts.size(); i++ ) 
    {
        cout << hosts[i].mString << endl;
    }

    Packet data;
    data.SetByteOrder(CX_PACKET_BIG_ENDIAN);
    if(!client.InitializeMulticastSocket(MULTICAST_GROUP, PORT, 1)) 
    {
        cout << "Unable to connect to host " << HOSTNAME << endl;
        return;
    }
    cout << "Initialized UDP Client Socket on port " << PORT << endl;
    char key = 0;
    while(key != 27) 
    {
        if((key = GetChar()) > 0) 
        {
            data.Write(key);
            cout << key;
            client.Send(data);  //  Send data (could send character array also if desired
            data.Clear();       //  Clear packet and encode data.
            /*  Alternative way to send:
                client.Send(&key, 1);    //  Send buffer/array.
            */
        }
    }
}