locked
tcplistener class (event-driven) RRS feed

  • Question

  • User-1335073632 posted

    I have write one program in vb.net which accepts data using port no and ip address after specific time using timer.

     

    Here is code

    Dim aTimer As New System.Timers.Timer()

    Private Shared Sub main()

    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

    aTimer.Start()

    aTimer.AutoReset = True

    aTimer.Interval = 5000

    aTimer.Enabled = True

    end sub

    Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)

    Try

    Dim listener As New TcpListener(21)

    listener.Start()

    Dim client As TcpClient = listener.AcceptTcpClient()

    Dim ns As NetworkStream = client.GetStream()

    ''-------------- Server Read ---------------------------------------------------------

    Dim bytes(1024) As Byte

    Dim bytesRead As Integer = ns.Read(bytes, 0, bytes.Length)

    Dim out As String = Encoding.ASCII.GetString(bytes, 0, bytesRead)

    client.Close()

    listener.Stop()

    End Sub

     

    This works fine. It gets data when timer event is fire.

    But i want event which will fire when data is arrived from network(DataArrived event for winsock control in vb6)

    Tuesday, August 26, 2008 9:05 AM

Answers

  • User1330954201 posted

    I don't think there is an Arrival event you can hook into. You'd need to set up a thread to listen on the port and process data as it comes in.

     There is some example code on the following msdn page :-

    http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2008 10:28 AM

All replies

  • User1330954201 posted

    I don't think there is an Arrival event you can hook into. You'd need to set up a thread to listen on the port and process data as it comes in.

     There is some example code on the following msdn page :-

    http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(VS.80).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2008 10:28 AM
  • User-1335073632 posted

    Thanks for reply.

    Saturday, August 30, 2008 1:13 AM