title: EndReceive Never never finishes receiving vb Net
body: <p>i'm using the <a href="http://msdn.microsoft.com/en-us/library/bew39x2a%28v=VS.85%29.aspx" rel="nofollow noreferrer">example of Microsoft</a> (Visual Basic net 4.5) to send and receive datas throw socket, but this block is always true:</p>
  
  <pre><code>    Private Sub OnRecieve(ByVal ar As IAsyncResult)
      Try
          Dim state As StateObject = CType(ar.AsyncState, StateObject)
          Dim client As Socket = state.workSocket
  
          ' Read data from the remote device.
          Dim bytesRead As Integer = client.EndReceive(ar)
  
          If bytesRead &gt; 0 Then
              ' There might be more data, so store the data received so far.
              state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
  
              '  Se supone que vuelve por los datos que faltan, pero no lo hace (Creo)
              client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf OnRecieve, state)
  
          Else
              ' All the data has arrived; put it in response.
              If state.sb.Length &gt; 1 Then
                  VariablesGlobales.response = state.sb.ToString()
              End If
              ' Signal that all bytes have been received.
              receiveDone.Set()
          End If
  
  
      Catch ex As Exception
          'clientSocket.Close()
          RaiseEvent FallaAlRecibirDatos(ex.Message, "Falla en endReive.")
      End Try
  
  End Sub
  </code></pre>
  
  <p>But i send, and send, and send message, short or large, and it never enter to the else sentence. Here, my initial code:</p>
  
  <pre><code>Public Sub Conectar()
  
      clientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
  
      Dim ipEndPoint As IPEndPoint = New IPEndPoint(Me.ipAddress, VariablesGlobales.Puerto)
      clientSocket.BeginConnect(ipEndPoint, New AsyncCallback(AddressOf OnConnect), clientSocket)
  
      ' Wait for connect.
      connectDone.WaitOne()
  
      EnviarDatosPersonales()
  
      ' Wait for send datas.
      sendDone.WaitOne()
  
      While True
          AvtivarEscuchador()
          receiveDone.WaitOne()
  
          DescifrarMsg(VariablesGlobales.response)
      End While
  End Sub
  </code></pre>
  
  <p>I do recive the messages that the server sends, and i can see them in Visual Studio steps to steps, but i don`t know why it never enters to the else, i mean, it never finish receiving datas.</p>
  
  <p>I read <a href="https://stackoverflow.com/questions/4583750/endreceive-never-returns-a-0-how-do-you-know-when-packets-are-finished-in-tcp-i">the answer of Marc Gravell</a> but i prefer a code example of how to solve this, i didn't know what to do.</p>
  
  <p>Moreover, i removed "else" and it fill my textBox with to many white lines, like a infite loop of many receives. Please help me. Thanks.</p>
  
  <p>Oh Sorry, here is the Escuchador Function:</p>
  
  <pre><code>Private Sub AvtivarEscuchador()
  
      ' Borramos los datos de respuesta anterior
      VariablesGlobales.response = ""
  
      ' Activamos el escuchador
      Try
          ' Create the state object.
          Dim state As New StateObject()
          state.workSocket = Me.clientSocket
  
          ' Begin receiving the data from the remote device.
          Me.clientSocket.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf OnRecieve, state)
      Catch e As Exception
          RaiseEvent FallaAlRecibirDatos("No se pudo activar el escuchador.", "Falla al intentar escuchar.")
      End Try
  
  End Sub
  </code></pre>