异常,但仅在第 3 次出现 - IAsyncResult 对象未从此 class 上的相应异步方法返回
Exception, but only on 3rd time - The IAsyncResult object was not returned from the corresponding asynchronous method on this class
我有一个旧版 (2008) Windows 服务应用程序(使用 System.ServiceProcess.ServiceBase),我需要以与现在的工作方式略有不同的方式来使用它。目前它启动并在特定端口上创建一个 TCPLISTENER,供客户端应用程序连接(它连接一次)以便 send/receive 请求到监听端口。这一切都很好。但是,适配需要使用网络应用程序连接到侦听器端口,然后 send/rcv 正常但接收后必须 DISCONNECT(*)
(* - 如果 web 应用程序不必在每次测试后断开与侦听套接字的连接,那会更好,但我无法实现这一目标。我可以在会话状态下保持 TCP 连接吗重复使用?)
无论如何,对于第一个 TWO connect/send/rcv/disconnect 测试,适配工作正常,但在 THIRD服务在套接字 class 的 EndAccept 中抛出 ArgumentException。
我不明白为什么第三次测试失败了。
这是简单的测试客户端。
Public Class ConnectDisconnectTCP
Inherits System.Web.UI.Page
Private IP As String = "127.0.0.1"
Private port As Int32 = 10002
Private mTCP As System.Net.Sockets.TcpClient
Private netStrm As NetworkStream
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oEnc As New ASCIIEncoding
Dim ScanText As String = "100000000093001X" + vbLf
Dim mTCP As System.Net.Sockets.TcpClient
'connect and create a tcpclient
mTCP = New System.Net.Sockets.TcpClient(IP, port)
netStrm = mTCP.GetStream()
netStrm.WriteAsync(oEnc.GetBytes(ScanText.ToString), 0, ScanText.ToString.Length)
If netStrm.CanRead Then
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As StringBuilder = New StringBuilder()
Dim numberOfBytesRead As Integer = 0
' Incoming message may be larger than the buffer size.
Do
numberOfBytesRead = netStrm.Read(myReadBuffer, 0, myReadBuffer.Length)
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
Loop While netStrm.DataAvailable
End If
'CLOSE
netStrm.Close()
mTCP.Close() 'Disposes this TcpClient instance and requests that the underlying TCP connection be closed.
mTCP = Nothing
Label1.Text = "Closed"
End Sub End Class
这是完成所有工作的服务上的 TCPListener class
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Public Class TCPListenerPort
Private currentAsynchResult As IAsyncResult
Private cState As ClientState
Private servSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Private m_connectionstate As Int32 = 0
Dim sData As String = String.Empty
'counters
Public cnt As Int32 = 0
Public rcvcnt As Int32 = 0
Public acccnt As Int32 = 0
Public sndcnt As Int32 = 0
Public sndcal As Int32 = 0
Public cncnt As Int32 = 0
'Public Events
Public Event ConnectionState(ByVal enmState As Int32)
Public Event DataArrived(ByVal strData As Object)
Public Sub New()
End Sub
Public Sub New(ByVal dr As DataRow)
End Sub
Protected Overrides Sub Finalize()
Try
If Not cState Is Nothing Then
cState.ClntSock.Close()
End If
If Not servSock Is Nothing Then
servSock.Close()
End If
Catch ex As Exception
Throw ex
Finally
MyBase.Finalize()
End Try
End Sub
Private Class ClientState
Public Const BUFSIZE As Integer = 1024
Public mrcvBuffer As Byte() = New Byte(BUFSIZE - 1) {}
Public mSendBuffer As Byte() = New Byte(BUFSIZE - 1) {}
Private mclntSock As Socket = Nothing
Public Sub New(ByRef clntSock As Socket)
mclntSock = clntSock
ReDim mrcvBuffer(BUFSIZE)
ReDim mSendBuffer(BUFSIZE)
End Sub
Public ReadOnly Property SckConnected() As Boolean
Get
SckConnected = mclntSock.Connected
End Get
End Property
Public ReadOnly Property RcvBuffer() As Byte()
Get
RcvBuffer = mrcvBuffer
End Get
End Property
Public ReadOnly Property SendBuffer() As Byte()
Get
SendBuffer = mSendBuffer
End Get
End Property
Public ReadOnly Property ClntSock() As Socket
Get
ClntSock = mclntSock
End Get
End Property
End Class
Public Sub Connect(ByVal TCPPort As Int32, ByVal Backlog As Int32)
Try
cState = New ClientState(servSock)
cState.ClntSock.Bind(New IPEndPoint(System.Net.IPAddress.Any, TCPPort))
cState.ClntSock.Listen(100) '5
While True
cncnt = cncnt + 1
System.Diagnostics.Debug.WriteLine("in connect WHILE " + cncnt.ToString)
currentAsynchResult = servSock.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), cState)
currentAsynchResult.AsyncWaitHandle.WaitOne()
End While
Catch __unusedObjectDisposedException1__ As ObjectDisposedException
Catch e As Exception
End Try
End Sub
Private Sub AcceptCallback(ByVal asyncResult As IAsyncResult)
Try
acccnt = acccnt + 1
System.Diagnostics.Debug.WriteLine("AcceptCallback Start" + acccnt.ToString)
'obtain the Socket on which the connection attempt is being made
Dim servSock As ClientState = asyncResult.AsyncState 'Asyncstate gets a user defined object that qualifies or contains info about an async operation
Dim clntSock As Socket
clntSock = servSock.ClntSock.EndAccept(asyncResult)
System.Diagnostics.Debug.WriteLine(clntSock.RemoteEndPoint)
Dim cs As New ClientState(clntSock)
clntSock.BeginReceive(cs.RcvBuffer, 0, cs.RcvBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallback), cs)
Catch ar As ArgumentException
'!!!!!Test 3 errors here with
'The IAsyncResult object was not returned from the corresponding asynchronous method on this class.
Catch ex As Exception
End Try
System.Diagnostics.Debug.WriteLine("AcceptCallback End " + acccnt.ToString)
End Sub
Private Sub ReceiveCallback(ByVal asyncResult As IAsyncResult)
rcvcnt = rcvcnt + 1
System.Diagnostics.Debug.WriteLine("ReceiveCallback In " + rcvcnt.ToString)
Dim cs As ClientState = asyncResult.AsyncState
Dim AE As New System.Text.ASCIIEncoding
Dim recvMsgSize As Int32 = 0
Dim strTmp As String = String.Empty
Dim strSend As String = String.Empty
Try
recvMsgSize = cs.ClntSock.EndReceive(asyncResult)
strTmp = Replace(AE.GetString(cs.RcvBuffer), vbNullChar, "")
If recvMsgSize > 0 Then
System.Diagnostics.Debug.WriteLine("ReceiveCallback receiveMsgSize " + recvMsgSize.ToString)
If Right(strTmp, 1) = vbLf Or Right(strTmp, 1) = vbCr Then
strSend = sData + strTmp
strSend = strSend.Replace(vbLf, "")
strSend = strSend.Replace(vbCr, "")
cState = cs
sData = String.Empty
RaiseEvent DataArrived(strSend)
Else
End If
Else
End If
Catch ex As Exception
Throw ex
End Try
System.Diagnostics.Debug.WriteLine("ReceiveCallback Exit " + rcvcnt.ToString)
End Sub
Public Function SendData(ByVal strData As Object) As Boolean
' Change the data to a byte array if necessary, then send it via the socket
Dim oEncoder As New System.Text.ASCIIEncoding
Dim bytes As Byte()
Dim Result As IAsyncResult
Try
Select Case strData.GetType().ToString
Case "System.String" ' Convert a string to a byte array
bytes = oEncoder.GetBytes(strData)
Case "System.Byte[]" ' Send a byte array directly
bytes = strData
Case Else ' And just send anything else as-is
bytes = strData
End Select
sndcnt = sndcnt + 1
Result = cState.ClntSock.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf SendCallback), cState)
System.Diagnostics.Debug.WriteLine("SendData " + sndcnt + " " + Result.ToString)
Result.AsyncWaitHandle.WaitOne()
Catch ex As Exception
Throw ex
End Try
Return True
End Function
Private Sub SendCallback(ByVal asyncResult As IAsyncResult)
Dim cs As ClientState = asyncResult.AsyncState
Dim bytesSent As Int32 = 0
Try
bytesSent = cs.ClntSock.EndSend(asyncResult)
sndcal = sndcal + 1
cs.ClntSock.BeginReceive(cs.RcvBuffer, 0, cs.RcvBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallback), cs)
System.Diagnostics.Debug.WriteLine("SendCallBack " + sndcal.ToString + " " + bytesSent.ToString)
Catch ex As Exception
Throw (ex)
End Try
End Sub
End Class
启用 .net 跟踪,加上代码的系统诊断 debug.write,这里是 TEST1、TEST2、TEST3/CRASH 操作的完整日志。请注意“<-- SCANTEXT RECEIVED”和“<-- RESPONSE RECEIVED”注释。 'in connect WHILE 4' 注释显示第 3 次请求尝试的到来。
</p>
<pre><code> [12572] TcpClient#57416410::TcpClient(AddressFamily#2)
[12572] Socket#61940669::Socket(AddressFamily#2)
[12572] Exiting Socket#61940669::Socket()
[12572] Exiting TcpClient#57416410::TcpClient()
[12572] TcpClient#57416410::TcpClient()
[12572] Exiting TcpClient#57416410::TcpClient()
[12572] Socket#15193904::Socket(AddressFamily#2)
[12572] Exiting Socket#15193904::Socket()
[17872] Socket#40528290::Socket(AddressFamily#2)
[17872] Exiting Socket#40528290::Socket()
[17872] Socket#40528290::Bind(0.0.0.0:10002#10002)
[17872] Exiting Socket#40528290::Bind()
[17872] Socket#40528290::Listen(Int32#100)
[17872] Exiting Socket#40528290::Listen()
in connect WHILE 1
[17872] Socket#40528290::BeginAccept()
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#515737
The thread '1' (0x6674) has exited with code 0 (0x0).
The thread '<No Name>' (0x1df8) has exited with code 0 (0x0).
The thread '<No Name>' (0x5dac) has exited with code 0 (0x0).
[7984] Socket#49538252::Socket()
[7984] Exiting Socket#49538252::Socket()
in connect WHILE 2
AcceptCallback Start1
[17872] Socket#40528290::BeginAccept()
[7984] Socket#40528290::EndAccept(AcceptAsyncResult#515737)
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#27334100
System.Net.Sockets Information: 0 : [7984] Socket#49538252 - Accepted connection from 127.0.0.1:63817 to 127.0.0.1:10002.
[7984] Exiting Socket#40528290::EndAccept() -> Socket#49538252
127.0.0.1:63817
[7984] Socket#49538252::BeginReceive()
[7984] Exiting Socket#49538252::BeginReceive() -> OverlappedAsyncResult#62696216
AcceptCallback End 1
[22168] Data from Socket#49538252::PostCompletion
[22168] 00000000 : 32 30 30 30 30 30 30 30-30 30 38 33 30 30 31 58 : 100000000093001X <-- SCANTEXT RECEIVED, TEST1
[22168] 00000010 : 0A : .
ReceiveCallback In 1
[22168] Socket#49538252::EndReceive(OverlappedAsyncResult#62696216)
[22168] Exiting Socket#49538252::EndReceive() -> Int32#17
ReceiveCallback receiveMsgSize 17
[22168] Socket#49538252::BeginSend()
[22168] Exiting Socket#49538252::BeginSend() -> OverlappedAsyncResult#13462887
A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll
[19228] Data from Socket#49538252::PostCompletion
[19228] 00000000 : 07 : . <-- RESPONSE RECEIVED, TEST1
[19228] Socket#49538252::EndSend(OverlappedAsyncResult#13462887)
[19228] Exiting Socket#49538252::EndSend() -> Int32#1
[19228] Socket#49538252::BeginReceive()
[19228] Exiting Socket#49538252::BeginReceive() -> OverlappedAsyncResult#25961440
SendCallBack 1 1
[7984] Data from Socket#49538252::PostCompletion
[7984] 00000000 : :
ReceiveCallback In 2
[7984] Socket#49538252::EndReceive(OverlappedAsyncResult#25961440)
[7984] Exiting Socket#49538252::EndReceive() -> Int32#0
ReceiveCallback Exit 2
ReceiveCallback Exit 2
[22168] Socket#31352595::Socket()
[22168] Exiting Socket#31352595::Socket()
AcceptCallback Start2
in connect WHILE 3
[17872] Socket#40528290::BeginAccept()
[22168] Socket#40528290::EndAccept(AcceptAsyncResult#27334100)
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#39421196
System.Net.Sockets Information: 0 : [22168] Socket#31352595 - Accepted connection from 127.0.0.1:63820 to 127.0.0.1:10002.
[22168] Exiting Socket#40528290::EndAccept() -> Socket#31352595
127.0.0.1:63820
[22168] Socket#31352595::BeginReceive()
[22168] Exiting Socket#31352595::BeginReceive() -> OverlappedAsyncResult#28002689
AcceptCallback End 2
[7984] Data from Socket#31352595::PostCompletion
[7984] 00000000 : 32 30 30 30 30 30 30 30-30 30 38 33 30 30 31 58 : 100000000093001X <-- SCANTEXT RECEIVED, TEST2
[7984] 00000010 : 0A : .
ReceiveCallback In 3
[7984] Socket#31352595::EndReceive(OverlappedAsyncResult#28002689)
[7984] Exiting Socket#31352595::EndReceive() -> Int32#17
ReceiveCallback receiveMsgSize 17
[7984] Socket#31352595::BeginSend()
[7984] Exiting Socket#31352595::BeginSend() -> OverlappedAsyncResult#31071611
[22168] Data from Socket#31352595::PostCompletion
A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll
[22168] 00000000 : 07 : . <-- RESULT RECEIVED, TEST2
[22168] Socket#31352595::EndSend(OverlappedAsyncResult#31071611)
[22168] Exiting Socket#31352595::EndSend() -> Int32#1
[22168] Socket#31352595::BeginReceive()
[22168] Exiting Socket#31352595::BeginReceive() -> OverlappedAsyncResult#51673536
SendCallBack 2 1
[4640] Data from Socket#31352595::PostCompletion
[4640] 00000000 : :
ReceiveCallback Exit 3
The thread '<No Name>' (0x4b1c) has exited with code 0 (0x0).
ReceiveCallback In 4
[4640] Socket#31352595::EndReceive(OverlappedAsyncResult#51673536)
[4640] Exiting Socket#31352595::EndReceive() -> Int32#0
ReceiveCallback Exit 4
[4640] Socket#37088038::Socket()
[4640] Exiting Socket#37088038::Socket()
AcceptCallback Start3
[4640] Socket#49538252::EndAccept(AcceptAsyncResult#39421196)
in connect WHILE 4
[17872] Socket#40528290::BeginAccept()
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#8948635
//ERROR THROWN HERE
AcceptCallback End 3
使用 netstat 进行监控
服务启动时出现这个,说明服务正在监听10002端口
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
在第一次和第二次扫描(即成功)时一切正常。测试 Web 应用程序连接,发送 'scantext',各种回调委托被触发,我们得到了很好的回复。
第一次测试期间的 Netstat,建立连接后:
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
TCP 127.0.0.1:10002 apps:59574 ESTABLISHED
TCP 127.0.0.1:59574 apps:10002 ESTABLISHED
(然后扫描#1 完成)
调用testApp mTCP.Close()时,状态变为CLOSE_WAIT和FIN_WAIT_2
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
TCP 127.0.0.1:10002 apps:59574 CLOSE_WAIT
TCP 127.0.0.1:59574 apps:10002 FIN_WAIT_2
几分钟后,CLOSE 和 FIN 连接消失,但仍然有 LISTENER
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
接下来,发送扫描#2,它重复上述序列,结果成功。
在第三次扫描时,当它尝试建立新连接时,在这一行的 AcceptCallback 中抛出异常:
clntSock = servSock.ClntSock.EndAccept(asyncResult)
具体来说,它是一个 ArgumentException,'asyncResult was not created by a call to BeginAccept'(参见 EndAccept)
因此,出于我无法理解的原因,第三个测试的 IASyncResult 导致 EndAccept 方法抛出 ArgumentException。
总结错误 - Connect 调用的 BeginAccept 为 AcceptCallback 设置委托并传递 IASyncResult。 AcceptCallBacks EndAccept 抛出 ArgumentException
我意识到做事的方式不是最新的,我们最好做些别的事情,但是对于一件事和另一件事(时间、资源)我需要尝试做出轻微的调整工作。但我不明白为什么(现有客户端应用程序)CONNECT、扫描、扫描、扫描、扫描等工作,但(网络应用程序)CONNECT、扫描、DISCONNECT、CONNECT、扫描、DISCONNECT 在第三次扫描的 CONNECT
提前感谢您提出任何建议。
有人提出并回答了类似的问题here
... you are holding the socket object in a class-scoped variable. So, if
somehow the socket gets disconnected while the ReadCallback is
executing and you reconnect it (thereby changing the socket object) -
now they don't match, you are trying to complete the Async request
using the new object when the old one is the one actually completing.
在您的例子中,它是变量 cState
。在 Connect
方法上,如果您只是在本地声明一个包含套接字的 cState
的新实例,那么错误可能会停止发生。
Dim clientState As New ClientState(servSock)
clientState.ClntSock.Bind(New IPEndPoint(System.Net.IPAddress.Any, TCPPort))
clientState.ClntSock.Listen(Backlog)
While True
Dim result As IAsyncResult = servSock.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), clientState)
result.AsyncWaitHandle.WaitOne()
End While
另外,mTCP
应该在客户端配置。
我有一个旧版 (2008) Windows 服务应用程序(使用 System.ServiceProcess.ServiceBase),我需要以与现在的工作方式略有不同的方式来使用它。目前它启动并在特定端口上创建一个 TCPLISTENER,供客户端应用程序连接(它连接一次)以便 send/receive 请求到监听端口。这一切都很好。但是,适配需要使用网络应用程序连接到侦听器端口,然后 send/rcv 正常但接收后必须 DISCONNECT(*)
(* - 如果 web 应用程序不必在每次测试后断开与侦听套接字的连接,那会更好,但我无法实现这一目标。我可以在会话状态下保持 TCP 连接吗重复使用?)
无论如何,对于第一个 TWO connect/send/rcv/disconnect 测试,适配工作正常,但在 THIRD服务在套接字 class 的 EndAccept 中抛出 ArgumentException。
我不明白为什么第三次测试失败了。
这是简单的测试客户端。
Public Class ConnectDisconnectTCP
Inherits System.Web.UI.Page
Private IP As String = "127.0.0.1"
Private port As Int32 = 10002
Private mTCP As System.Net.Sockets.TcpClient
Private netStrm As NetworkStream
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oEnc As New ASCIIEncoding
Dim ScanText As String = "100000000093001X" + vbLf
Dim mTCP As System.Net.Sockets.TcpClient
'connect and create a tcpclient
mTCP = New System.Net.Sockets.TcpClient(IP, port)
netStrm = mTCP.GetStream()
netStrm.WriteAsync(oEnc.GetBytes(ScanText.ToString), 0, ScanText.ToString.Length)
If netStrm.CanRead Then
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As StringBuilder = New StringBuilder()
Dim numberOfBytesRead As Integer = 0
' Incoming message may be larger than the buffer size.
Do
numberOfBytesRead = netStrm.Read(myReadBuffer, 0, myReadBuffer.Length)
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
Loop While netStrm.DataAvailable
End If
'CLOSE
netStrm.Close()
mTCP.Close() 'Disposes this TcpClient instance and requests that the underlying TCP connection be closed.
mTCP = Nothing
Label1.Text = "Closed"
End Sub End Class
这是完成所有工作的服务上的 TCPListener class
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Public Class TCPListenerPort
Private currentAsynchResult As IAsyncResult
Private cState As ClientState
Private servSock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Private m_connectionstate As Int32 = 0
Dim sData As String = String.Empty
'counters
Public cnt As Int32 = 0
Public rcvcnt As Int32 = 0
Public acccnt As Int32 = 0
Public sndcnt As Int32 = 0
Public sndcal As Int32 = 0
Public cncnt As Int32 = 0
'Public Events
Public Event ConnectionState(ByVal enmState As Int32)
Public Event DataArrived(ByVal strData As Object)
Public Sub New()
End Sub
Public Sub New(ByVal dr As DataRow)
End Sub
Protected Overrides Sub Finalize()
Try
If Not cState Is Nothing Then
cState.ClntSock.Close()
End If
If Not servSock Is Nothing Then
servSock.Close()
End If
Catch ex As Exception
Throw ex
Finally
MyBase.Finalize()
End Try
End Sub
Private Class ClientState
Public Const BUFSIZE As Integer = 1024
Public mrcvBuffer As Byte() = New Byte(BUFSIZE - 1) {}
Public mSendBuffer As Byte() = New Byte(BUFSIZE - 1) {}
Private mclntSock As Socket = Nothing
Public Sub New(ByRef clntSock As Socket)
mclntSock = clntSock
ReDim mrcvBuffer(BUFSIZE)
ReDim mSendBuffer(BUFSIZE)
End Sub
Public ReadOnly Property SckConnected() As Boolean
Get
SckConnected = mclntSock.Connected
End Get
End Property
Public ReadOnly Property RcvBuffer() As Byte()
Get
RcvBuffer = mrcvBuffer
End Get
End Property
Public ReadOnly Property SendBuffer() As Byte()
Get
SendBuffer = mSendBuffer
End Get
End Property
Public ReadOnly Property ClntSock() As Socket
Get
ClntSock = mclntSock
End Get
End Property
End Class
Public Sub Connect(ByVal TCPPort As Int32, ByVal Backlog As Int32)
Try
cState = New ClientState(servSock)
cState.ClntSock.Bind(New IPEndPoint(System.Net.IPAddress.Any, TCPPort))
cState.ClntSock.Listen(100) '5
While True
cncnt = cncnt + 1
System.Diagnostics.Debug.WriteLine("in connect WHILE " + cncnt.ToString)
currentAsynchResult = servSock.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), cState)
currentAsynchResult.AsyncWaitHandle.WaitOne()
End While
Catch __unusedObjectDisposedException1__ As ObjectDisposedException
Catch e As Exception
End Try
End Sub
Private Sub AcceptCallback(ByVal asyncResult As IAsyncResult)
Try
acccnt = acccnt + 1
System.Diagnostics.Debug.WriteLine("AcceptCallback Start" + acccnt.ToString)
'obtain the Socket on which the connection attempt is being made
Dim servSock As ClientState = asyncResult.AsyncState 'Asyncstate gets a user defined object that qualifies or contains info about an async operation
Dim clntSock As Socket
clntSock = servSock.ClntSock.EndAccept(asyncResult)
System.Diagnostics.Debug.WriteLine(clntSock.RemoteEndPoint)
Dim cs As New ClientState(clntSock)
clntSock.BeginReceive(cs.RcvBuffer, 0, cs.RcvBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallback), cs)
Catch ar As ArgumentException
'!!!!!Test 3 errors here with
'The IAsyncResult object was not returned from the corresponding asynchronous method on this class.
Catch ex As Exception
End Try
System.Diagnostics.Debug.WriteLine("AcceptCallback End " + acccnt.ToString)
End Sub
Private Sub ReceiveCallback(ByVal asyncResult As IAsyncResult)
rcvcnt = rcvcnt + 1
System.Diagnostics.Debug.WriteLine("ReceiveCallback In " + rcvcnt.ToString)
Dim cs As ClientState = asyncResult.AsyncState
Dim AE As New System.Text.ASCIIEncoding
Dim recvMsgSize As Int32 = 0
Dim strTmp As String = String.Empty
Dim strSend As String = String.Empty
Try
recvMsgSize = cs.ClntSock.EndReceive(asyncResult)
strTmp = Replace(AE.GetString(cs.RcvBuffer), vbNullChar, "")
If recvMsgSize > 0 Then
System.Diagnostics.Debug.WriteLine("ReceiveCallback receiveMsgSize " + recvMsgSize.ToString)
If Right(strTmp, 1) = vbLf Or Right(strTmp, 1) = vbCr Then
strSend = sData + strTmp
strSend = strSend.Replace(vbLf, "")
strSend = strSend.Replace(vbCr, "")
cState = cs
sData = String.Empty
RaiseEvent DataArrived(strSend)
Else
End If
Else
End If
Catch ex As Exception
Throw ex
End Try
System.Diagnostics.Debug.WriteLine("ReceiveCallback Exit " + rcvcnt.ToString)
End Sub
Public Function SendData(ByVal strData As Object) As Boolean
' Change the data to a byte array if necessary, then send it via the socket
Dim oEncoder As New System.Text.ASCIIEncoding
Dim bytes As Byte()
Dim Result As IAsyncResult
Try
Select Case strData.GetType().ToString
Case "System.String" ' Convert a string to a byte array
bytes = oEncoder.GetBytes(strData)
Case "System.Byte[]" ' Send a byte array directly
bytes = strData
Case Else ' And just send anything else as-is
bytes = strData
End Select
sndcnt = sndcnt + 1
Result = cState.ClntSock.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, New AsyncCallback(AddressOf SendCallback), cState)
System.Diagnostics.Debug.WriteLine("SendData " + sndcnt + " " + Result.ToString)
Result.AsyncWaitHandle.WaitOne()
Catch ex As Exception
Throw ex
End Try
Return True
End Function
Private Sub SendCallback(ByVal asyncResult As IAsyncResult)
Dim cs As ClientState = asyncResult.AsyncState
Dim bytesSent As Int32 = 0
Try
bytesSent = cs.ClntSock.EndSend(asyncResult)
sndcal = sndcal + 1
cs.ClntSock.BeginReceive(cs.RcvBuffer, 0, cs.RcvBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceiveCallback), cs)
System.Diagnostics.Debug.WriteLine("SendCallBack " + sndcal.ToString + " " + bytesSent.ToString)
Catch ex As Exception
Throw (ex)
End Try
End Sub
End Class
启用 .net 跟踪,加上代码的系统诊断 debug.write,这里是 TEST1、TEST2、TEST3/CRASH 操作的完整日志。请注意“<-- SCANTEXT RECEIVED”和“<-- RESPONSE RECEIVED”注释。 'in connect WHILE 4' 注释显示第 3 次请求尝试的到来。
</p>
<pre><code> [12572] TcpClient#57416410::TcpClient(AddressFamily#2)
[12572] Socket#61940669::Socket(AddressFamily#2)
[12572] Exiting Socket#61940669::Socket()
[12572] Exiting TcpClient#57416410::TcpClient()
[12572] TcpClient#57416410::TcpClient()
[12572] Exiting TcpClient#57416410::TcpClient()
[12572] Socket#15193904::Socket(AddressFamily#2)
[12572] Exiting Socket#15193904::Socket()
[17872] Socket#40528290::Socket(AddressFamily#2)
[17872] Exiting Socket#40528290::Socket()
[17872] Socket#40528290::Bind(0.0.0.0:10002#10002)
[17872] Exiting Socket#40528290::Bind()
[17872] Socket#40528290::Listen(Int32#100)
[17872] Exiting Socket#40528290::Listen()
in connect WHILE 1
[17872] Socket#40528290::BeginAccept()
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#515737
The thread '1' (0x6674) has exited with code 0 (0x0).
The thread '<No Name>' (0x1df8) has exited with code 0 (0x0).
The thread '<No Name>' (0x5dac) has exited with code 0 (0x0).
[7984] Socket#49538252::Socket()
[7984] Exiting Socket#49538252::Socket()
in connect WHILE 2
AcceptCallback Start1
[17872] Socket#40528290::BeginAccept()
[7984] Socket#40528290::EndAccept(AcceptAsyncResult#515737)
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#27334100
System.Net.Sockets Information: 0 : [7984] Socket#49538252 - Accepted connection from 127.0.0.1:63817 to 127.0.0.1:10002.
[7984] Exiting Socket#40528290::EndAccept() -> Socket#49538252
127.0.0.1:63817
[7984] Socket#49538252::BeginReceive()
[7984] Exiting Socket#49538252::BeginReceive() -> OverlappedAsyncResult#62696216
AcceptCallback End 1
[22168] Data from Socket#49538252::PostCompletion
[22168] 00000000 : 32 30 30 30 30 30 30 30-30 30 38 33 30 30 31 58 : 100000000093001X <-- SCANTEXT RECEIVED, TEST1
[22168] 00000010 : 0A : .
ReceiveCallback In 1
[22168] Socket#49538252::EndReceive(OverlappedAsyncResult#62696216)
[22168] Exiting Socket#49538252::EndReceive() -> Int32#17
ReceiveCallback receiveMsgSize 17
[22168] Socket#49538252::BeginSend()
[22168] Exiting Socket#49538252::BeginSend() -> OverlappedAsyncResult#13462887
A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll
[19228] Data from Socket#49538252::PostCompletion
[19228] 00000000 : 07 : . <-- RESPONSE RECEIVED, TEST1
[19228] Socket#49538252::EndSend(OverlappedAsyncResult#13462887)
[19228] Exiting Socket#49538252::EndSend() -> Int32#1
[19228] Socket#49538252::BeginReceive()
[19228] Exiting Socket#49538252::BeginReceive() -> OverlappedAsyncResult#25961440
SendCallBack 1 1
[7984] Data from Socket#49538252::PostCompletion
[7984] 00000000 : :
ReceiveCallback In 2
[7984] Socket#49538252::EndReceive(OverlappedAsyncResult#25961440)
[7984] Exiting Socket#49538252::EndReceive() -> Int32#0
ReceiveCallback Exit 2
ReceiveCallback Exit 2
[22168] Socket#31352595::Socket()
[22168] Exiting Socket#31352595::Socket()
AcceptCallback Start2
in connect WHILE 3
[17872] Socket#40528290::BeginAccept()
[22168] Socket#40528290::EndAccept(AcceptAsyncResult#27334100)
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#39421196
System.Net.Sockets Information: 0 : [22168] Socket#31352595 - Accepted connection from 127.0.0.1:63820 to 127.0.0.1:10002.
[22168] Exiting Socket#40528290::EndAccept() -> Socket#31352595
127.0.0.1:63820
[22168] Socket#31352595::BeginReceive()
[22168] Exiting Socket#31352595::BeginReceive() -> OverlappedAsyncResult#28002689
AcceptCallback End 2
[7984] Data from Socket#31352595::PostCompletion
[7984] 00000000 : 32 30 30 30 30 30 30 30-30 30 38 33 30 30 31 58 : 100000000093001X <-- SCANTEXT RECEIVED, TEST2
[7984] 00000010 : 0A : .
ReceiveCallback In 3
[7984] Socket#31352595::EndReceive(OverlappedAsyncResult#28002689)
[7984] Exiting Socket#31352595::EndReceive() -> Int32#17
ReceiveCallback receiveMsgSize 17
[7984] Socket#31352595::BeginSend()
[7984] Exiting Socket#31352595::BeginSend() -> OverlappedAsyncResult#31071611
[22168] Data from Socket#31352595::PostCompletion
A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll
[22168] 00000000 : 07 : . <-- RESULT RECEIVED, TEST2
[22168] Socket#31352595::EndSend(OverlappedAsyncResult#31071611)
[22168] Exiting Socket#31352595::EndSend() -> Int32#1
[22168] Socket#31352595::BeginReceive()
[22168] Exiting Socket#31352595::BeginReceive() -> OverlappedAsyncResult#51673536
SendCallBack 2 1
[4640] Data from Socket#31352595::PostCompletion
[4640] 00000000 : :
ReceiveCallback Exit 3
The thread '<No Name>' (0x4b1c) has exited with code 0 (0x0).
ReceiveCallback In 4
[4640] Socket#31352595::EndReceive(OverlappedAsyncResult#51673536)
[4640] Exiting Socket#31352595::EndReceive() -> Int32#0
ReceiveCallback Exit 4
[4640] Socket#37088038::Socket()
[4640] Exiting Socket#37088038::Socket()
AcceptCallback Start3
[4640] Socket#49538252::EndAccept(AcceptAsyncResult#39421196)
in connect WHILE 4
[17872] Socket#40528290::BeginAccept()
[17872] Exiting Socket#40528290::BeginAccept() -> AcceptAsyncResult#8948635
//ERROR THROWN HERE
AcceptCallback End 3
使用 netstat 进行监控
服务启动时出现这个,说明服务正在监听10002端口
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
在第一次和第二次扫描(即成功)时一切正常。测试 Web 应用程序连接,发送 'scantext',各种回调委托被触发,我们得到了很好的回复。
第一次测试期间的 Netstat,建立连接后:
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
TCP 127.0.0.1:10002 apps:59574 ESTABLISHED
TCP 127.0.0.1:59574 apps:10002 ESTABLISHED
(然后扫描#1 完成)
调用testApp mTCP.Close()时,状态变为CLOSE_WAIT和FIN_WAIT_2
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
TCP 127.0.0.1:10002 apps:59574 CLOSE_WAIT
TCP 127.0.0.1:59574 apps:10002 FIN_WAIT_2
几分钟后,CLOSE 和 FIN 连接消失,但仍然有 LISTENER
TCP 0.0.0.0:10002 LON-WKS-ZER01:0 LISTENING
接下来,发送扫描#2,它重复上述序列,结果成功。
在第三次扫描时,当它尝试建立新连接时,在这一行的 AcceptCallback 中抛出异常:
clntSock = servSock.ClntSock.EndAccept(asyncResult)
具体来说,它是一个 ArgumentException,'asyncResult was not created by a call to BeginAccept'(参见 EndAccept)
因此,出于我无法理解的原因,第三个测试的 IASyncResult 导致 EndAccept 方法抛出 ArgumentException。
总结错误 - Connect 调用的 BeginAccept 为 AcceptCallback 设置委托并传递 IASyncResult。 AcceptCallBacks EndAccept 抛出 ArgumentException
我意识到做事的方式不是最新的,我们最好做些别的事情,但是对于一件事和另一件事(时间、资源)我需要尝试做出轻微的调整工作。但我不明白为什么(现有客户端应用程序)CONNECT、扫描、扫描、扫描、扫描等工作,但(网络应用程序)CONNECT、扫描、DISCONNECT、CONNECT、扫描、DISCONNECT 在第三次扫描的 CONNECT
提前感谢您提出任何建议。
有人提出并回答了类似的问题here
... you are holding the socket object in a class-scoped variable. So, if somehow the socket gets disconnected while the ReadCallback is executing and you reconnect it (thereby changing the socket object) - now they don't match, you are trying to complete the Async request using the new object when the old one is the one actually completing.
在您的例子中,它是变量 cState
。在 Connect
方法上,如果您只是在本地声明一个包含套接字的 cState
的新实例,那么错误可能会停止发生。
Dim clientState As New ClientState(servSock)
clientState.ClntSock.Bind(New IPEndPoint(System.Net.IPAddress.Any, TCPPort))
clientState.ClntSock.Listen(Backlog)
While True
Dim result As IAsyncResult = servSock.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), clientState)
result.AsyncWaitHandle.WaitOne()
End While
另外,mTCP
应该在客户端配置。