We help IT Professionals succeed at work.
Get Started

Acknowledging back from UDP port

1,874 Views
Last Modified: 2013-12-17

I am developing a GPS tracking application. The GPS data is coming into a UDP port 2000 from 150 GPS devices . The server need to acknowledge back to device everytime the data comes  .

 The application works perfect for a single  GPS device  in a single thread. But when I test (with a simulator application sending data from 150 different ports from  another machine)  I get following error  

A blocking operation was interrupted by a call to WSACancelBlockingCall

 What I did is , keep listening to the UDP port  in a thread and when data arrives, close the UDPClient temporarily and send acknowledgement from the socket and open the UDPClient and listen again   .
 As I told, it works  fine in a single thread . But the problem is , I can't acknowledge in the same thread of receiving because many devices are reporting at the same time . ( About  20% data coming in is lost  when single thread is used)

 Please help me with the  pseudocode (and sample code, if possible ) I have to use
   
Dim receivingUdpClient As UdpClient
    Dim receiveBytes As [Byte]()
    Public ThreadReceive As System.Threading.Thread
    Public RemoteIpEndPoint As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
   
 '''' Routine to listen UDP data 
    Public Sub ReceiveMessages()

        Try
            receiveBytes = receivingUdpClient.Receive(RemoteIpEndPoint)   ''' recieve from UDP port

             
            NewInitializeThread()  '' Initialise a new thread
            GC.Collect()

        Catch ex As Exception

        End Try

    End Sub


    Public Sub NewInitializeThread()
       ''' Actually this runs in a different thread
            AcknowledgeBack()  '' Routine to acknowledgeback 


        ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
        ThreadReceive.Start()

    End Sub

 Sub AcknowledgeBack(ByVal strIP As String, ByVal intPort As String, ByVal strHex As String)
        Dim strAcknowledgeHex As String

        receivingUdpClient.Close()

         
        SendWithUDPSocket(strIP, intPort, strAcknowledgeBytes)

        If Not receivingUdpClient Is Nothing Then
            receivingUdpClient = Nothing
        End If

        receivingUdpClient = New System.Net.Sockets.UdpClient(SocketNO)

    End Sub

   
   Function SendWithUDPSocket(ByVal LocalIP As IPAddress, ByVal LocalPort As Long _
                                 , ByVal MsgBytes As Byte())

        Dim _socket As Socket
        Try

            _socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            Dim endPoint = New IPEndPoint(LocalIP, LocalPort)
            _socket.Bind(endPoint)



            Dim _remoteEndPoint As IPEndPoint

            _remoteEndPoint = RemoteIpEndPoint ''New IPEndPoint(GLOIP, GLOINTPORT)
            _socket.SendTo(MsgBytes, MsgBytes.Length, SocketFlags.None, _remoteEndPoint)

            _socket.Close()



        Catch ex As Exception
            MsgBox("Error - " & ex.Message)
        End Try
        _socket = Nothing

        'Return _socket
        GC.Collect()

    End Function

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE