Problem with UDP socket under NET Framework

  • Thread starter Thread starter Volodia
  • Start date Start date
V

Volodia

I have a problem with using UPD under NET framwork.
The problem consist if any client disconnect under UDP protocol
from server ( code below ) it raise exeption 10054 -
It shouldn't be under UDP protocol - becouse this protocol is
connectionless and does not have any notification about closing.

Exeption 10054 is:
An existing connection was forcibly closed by the remote host.

Thanks,
Volodia.


UDP server code (by the way - client done in unmanaged C++) :

Sub ThreadProc()

_ReceivingSocket = New Socket(AddressFamily.InterNetwork, _
SocketType.Dgram, ProtocolType.Udp)

_ReceivingSocket.SetSocketOption(SocketOptionLevel.Socket, _
SocketOptionName.ReuseAddress, 1)

Dim listenEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, _Port)
_ReceivingSocket.Bind(listenEndPoint)

Dim state As New StateObject
Dim nodecallback As New AsyncCallback(AddressOf RecieveCallback)

Dim nodeIP As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim nodeEP As EndPoint = DirectCast(nodeIP, EndPoint)

While NodeListenerRun > 0

ReadDone.Reset()

Try

_ReceivingSocket.BeginReceiveFrom(state.buffer, 0, _
state.BufferSize, SocketFlags.None, nodeEP,
nodecallback, state)

ReadDone.WaitOne()

Catch sex As SocketException

If (sex.ErrorCode = 10040) Then
state.IncreaseBuffer()
Else
SwitchSocketOff()
End If

Catch exp As Exception

CloseListening()

End Try

End While

End sub


Private Shared Sub RecieveCallback(ByVal ar As IAsyncResult)

Try
'
' Get state object
'
Dim state As StateObject = CType(ar.AsyncState, StateObject)

'
' Creates a temporary EndPoint to pass to EndReceiveFrom.
'
Dim sender As New IPEndPoint(IPAddress.Any, 0)
Dim fromEP As EndPoint = CType(sender, EndPoint)

'
' Get number of bytes.
'
Dim bytesRead As Integer = state.workSocket.EndReceiveFrom(ar,
fromEP)
Dim str As String

If bytesRead > 0 Then

str = Encoding.ASCII.GetChars(state.buffer, 0, bytesRead)

'
' Process data
'
ClientResponce(state, fromEP, str)

End If

Catch sex As SocketException

If (sex.ErrorCode = 10054) Then

SwitchSocketOff()
Tracing("Listener RecieveCallback : " & sex.Message)

End If

Catch ex As Exception

Tracing("Listener RecieveCallback error: " & ex.ToString)

End Try

'
' All data was get, so reset event.
'
ReadDone.Set()

End Sub
 
Back
Top