Question

At the client I use additional thread which executes this method:

public void FillBuf(object sender)
{
var handler = (Socket)sender;

while (true)
{
    received = 0;

    while (received < 512)
    {
    int block = handler.Receive(bytes, received, bytes.Length - received, SocketFlags.None);
    received += block;
    }

    que.Enqueue(bytes);

}
}

At the server I also have additional thread in in which I send data like this:

while(true)
{
for (int i=0; i < handler.Length; i++)
{
if (handler[i] != null)
{

    bytes = BitConverter.GetBytes(move);//move
    Array.Resize<byte>(ref bytes, 512);
    handler[i].Send(bytes);

    bytes = Transmition.SerializationCardArr(game.player[i].GetPlayerPack);//playerCards
    Array.Resize<byte>(ref bytes, 512);
    handler[i].Send(bytes);

    bytes = Transmition.SerializationCardArr(table.GetAttackCards);//attackCards
    Array.Resize<byte>(ref bytes, 512);
    handler[i].Send(bytes);

    bytes = Transmition.SerializationCardArr(table.GetDefendCards);//defendCards
    Array.Resize<byte>(ref bytes, 512);
    handler[i].Send(bytes);

}
}
}

When number of elements in queue reaches some particular amount, I extract data form the buffer.

Problem is that all elements in a buffer are the same. Thank you.

Was it helpful?

Solution

In your client code, you're reusing the same byte array over and over, then enqueuing a reference to it. You need to create a new byte array for each buffer:

public void FillBuf(object sender)
{
    var handler = (Socket)sender;

    while (true)
    {
        // Note: use local variables here. You really don't want these variables
        // getting mixed up between threads etc.
        int received = 0;
        byte[] bytes = new byte[512];

        while (received < bytes.Length)
        {
            int block = handler.Receive(bytes, received, bytes.Length - received,
                                        SocketFlags.None);
            received += block;
        }
        que.Enqueue(bytes);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top