开发者

C# Silverlight Equivlant of Windows Form Method?

开发者 https://www.devze.com 2023-03-10 13:44 出处:网络
What is the equivlant of this: while (Offset < packet.Data.Length) { Offset += m_Socket.Receive(packet.Data, Offset, packet.Data.Length 开发者_如何学Go- Offset, SocketFlags.None);

What is the equivlant of this:

while (Offset < packet.Data.Length)
{
    Offset += m_Socket.Receive(packet.Data, Offset, packet.Data.Length 开发者_如何学Go- Offset, SocketFlags.None);
}

In Siliverlight? That is Windows Form and does not work with Silverlight :/ Any helped would be appreciated.

Thanks

What the function does is, on the "completed" sub, I catch 4 bytes which is a header length from my server, after I caught those 4 bytes I want to go into the endReceive method which gets the rest of the packets length.

How would I do this in Silverlight?


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

Good example here: Pushing Data to a Silverlight Client with Sockets.

[Edit] A basic idea 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消