codes not execute when Socket.Receive return zero

I have a Function to handle requests and another to forward data.

In ForwardToBrowser function when receivedBytes in hostSocket.Receive return zero the codes stopped working and my codes after it not executed. BUT The program resumes.

For example, when receivedBytes return zero the "MessageBox1" not shown and the program still working. Without any error and without any Exception.

I put my codes such as MessageBox after while loop (you can see it as "MessageBox2") and it doesn't execute.

I'm using this ForwardToBrowser function in Handle function in the same class, Unfortunately the codes after it such as Shutdown and closing socket and Tools.Save and "MessageBox3" when receivedBytes become zero can't execute.

private void ForwardToBrowser(Socket hostSocket)
        {
            byte[] buffer = new byte[10240];
            int receivedBytes = 0;
            receivedBytes = hostSocket.Receive(buffer);
            while (receivedBytes > 0)
            {
                try
                {
                    _browserSocket.Send(buffer, receivedBytes, SocketFlags.None);
                    receivedBytes = hostSocket.Receive(buffer, SocketFlags.None);

                    System.Windows.Forms.MessageBox.Show("MessageBox1");

                    if (receivedBytes <= 0) return;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            System.Windows.Forms.MessageBox.Show("MessageBox2");
        }

Using in Handle function:

public void Handle()
{
    // host, port, header Defined before
    Socket hostSocket = SendToRealServer(host, port, header);
    if (hostSocket == null) return;

    // Forward all bytes directly from host to browser
    ForwardToBrowser(hostSocket);

    //if `receivedBytes` in `ForwardToBrowser()` return zero the codes after this line not executed.
    System.Windows.Forms.MessageBox.Show("MessageBox3");
    hostSocket.Shutdown(SocketShutdown.Both);
    hostSocket.Close();
    
    Tools.SaveCache(_db, host, hostHeader, path);
}

Please help me to handle this behavior of Socket.Receive and change my code to resume even receivedBytes is zero. Thanks.



from Recent Questions - Stack Overflow https://ift.tt/3xZZKW4
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation