C # Silverlight Equivlant Windows Form Method?

Which is equal to this:

while (Offset < packet.Data.Length)
{
    Offset += m_Socket.Receive(packet.Data, Offset, packet.Data.Length - Offset, SocketFlags.None);
}

In Siliverlight? This is a Windows form and does not work with Silverlight: / Any help would be appreciated.

thank

What the function does, on the “completed” sub, I catch 4 bytes, which is the length of the header from my server, after I catch these 4 bytes, I want to go to the endReceive method, which receives the remaining packets length.

How can I do this in Silverlight?

+3
source share
1 answer

Silverlight does not have synchronous socket methods. You will need to use the Socket.ReceiveAsync Method .

A good example is here: Pushing data in Silverlight Client using sockets .

[Edit] The basic idea is to do something like this:

var e = new SocketAsyncEventArgs();
e.Completed += SocketReceiveCompleted;
Socket.ReceiveAsync(e);

private void SocketReceiveCompleted(object sender, SocketAsyncEventArgs e)
{
    Offset += e.BytesTransferred;
    if (Offset > packet.Data.Length)
    {
        Socket.Close(); // or do whatever you need to do after your while loop
        return;
    }
    Array.Copy(e.Buffer, 0, packet.Data, Offset, e.BytesTransferred);
}
+4
source

All Articles