什么是BeginReceive(Byte(),Int32,Int32,SocketFlags,AsyncCallback,Object)和socket中的回调方法?

时间:2014-03-18 08:26:22

标签: c# vb.net sockets

我是套接字编程的新手。所以,最近我被分配到使用vb.net进行套接字编程的任务,以找出为什么这个程序分配了近20GB的物理内存。但由于我对Socket没有适当的知识,所以我完全被困在里面。我发现BeginReceive()方法和EndReceive()方法作为回调在程序中,当我搜索这些时,我发现了一些EndReceive()应该调用回调的信息。所以,我的问题是:

  1. 每次EndReceive()之后是否需要调用BeginReceive()方法来清除缓冲区以便内存分配变得清晰?
  2. 如果有人用简单的词语解释BeginReceive()EndReceive()的真正含义,我真的很感激。
  3. 更新:请检查我在项目中找到的以下代码:

    Public Sub Receive(ByVal client As Socket)
    
          Dim state As New StateObject
        state.workSocket = client
    
    
        Try
            If m_clientSocket.Connected Then
    
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
            Else
                Connect()
            End If
    
        Catch se As SocketException
            logstr = se.Message
            write_log(logstr, "Receive", "Receive")
        End Try
    End Sub
    
    
     Public Sub ReceiveCallback(ByVal ar As IAsyncResult)
    
    
        Dim state As StateObject = CType(ar.AsyncState, StateObject)
        Dim client As Socket = state.workSocket
        Dim receiveData As String = ""
        Dim sendData As String = ""
    
        Dim bytesRead As Integer = client.EndReceive(ar)
    
        If bytesRead > 0 Then
    
            state.sb.Append(System.Text.Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
    
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)      
    
            receiveData = state.sb.ToString()
    
            Me.lsBulk.Items.Insert(0, receiveData)
            sendData = getReply(receiveData)
            write_log(Now.ToString & " : " & receiveData & vbCrLf, "bytesRead", "bytesRead")
    
            Try
                Dim objData As [Object] = sendData
                Dim byData As Byte() = System.Text.Encoding.ASCII.GetBytes(objData.ToString())
                If m_clientSocket IsNot Nothing Then
                    m_clientSocket.Send(byData)
                End If
    
                write_log(Now.ToString & " : " & sendData & vbCrLf, "sendData", "sendData")
            Catch se As SocketException
                logstr = se.Message
                write_log(logstr, "WaitForData", "waitfordata")
            End Try
    
        Else
    
            receiveData = state.sb.ToString()
    
    
            If receiveData <> "" Then
                write_log(Now.ToString & " : " & receiveData & vbCrLf, "receiveData2", "receiveData2")
            End If
    
    
        End If
    End Sub 
    

1 个答案:

答案 0 :(得分:1)

根据文档BeginReceive&#34;开始异步接收来自已连接的Socket的数据。&#34;。因此,在正常语言中:在继续执行程序的同时开始接收数据。

EndReceive&#34;的文档中,回调方法必须接受BeginConnect方法返回的IAsyncResult作为参数。&#34;和&#34;获取Socket后,您可以调用EndConnect方法成功完成连接尝试。&#34;

并且:&#34;必须通过调用EndReceive方法来完成异步BeginReceive操作。通常,该方法由回调委托调用。&#34;

是的,您必须致电EndReceive以获取BeginReceive的结果。

另外,作为对第二个问题的回答:&#34;要获取接收的数据,请调用AsyncState的{​​{1}}方法,并提取生成的状态对象中包含的缓冲区。& #34;因此结果缓存在IAsyncResult对象中。从那里提取它并摆脱对象让GC收集它。

来源:BeginReceiveEndReceiveIAsyncResult