locked
Send and Receive data( byte) using DatagramSocket

    Question

  • Hi,

    Please help me with a sample code in c++, which demonstrates sending and receiving of data(bytes not string) using DatagramSocket in winRT.

    Thanks


    • Edited by AKEV Friday, October 19, 2012 6:31 AM
    Friday, October 19, 2012 6:29 AM

Answers

  • Here is the send function, using the OutputStream property.

    int DatagramSocket::Send(String^ dataBuffer, IOutputStream^ outputStream)
    {
        IBuffer^ inputBuffer = CryptographicBuffer::ConvertStringToBinary(dataBuffer, BinaryStringEncoding::Utf8);
    
        size_t byteSent = 0;
    
        create_task( outputStream->WriteAsync(inputBuffer)).then( [&] (task<unsigned int> previousTask)
        {
            try
            {
                byteSent = previousTask.get();
            }
            catch (Exception^ exception)
            {
                // exception->Message;
            }
        }).get();
    
    return byteSent;
    }

    Try to implement receive function. See followings...

    Windows.Storage.Streams namespace

    DatagramSocket.ConnectAsync

    DatagramSocket sample

    • Marked as answer by AKEV Monday, October 22, 2012 11:56 AM
    Friday, October 19, 2012 9:30 AM