Link to home
Start Free TrialLog in
Avatar of imarshad
imarshadFlag for Pakistan

asked on

Raising an event....

Hi all,
        I am relatively new to VB.net (At least to writing code related to Events). My problem is this. I need to handle an event in Form1 that was raised from another class.
Visual Studio version = 2008
Description of code:
I have created a class named UDP for UDP hanling. I call the sub StartListen to start listening on a specific port. Now according to my knowledge the Event handler    
Public Sub UDP_Data(ByVal IPAdd As String, ByVal DataBytes As String) Handles UDP1.udpDataReceived
        MsgBox(DataBytes)
    End Sub
should be invoked each time I receive some data on the port. But it is not raised at all. I have confirmed that I receive data by inserting break point in the ReceiveMessages() sub where I receive the data....
What am I doing wrong? Why is the event not being raised or handled ?
Public Class UDP
    Public Event udpDataReceived(ByVal IPAddress As String, ByVal udpData As String)
    Dim GLOIP As Net.IPAddress
    Dim GLOINTPORT As Integer
    Dim bytCommand As Byte() = New Byte() {}
    Dim udpSend As New Net.Sockets.UdpClient
    Dim udpReceiver As New Net.Sockets.UdpClient
    Public RemoteIpEndPoint As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
    Public ThreadReceive As System.Threading.Thread
    Dim SocketNO As Integer
 
 
    Public Sub SendUDP(ByVal RemoteIP As String, ByVal RemotePort As Integer, ByVal MessageTOSend As String)
        Dim pRet As Integer
 
        Try
            GLOIP = Net.IPAddress.Parse(RemoteIP)
            udpSend.Connect(GLOIP, RemotePort)
            bytCommand = System.Text.Encoding.ASCII.GetBytes(MessageTOSend)
            pRet = udpSend.Send(bytCommand, bytCommand.Length)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub StartListen(ByVal PortNo As Integer)
        Try
            udpReceiver = New System.Net.Sockets.UdpClient(PortNo)
            ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
            ThreadReceive.Start()
        Catch x As Exception
            MsgBox(x.Message)
        End Try
 
    End Sub
    Private Sub ReceiveMessages()
        Try
            Dim receiveBytes As [Byte]() = udpReceiver.Receive(RemoteIpEndPoint)
            RaiseEvent udpDataReceived(RemoteIpEndPoint.Address.ToString, System.Text.Encoding.ASCII.GetChars(receiveBytes))
            NewInitialize()
        Catch e As Exception
            MsgBox(e.Message)
        End Try
    End Sub
    Private Sub NewInitialize()
        ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
        ThreadReceive.Start()
    End Sub
End Class
 
Public Class Form1
    Private WithEvents UDP1 As New UDP
    Public Sub UDP_Data(ByVal IPAdd As String, ByVal DataBytes As String) Handles UDP1.udpDataReceived
        MsgBox(DataBytes)
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ListeningUDP As New UDP
        ListeningUDP.StartListen(Val(txtIP.Text))
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imarshad

ASKER

Thanks webtubbs..... It solved the problem.... I knew it will be such a small mistake but I wasn't able to figure it out......