Send Data on NetworkStream Getting Stuck-VBForums
Results 1 to 10 of 10

Thread: Send Data on NetworkStream Getting Stuck

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Posts
    512

    Send Data on NetworkStream Getting Stuck

    The IDE seems to get stuck on this one line of code in our SendData. I wanted to see what you recommend to fix or debug this. We have tried a few debug.print type things. We are just getting some serialization over the network setup and tested.

    vb Code:
    1. ''' <summary>
    2.     ''' Sends binary data.
    3.     ''' </summary>
    4.     Public Sub sendData(ByVal data() As Byte, Optional ByVal curStream As NetworkStream = Nothing)
    5.         Try
    6.             Dim endbytes() As Byte = System.Text.Encoding.ASCII.GetBytes("{682427e7-6a56-4513-87f1-2dec81ddae2d}")
    7.  
    8.             Dim lenBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(data.Length)
    9.  
    10.             curStream.Write(lenBytes, 0, lenBytes.Length)  '<------- when paused, cursor goes here with Green arrow (it says it will continue here but it never does).
    11.             curStream.Write(endbytes, 0, endbytes.Length)
    12.  
    13.             curStream.Write(data, 0, data.Length)
    14.             curStream.Flush()
    15.  
    16.         Catch
    17.  
    18.         End Try
    19.  
    20.         'If curStream IsNot Nothing Then
    21.         '    curStream.Write(data, 0, data.Length)
    22.         'End If
    23.  
    24.     End Sub
    I use VB .NET 2010.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Posts
    512

    Re: Send Data on NetworkStream Getting Stuck

    I tried a few changes like SyncLock and CanWrite but it is still having the same issue:
    vb Code:
    1. ''' <summary>
    2.     ''' Sends binary data.
    3.     ''' </summary>
    4.     Public Sub sendData(ByVal data() As Byte, Optional ByVal curStream As NetworkStream = Nothing)
    5.  
    6.         Try
    7.             SyncLock curStream
    8.                 Dim endbytes() As Byte = System.Text.Encoding.ASCII.GetBytes("{682427e7-6a56-4513-87f1-2dec81ddae2d}")
    9.  
    10.                 Dim lenBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(data.Length)
    11.  
    12.                 If curStream.CanWrite Then
    13.                     curStream.Write(lenBytes, 0, lenBytes.Length)  '<----------- It is getting stuck here sometimes
    14.                 End If
    15.                 If curStream.CanWrite Then
    16.                     curStream.Write(endbytes, 0, endbytes.Length)  '<----------- It is getting stuck here sometimes
    17.                 End If
    18.                 If curStream.CanWrite Then
    19.                     curStream.Write(data, 0, data.Length)
    20.                 End If
    21.                 curStream.Flush()
    22.             End SyncLock
    23.         Catch
    24.  
    25.         End Try
    26.  
    27.         'If curStream IsNot Nothing Then
    28.         '    curStream.Write(data, 0, data.Length)
    29.         'End If
    30.  
    31.     End Sub
    I use VB .NET 2010.

  3. #3
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,082

    Re: Send Data on NetworkStream Getting Stuck

    Synclock is unlikely to have any impact on this. If you are calling SendData from multiple threads, then locking may be reasonable, but I would be inclined to lock at a higher level (lock the whole method), and not use the stream as the lock object. Still, I doubt that is at all related to the issue. The issue, one way or another, is probably related to this line from the MSDN documentation:

    The Write method blocks until the requested number of bytes is sent or a SocketException is thrown
    I suspect that if you pass in a length that is not less than or equal to the number of bytes, that would cause the problem you are seeing. The first thing I would do would be to take a look at the byte array and the length. Naturally, I would expect that since the length came from the byte array, it should be the same, but take a look. Also, it may be that there is a bad byte in that array. I'm not quite sure how Write works, but it may stop at a termination character, which could be nearly any value, but I would expect &H0. Therefore, the next test I would try would be to make up a dummy data array as an array of known byte values, and see whether that worked.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Posts
    512

    Re: Send Data on NetworkStream Getting Stuck

    Hmm, I know this normally works, because we use the function/class for some other things in the game. Although, it is newly written so it still has some issues. I am getting the error this time on the 2nd line. Which is very interesting because the 'endbytes' string is a string set earlier in the same function. It has a green arrow and a blue dot on the left side and says: 'This is the next statement to execute when this thread returns from the current function.' Then if I hit play and pause again it comes back to the same spot with the same green arrow, but no blue dot. Also both times the data is the same:

    endbytes = {Length=38}

    The data contains 38 bytes from 45-125 representing this: "{682427e7-6a56-4513-87f1-2dec81ddae2d}"
    I use VB .NET 2010.

  5. #5
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,082

    Re: Send Data on NetworkStream Getting Stuck

    Yeah, that's a bit odd. That appears to be a typical GUID, and the array shouldn't have any termination bytes by any means. The behavior certainly suggests that the Write method is blocking, but I'm not so sure why. How did you get it to get through the first line?
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Posts
    512

    Re: Send Data on NetworkStream Getting Stuck

    The issue seems random. Sometimes it locks on the first one (which is actually just a 3 string with the length of the data (usually around 450)).
    I use VB .NET 2010.

  7. #7
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,082

    Re: Send Data on NetworkStream Getting Stuck

    Truly random would be kind of bad. One thing you might consider is adding something to log the arguments that are going to the method before the call. Something as simple as a pair of lists would do. The point would be to try to find a pattern in the random.

    Aside from that, you might look at WriteTimeout. This would be the "Give Up" option, though, but it would give you another test, potentially: If you added a write timeout, then if the timeout occurred, you could re-write the same thing. If it is ALWAYS blocking for certain arguments (which the failure of the second line suggests is NOT the case), then you might be able to identify the arguments that cause the blockage. If it really is random, then a second try would cause the exact same arguments to go through, in which case you have to look elsewhere.
    My usual boring signature: Nothing

  8. #8
    PowerPoster
    Join Date
    May 2002
    Posts
    25,387

    Re: Send Data on NetworkStream Getting Stuck

    Have you tried sending something else? Something a bit simpler perhaps? could it be an issue with the receiving end of your curStream?

    something else ... have you tried putting everything into one single byte array first, and then sending just that?

    I'm just thinking out loud here... networking and sockets are not my bailywick, so it's possible I'm completely out of bounds here.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2006
    Posts
    512

    Re: Send Data on NetworkStream Getting Stuck

    I am thinking that we may have overloaded the function with calls somehow. Anyways, the WriteTimeout seems very useful as well. I will let you know if we are still stuck. It may take another day or so for testing
    I use VB .NET 2010.

  10. #10
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,082

    Re: Send Data on NetworkStream Getting Stuck

    Network issues are SUCH a joy.
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.