Question

Please forgive me for a silly question but I'm really can't find a solution. This is this way I'm trying to do it:

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

    while (true)
    {
        received = 0;

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

        que.Enqueue(bytes);
    }

At the beginning of the program I start thread which executes FillBuf method. The problem is that I never receive 1024 bytes for some reason. Thank you in advance.

Was it helpful?

Solution

It appears that the server isn't always sending 1024 bytes. This means that the client code which is expecting them will hang until it gets the correct amount of data. You should make sure that the server is sending the expected number of bytes or implement some mechanism in your protocol that would allow the sender to indicate at the beginning of the payload the number of bytes it is going to send. This way the client will know how many bytes to read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top