I have a Window program (vb) which has a UDP listener. I'm having
problem terminating this program as the program is listening to a port
and not to my btnExit_Click event.
Here is the code:
Dim sErr As String = ""
Dim sLog As String = ""
Dim ipAddress As IPAddress
'Create a UDP socket.
Dim listener As Socket = New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim ipHostInfo As IPHostEntry
ipHostInfo = Dns.Resolve(Dns.GetHostName())
ipAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, CType(sPortNumb,
Integer))
listener.Bind(localEndPoint)
Dim msgLen As Int32 = 900
Dim i As Int32
Dim received(900) As Byte
While Not (bTerminated)
For i = 0 To msgLen - 1
received(i) = Byte.Parse("0")
Next
Dim tmpIpEndPoint As IPEndPoint = New IPEndPoint(ipAddress,
CType(sPortNumb, Integer))
Application.DoEvents()
Dim remoteEP As EndPoint = (tmpIpEndPoint)
Dim bytesReceived As Integer = listener.ReceiveFrom(received,
remoteEP)
Dim sRecvd As String =
Encoding.ASCII.GetString(received).Trim(Chr(0))
txtMsg.text = "Message Received: " & sRecvd
Application.DoEvents()
tmpIpEndPoint = Nothing
End While
listener.Shutdown(SocketShutdown.Receive)
listener.Close()
listener = Nothing
Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
bTerminated = True
me.Close()
End Sub
|