T
Terry Olsen
I'm trying to use Async Socket calls to create a tcp server. It goes into
the listening correctly, but when I connect, it crashes with the following
exception:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.dll
Additional information: AcceptCallback
There is no source code available for the current location.
Here's the code. Hopefully someone can help me out.
-----------------------------------------------------------
Private sck As Socket 'Class level
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
Dim addr As IPAddress = IPAddress.Parse("0.0.0.0")
Dim ep As New IPEndPoint(addr, CInt(txtPort.Text))
Dim sck As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
sck.Bind(ep)
sck.Listen(2)
sck.BeginAccept(AddressOf AcceptCallback, Nothing)
lblStatus.Text = "Listening"
End Sub
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
sck.EndAccept(ar) 'I believe this is my problem
Dim bytes(4095) As Byte
sck.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, _
AddressOf ReceiveCallback, bytes)
End Sub
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = sck.EndReceive(ar)
If numbytes = 0 Then
Else
End If
End Sub
the listening correctly, but when I connect, it crashes with the following
exception:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.dll
Additional information: AcceptCallback
There is no source code available for the current location.
Here's the code. Hopefully someone can help me out.
-----------------------------------------------------------
Private sck As Socket 'Class level
Private Sub btnStart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStart.Click
Dim addr As IPAddress = IPAddress.Parse("0.0.0.0")
Dim ep As New IPEndPoint(addr, CInt(txtPort.Text))
Dim sck As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
sck.Bind(ep)
sck.Listen(2)
sck.BeginAccept(AddressOf AcceptCallback, Nothing)
lblStatus.Text = "Listening"
End Sub
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
sck.EndAccept(ar) 'I believe this is my problem
Dim bytes(4095) As Byte
sck.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, _
AddressOf ReceiveCallback, bytes)
End Sub
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = sck.EndReceive(ar)
If numbytes = 0 Then
Else
End If
End Sub