EndReceive永远不会完成接收vb Net

时间:2013-08-27 15:19:06

标签: .net vb.net sockets beginreceive

我正在使用example of Microsoft(Visual Basic net 4.5)发送和接收数据抛出套接字,但此块始终为true:

    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 > 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 > 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

但我发送,发送和发送短信息或大信息,并且它永远不会进入其他句子。在这里,我的初始代码:

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

我确实收到了服务器发送的消息,我可以在Visual Studio步骤中看到它们,但我不知道为什么它永远不会进入其他地方,我的意思是,它永远不会完成接收数据。

我读过the answer of Marc Gravell但我更喜欢如何解决这个问题的代码示例,我不知道该怎么做。

此外,我删除了“else”,它将我的textBox填充到许多白线,就像许多接收的infite循环一样。请帮我。感谢。

哦对不起,这是Escuchador功能:

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

1 个答案:

答案 0 :(得分:0)

正如你已经链接到Marc Gravell的答案,他在重要的一句中的第一句话:

“如果流已关闭,则EndReceive可能会获得0,并且所有数据都已消耗完。”

只要流打开,您就永远不会从EndReceive获得0。您必须根据协议处理您获得的数据,以查找邮件的结尾并发送回复。

正如您已经要求提供一些代码,这里是您的代码,其中包含注释,其中包含其他部分:

If bytesRead > 0 Then
    ' There might be more data, so store the data received so far.
    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))

    ' Put here code to check for protocol end (for example \0) in your buffer state.sb
    ' and handle protocol if found. The rest of the buffer should remain in the buffer
    ' as it could be part of the next protocol.

    ' if you only except one message, set receiveDone.Set() here if message received
    ' completely

    '  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
    'connection was closed
    ' handle existing information if appropreate
    receiveDone.Set()
End If