gpt4 book ai didi

vb.net - 套接字-为什么beginRecieve会阻止?

转载 作者:行者123 更新时间:2023-12-03 12:01:34 24 4
gpt4 key购买 nike

好的,所以我正在将MSDN示例用于异步客户端套接字。我为客户端使用的代码如下。我现在正在通过连接到安装在计算机上的telnet服务器以及连接到两个cisco交换机上的telnet端口进行测试。连接到任何这些telnet服务器的结果都是相同的。 receiveCallback例程将执行...如果bytesRead> 0变为true,则将导致再次调用client.BeginReceive。此时,程序将挂起30秒或更长时间(我假设它挂起,直到telnet服务器关闭连接为止)。挂起时,Windows窗体没有响应(甚至无法在屏幕上移动)。最终,再次调用ReceiveCallback例程,这一次bytesRead> 0为false,程序将解开。

我知道我没有内置处理消息等的逻辑,但是为什么beginReceive会导致ui变得无响应。

代码:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Imports System

Imports System.Threading

Public Class Form2


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AsynchronousClient.Main()
End Sub

End Class





' State object for receiving data from remote device.

Public Class StateObject
' Client socket.
Public workSocket As Socket = Nothing
' Size of receive buffer.
Public Const BufferSize As Integer = 256
' Receive buffer.
Public buffer(BufferSize) As Byte
' Received data string.
Public sb As New StringBuilder
End Class 'StateObject


Public Class AsynchronousClient
' The port number for the remote device.
Private Const port As Integer = 23

' ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)

' The response from the remote device.
Private Shared response As String = String.Empty


Public Shared Sub Main()
'http://msdn.microsoft.com/en-us/library/bew39x2a.aspx
'http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx



' Establish the remote endpoint for the socket.
' For this example use local machine.
' Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipHostInfo As IPHostEntry = Dns.Resolve("127.0.0.1")

Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, port)

' Create a TCP/IP socket.
Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

' Connect to the remote endpoint.
client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client)

' Wait for connect.
connectDone.WaitOne()

' Send test data to the remote device.
Send(client, "This is a test<EOF>")
' Send(client, "GET")
sendDone.WaitOne()

' Receive the response from the remote device.
Receive(client)
receiveDone.WaitOne()
Debug.Print("got here")
' Write the response to the console.
Debug.Print("Response received : " & response)

' Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
End Sub 'Main


Private Shared Sub ConnectCallback(ByVal ar As IAsyncResult)
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)

' Complete the connection.
client.EndConnect(ar)

Debug.Print("Socket connected to " & client.RemoteEndPoint.ToString())

' Signal that the connection has been made.
connectDone.Set()
End Sub 'ConnectCallback


Private Shared Sub Receive(ByVal client As Socket)

' Create the state object.
Dim state As New StateObject
state.workSocket = client

' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
End Sub 'Receive


Private Shared Sub ReceiveCallback(ByVal ar As IAsyncResult)

' Retrieve the state object and the client socket
' from the asynchronous state object.
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))

'Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Else
' All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToString()
End If
' Signal that all bytes have been received.
receiveDone.Set()
End If
End Sub 'ReceiveCallback


Private Shared Sub Send(ByVal client As Socket, ByVal data As String)
' Convert the string data to byte data using ASCII encoding.
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)

' Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
End Sub 'Send


Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)

' Complete sending the data to the remote device.
Dim bytesSent As Integer = client.EndSend(ar)
Debug.Print("Sent " & bytesSent & " bytes to server.")

' Signal that all bytes have been sent.
sendDone.Set()
End Sub 'SendCallback
End Class 'AsynchronousClient

最佳答案

所有对 ManualResetEvent.WaitOne() 的调用都将阻塞,并且您正在UI线程上(从单击处理程序中)调用此函数,因此UI被阻塞。

没有必要执行异步操作并立即在同一线程上等待阻塞-您刚刚重新实现了同步API(在Windows中所有IO都是异步的情况下-同步API只是启动异步的包装器操作,然后在其完成时阻止)。

您需要重新编写代码:

  • 在回调中完成操作(EndXYZ方法以匹配BeginXYZ并获取失败时的结果或异常)并开始下一个异步操作。
  • 在最后一个回调中(在您的情况下,是传递给 BeginReceive 的回调),请使用 Control.BeginInvoke 在与控件关联的UI线程上运行回调。
  • 删除所有等待。

  • 除了开始一系列操作和结束操作之外的所有操作都将在线程池中进行,而不是阻塞UI。 (当然,UI的状态可能需要反射(reflect)此处理,例如,在获取结果时锁定字段)。

    这种编程模型就像挂接UI事件序列一样,它不是关于具有线性调用序列的单个方法(至少要等到异步CTP成为VB和C#的 future 版本的一部分时)。

    关于vb.net - 套接字-为什么beginRecieve会阻止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4843482/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com