.NET Sockets Exception on application.exit

AVR

New member
Joined
Jan 5, 2006
Messages
3
Programming Experience
10+
Hi,

I have an application that I have been developing. It communicates with a server and some other devices over a network. It is running on a Pocket PC using the .NET Compact framework. VS2003.

The application runs perfectly and is stable over long periods of time (weeks). However, when closing the application I get an error. Using application.exit will close the application but then I get an error.

The problem appears to be with the receive callback. .NET runs the callback on a separate thread. My guess is that the I must shutdown the callback correctly before closing the application.

Here is some sample code from my application:

Private Sub connect()
Try
If clientsocket.Connected = True Then
clientsocket.Shutdown(SocketShutdown.Both)
clientsocket.Close()
End If
Catch
End Try

Dim addr As IPAddress = IPAddress.Parse(ip.Text)
If Not addr Is Nothing Then
Try
Dim EP As New IPEndPoint(addr, CInt("9004"))
clientsocket = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
clientsocket.BeginConnect(EP, AddressOf connectcallback, Nothing)
Catch
End Try
End If
End Sub

‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

Private Sub connectcallback(ByVal ar As IAsyncResult)
If clientsocket.Connected = True Then
Try
clientsocket.EndConnect(ar)
Dim bytes(1024) As Byte
clientsocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, _
AddressOf receivecallback, bytes)
Me.Invoke(New EventHandler(AddressOf connectedui))
Catch
End Try
Else
End If
End Sub

‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
Private Sub receivecallback(ByVal ar As IAsyncResult)
Try
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = clientsocket.EndReceive(ar)

'clear(Buffer)
'Array.Clear(bytes, 0, bytes.Length)

bufferstring = bufferstring + ascii.GetString(bytes, 0, numbytes)

If (Mid$(bufferstring, bufferstring.Length - 1)) = Chr(13) + Chr(10) Then
data = bufferstring
bufferstring = ""
Me.Invoke(New EventHandler(AddressOf newin))
End If

'begin receive again
clientsocket.BeginReceive(bytes, 0, bytes.Length, _
SocketFlags.None, AddressOf receivecallback, bytes)
Catch
End Try
End Sub

‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

Private Sub newin(ByVal sender As Object, ByVal e As EventArgs)

‘Do something to the UI with data that has been received.

Something=data

End Sub

I have omitted the connectedui sub which simply displays a message saying connected. Essentially the above code handles the connection, the connection callback and the receive callback. The receive callback also collates data until a CRLF is received. All data that I receive ends in CRLF, however, as these components are sending data from a microcontroller it is not guranteed that each packet will contain the entire ASCII string that is terminated with CRLF, as a single string may be sent over 2 or three packets. The ‘bufferstring’ above collates data until it ends with CRLF and then invokes an event handler to the UI thread to do something with the variable data. Please note that this application is for the .NET compact framework 1.1 so it can not have asynchronous delegates invoked on the UI thread.

Anyway, my code works perfectly and never crashes, that is until I want to close the application.

How should I close the application correctly?

I shutdown the sockets if they are connected which helps but I still get errors occasionally. Object disposed exceptions or an error message I do not understand but it points at the receive callback as being the problem.

Any help would be appreciated.

Thanks.
 
Back
Top