Results 1 to 13 of 13

Thread: How to LOOP this ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    How to LOOP this ?

    VB Code:
    1. Public Function receive() As String
    2.         Dim stringBuffer As String
    3.  
    4.         Dim recvBuffer1(tcpClient.ReceiveBufferSize) As Byte
    5.         Dim recvBuffer2(tcpClient.ReceiveBufferSize) As Byte
    6.         Dim recvBuffer3(tcpClient.ReceiveBufferSize) As Byte
    7.         Dim recvBuffer4(tcpClient.ReceiveBufferSize) As Byte
    8.         Dim recvBuffer5(tcpClient.ReceiveBufferSize) As Byte
    9.        
    10.      
    11.  
    12.         ns.Read(recvBuffer1, 0, CInt(tcpClient.ReceiveBufferSize))
    13.         stringBuffer = Encoding.ASCII.GetString(recvBuffer, 0, CInt(tcpClient.ReceiveBufferSize))
    14.  
    15.         ns.Read(recvBuffer2, 0, CInt(tcpClient.ReceiveBufferSize))
    16.         stringBuffer = stringBuffer + Encoding.ASCII.GetString(recvBuffer2, 0, CInt(tcpClient.ReceiveBufferSize))
    17.  
    18.         ns.Read(recvBuffer3, 0, CInt(tcpClient.ReceiveBufferSize))
    19.         stringBuffer = stringBuffer + Encoding.ASCII.GetString(recvBuffer3, 0, CInt(tcpClient.ReceiveBufferSize))
    20.  
    21.         ns.Read(recvBuffer4, 0, CInt(tcpClient.ReceiveBufferSize))
    22.         stringBuffer = stringBuffer + Encoding.ASCII.GetString(recvBuffer4, 0, CInt(tcpClient.ReceiveBufferSize))
    23.  
    24.         ns.Read(recvBuffer5, 0, CInt(tcpClient.ReceiveBufferSize))
    25.         stringBuffer = stringBuffer + Encoding.ASCII.GetString(recvBuffer5, 0, CInt(tcpClient.ReceiveBufferSize))
    26.  
    27.          Return stringBuffer
    28.  
    29. End Function

    Anyone know how to loop this ? for your information, the number of receive buffer is LOOP depending on the networkstream. If data haven't finish read, it's shall not create another receive buffer. anyone ?

  2. #2
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Question Re: How to LOOP this ?

    what do you wand to make ?


  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How to LOOP this ?

    Hi,

    If you are saying you want to handle an unknown number of recvBuffers then try

    Dim recvBuffers(0) As Byte


    then loop through with a counter starting at 0; adding the byte to recvBuffers(Counter); then increment counter by 1 until no more bytes.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    sorry for that, let me explain it.

    I would like to append the string(receive in terms of byte array) until the last data from the networkstream comes in. I do this because the amount of data that I receive is very big(picture) and I wish to receive all the byte and converting it back to string without cut off. And after that return it. : ) mind to find out ?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    Quote Originally Posted by taxes
    Hi,

    If you are saying you want to handle an unknown number of recvBuffers then try

    Dim recvBuffers(0) As Byte


    then loop through with a counter starting at 0; adding the byte to recvBuffers(Counter); then increment counter by 1 until no more bytes.
    mind to code it out? I am newbie in vb... thanks..

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: How to LOOP this ?

    You 'd need to change how you declare your arrays...use an array of arrays instead!

    VB Code:
    1. Dim recvBuffers(4)() As Byte 'an array of arrays
    2.  
    3.     recvBuffers(0) = New Byte(tcpClient.ReceiveBufferSize) {}
    4.     recvBuffers(1) = New Byte(tcpClient.ReceiveBufferSize) {}
    5.     recvBuffers(2) = New Byte(tcpClient.ReceiveBufferSize) {}
    6.     recvBuffers(3) = New Byte(tcpClient.ReceiveBufferSize) {}
    7.     recvBuffers(4) = New Byte(tcpClient.ReceiveBufferSize) {}
    8.  
    9.     For i As Integer = 0 To 4
    10.         ns.Read(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    11.         stringBuffer = Encoding.ASCII.GetString(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    12.     Next i

    Let us know how you get on.
    I don't live here any more.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    why am I doing that way? Because I found out that the maximum buffer size for the receivebuffersize is 8192 bytes. My biggest problem is that I can't receive fully from the server so I need to create many receivebuffersize and append the string. I also need to return the data ONLY after it's finish reading the last data from the server. thanks !

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    Quote Originally Posted by wossname
    You 'd need to change how you declare your arrays...use an array of arrays instead!

    VB Code:
    1. Dim recvBuffers(4)() As Byte 'an array of arrays
    2.  
    3.     recvBuffers(0) = New Byte(tcpClient.ReceiveBufferSize) {}
    4.     recvBuffers(1) = New Byte(tcpClient.ReceiveBufferSize) {}
    5.     recvBuffers(2) = New Byte(tcpClient.ReceiveBufferSize) {}
    6.     recvBuffers(3) = New Byte(tcpClient.ReceiveBufferSize) {}
    7.     recvBuffers(4) = New Byte(tcpClient.ReceiveBufferSize) {}
    8.  
    9.     For i As Integer = 0 To 4
    10.         ns.Read(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    11.         stringBuffer = Encoding.ASCII.GetString(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    12.     Next i

    Let us know how you get on.


    No, this doesn't work. The number of recvbuffer created will need to base on how many data to receive. Could you try it for me ? thanks!

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How to LOOP this ?

    Hi,

    Having had another look at your code why not

    Pseudo Code

    Public Function receive() As String
    Dim stringBuffer As String
    Dim recvBuffer1(tcpClient.ReceiveBufferSize) As Byte

    Do While (not end of file)
    ns.Read(recvBuffer1, 0, CInt(tcpClient.ReceiveBufferSize))
    stringBuffer += Encoding.ASCII.GetString(recvBuffer, 0, CInt
    (tcpClient.ReceiveBufferSize))
    (here clear recvBuffer1 if necessary)
    Loop

    Return stringBuffer

    End Function

    Sorry, I'm not familiar with what you are trying to do so I'm not sure about the use of ns.Read, so I've used Pseudo Code.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    Quote Originally Posted by taxes
    Hi,

    Having had another look at your code why not

    Pseudo Code

    Public Function receive() As String
    Dim stringBuffer As String
    Dim recvBuffer1(tcpClient.ReceiveBufferSize) As Byte

    Do While (not end of file)
    ns.Read(recvBuffer1, 0, CInt(tcpClient.ReceiveBufferSize))
    stringBuffer += Encoding.ASCII.GetString(recvBuffer, 0, CInt
    (tcpClient.ReceiveBufferSize))
    (here clear recvBuffer1 if necessary)
    Loop

    Return stringBuffer

    End Function

    Sorry, I'm not familiar with what you are trying to do so I'm not sure about the use of ns.Read, so I've used Pseudo Code.

    My problem is mainly base on how to receive fully from the server before it return the data to where it calls.

    I know there is a (in my code, 'ns' = networkstream) networkstream.dataAvailable. It seems like much more close to what I suppost to do. The main objective of this receive class is to receive all the data from server before it return. And another problem, the buffer is only 8192byte and it need to create multiple recvBuffer in order to receive all the data.

    Just to make it easy to understand:

    The data is about 10000 long. If the buffersize is only 100, I need to create 100 recvBuffer and append the data in order. The data is surely need to be fully receive and the number of recvBuffer created is depend on the data.


    Thanks for you people who tried it for me, I appreciate it. And I hope I can get what I am need soon. Thanks !

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How to LOOP this ?

    Hi,

    Are you saying that you don't know how to get data from a file?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2004
    Posts
    70

    Re: How to LOOP this ?

    Quote Originally Posted by taxes
    Hi,

    Are you saying that you don't know how to get data from a file?
    No, I don't know how to receive the data until the last data comes in. In short, don't know how to receive fully from server before it return.

    I got my code simplified.

    VB Code:
    1. Public Function receive() As String
    2.         Dim stringBuffer As String
    3.         Dim recvBuffers(4)() As Byte 'an array of arrays              
    4.                                                                                                  |
    5.         recvBuffers(0) = New Byte(tcpClient.ReceiveBufferSize) {}           |
    6.         recvBuffers(1) = New Byte(tcpClient.ReceiveBufferSize) {}           |-----> this is the part I am concerntrate on. Which have to decide how many recvBuffers to create
    7.         recvBuffers(2) = New Byte(tcpClient.ReceiveBufferSize) {}           |
    8.         recvBuffers(3) = New Byte(tcpClient.ReceiveBufferSize) {}           |
    9.         recvBuffers(4) = New Byte(tcpClient.ReceiveBufferSize) {}           |
    10.                                                                                        
    11.         For i As Integer = 0 To 4 Step 1 -------------------->number of loop also depending on how many recvBuffer created                                    
    12.             ns.Read(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    13.             stringBuffer = stringBuffer + Encoding.ASCII.GetString(recvBuffers(i), 0, CInt(tcpClient.ReceiveBufferSize))
    14.  
    15.         Next
    16.         Return stringBuffer
    17.     End Function

    In case you don't aware, the number of recvBuffer is base on data comes in. If still have data, create another recvbuffer. if no, then exit the loop.
    thanks wossname for the code above. Texas...can you fugure out.. ?

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: How to LOOP this ?

    Hi,

    You don't need to use more than one recvBuffers if you are simply going to add it's contents to the string. It's just a question of looping until the end of file marker is received, provided that the maximum length of a string variable is not exceeded, in which case you would use a string array.

    My problem is that I am not familiar with handling Byte input as opposed to characters. If no one else chips in with an answer, I will do some testing this afternoon.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width