none
tcpclient.getstream.beginread中回调函数的疑问 RRS feed

  • 问题

  • 有这样一段程序: 
    Code Snippet

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
            Dim client As TcpClient = New TcpClient("las.sinica.edu.tw", 23) 
            Dim reader As StreamReader = New StreamReader(client.GetStream(), Encoding.Default) 

            client.GetStream().BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing) 

            rtxtCmd.Text = client.GetStream().BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing).AsyncState 
        End Sub 
        Private Sub DoRead(ByVal ar As IAsyncResult) 
            Dim bytesRead As Integer 
            Dim strMessage As String 
            Try 
                bytesRead = client.GetStream.EndRead(ar) 
                If bytesRead < 1 Then 
                    MessageBox.Show("Disconnected!") 
                    Exit Sub 
                End If 
                strMessage = Encoding.ASCII.GetString(readBuffer, 0, bytesRead - 2) 
                client.GetStream.BeginRead(readBuffer, 0, 255, New AsyncCallback(AddressOf DoRead), Nothing) 
            Catch ex As Exception 
                MessageBox.Show(ex.Message) 
            End Try 



    其中GetStream.beginread方法的回调函数是DoRead,所以每次Form1 load的时候BeginRead方法完成之后会调用DoRead,但是DoRead过程中的BeginRead方法的回调函数也是DoRead,这样不是会陷入死循环吗? 请高手解答,谢谢
    2008年10月30日 4:43

答案

全部回复

  •  

        谢谢楼上的,我知道这叫递归,我的疑问就是这个递归过程的终止条件在哪里?
    2008年10月30日 5:23
  •  picat 写:

     

        谢谢楼上的,我知道这叫递归,我的疑问就是这个递归过程的终止条件在哪里?

     

    Code Snippet

    If bytesRead < 1 Then
                    MessageBox.Show("Disconnected!")
                    Exit Sub
                End If

     

     

    2008年10月30日 5:28
    版主
  • 先谢谢楼上的回答,由于我急着找答案所以没有自己看上面的代码,不好意思,现在我把碰到问题的完整代码贴上来:

    Code Snippet

    Imports System.Net.Sockets
    Imports System.Text


    Public Class TCPBroadcast
        Const READ_BUFFER_SIZE As Integer = 255
        Private client As TcpClient
        Private readBuffer(READ_BUFFER_SIZE) As Byte
        Private _Name As String

        Public Sub New(ByVal listener As TcpClient)
            Me.client = listener
            Me.client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceived, Nothing)
        End Sub

        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property

     

        Private Sub StreamReceived(ByVal asRes As IAsyncResult)
            Dim BytesRead As Integer
            Dim Message As String
            Try
                SyncLock client.GetStream
                    BytesRead = client.GetStream.EndRead(asRes)
                End SyncLock
                Message = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
                RaiseEvent DataReceived(Me, Message)
                SyncLock client.GetStream
                    client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceived, Nothing)
                End SyncLock
            Catch ex As Exception

            End Try

        End Sub

     

        Public Event DataReceived(ByVal tcpSender As TCPBroadcast, ByVal data As String)


        Public Sub SendMsg(ByVal data As String)
            SyncLock client.GetStream
                Dim writer As New IO.StreamWriter(Me.client.GetStream)
                writer.Write(data & Chr(13) & Chr(10))
                writer.Flush()
            End SyncLock
        End Sub
    End Class

     

     

    其中的过程StreamReceived似乎没有退出条件,请看看是不是我的理解有问题?

    本代码出自<<vb.net windows 服务开发手册>>

     

    2008年10月30日 5:47
  • 没有

    2008年10月30日 6:00
    版主
  • 谢谢,那就结贴了,看来不能尽信书啊,呵呵.

     

    2008年10月30日 6:21