什么是套接字中的 BeginReceive(Byte(), Int32, Int32, SocketFlags, AsyncCallback, Object) 和回调方法? [英] What is BeginReceive(Byte(), Int32, Int32, SocketFlags, AsyncCallback, Object) and callback method in socket?

查看:29
本文介绍了什么是套接字中的 BeginReceive(Byte(), Int32, Int32, SocketFlags, AsyncCallback, Object) 和回调方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am so new in socket programming. So, recently I am assigned to a task in socket programming using vb.net to find out why this program is allocating almost 20GB of the physical memory. But as I don't have proper knowledge in Socket so I am quite stuck inside. I have found that BeginReceive() method and EndReceive() method as callback is in the program and when I googled for these I found some information that EndReceive() should be called for callback. So, my question is:

  1. 是否需要每次在 BeginReceive() 之后调用 EndReceive() 方法来清除缓冲区,以便清除内存分配?
  2. 如果有人用简单的语言解释BeginReceive()EndReceive() 的真正含义,我将不胜感激?
  1. Do I need to call the EndReceive() method after BeginReceive() every time to clear the buffer so that the memory allocation becomes clear?
  2. I would really appreciate if someone explain in simple words what does really mean by BeginReceive() and EndReceive()?

更新:请检查我在项目中找到的以下代码:

Update: Please check the below code I found in the project:

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 

推荐答案

根据文档 BeginReceive 开始从连接的 Socket 异步接收数据.".所以在正常语言中:在继续执行程序的同时开始接收数据.

According to the documentation BeginReceive "begins to asynchronously receive data from a connected Socket.". So in normal language: start receiving data while continuing execution of the program.

EndReceive 的文档中,回调方法必须接受由 BeginConnect 方法返回的 IAsyncResult 作为参数."以及获得Socket后,可以调用EndConnect方法成功完成连接尝试."

In the documentation from EndReceive "the callback method must accept the IAsyncResult returned by the BeginConnect method as a parameter." and "After obtaining the Socket, you can call the EndConnect method to successfully complete the connection attempt."

还有:异步BeginReceive操作必须通过调用EndReceive方法完成.通常,该方法由回调委托调用."

And also: "The asynchronous BeginReceive operation must be completed by calling the EndReceive method. Typically, the method is invoked by the callback delegate."

所以是的,您必须调用 EndReceive 来获取 BeginReceive 的结果.

So yes, you have to call EndReceive to get the results of the BeginReceive.

另外,作为第二个问题的答案:要获取接收到的数据,请调用 IAsyncResultAsyncState 方法,并提取包含在结果状态中的缓冲区目的."所以结果缓存在 IAsyncResult 对象中.从那里提取它并摆脱对象让GC收集它.

Also, as answer to your second question: "To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object." So the result is buffered in the IAsyncResult object. Extract it from there and get rid of the object to let GC collect it.

来源:开始接收结束接收IAsyncResult.

这篇关于什么是套接字中的 BeginReceive(Byte(), Int32, Int32, SocketFlags, AsyncCallback, Object) 和回调方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆