Recieving zero's instead of string through tcp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    Recieving zero's instead of string through tcp

    I work in visual basic,and have 2 forms programs.

    One acts as a client, the other as a server. I send two things from client to server;
    1: a string describing which service of the server is needed. The server enters a sub based on this string.

    2: data needed for the sub the server is in.

    However, apparently the second transmission results in 8000+ zero's being recieved instead of the string i sent..

    Code: Client

    Code:
        'will send the info
        Public Sub sendInfo()
            'check connection
            If Not connected Then
                Throw New Exception("There is no connection to the server.")
            End If
    
           'send message
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("SERVICE_1")
            networkStream.Write(sendBytes, 0, sendBytes.Length)
    
            Dim a As String = "string to be sent for the service_1 sub of the server"
    
            Dim sendBytes2 As [Byte]() = Encoding.ASCII.GetBytes(a)
            networkStream.Write(sendBytes2, 0, sendBytes2.Length)
        End Sub

    Code: server
    Code:
        'will serve a client
        Private Sub serveClient(ByVal client As TcpClient)
            'recieve message
            Dim bytes(client.ReceiveBufferSize) As Byte
            client.GetStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
            Dim request As String = Encoding.ASCII.GetString(bytes)
    
            'check needed service
    
            If matches(request, "SERVICE_1") Then
                service1(client)
            ElseIf matches(request, "SERVICE_2") Then
                service2(client)
            ElseIf matches(request, "SERVICE_3") Then
                service3(client)
            End If
        End Sub
    
    
        Private Sub service1(ByVal client As TcpClient)
    
            Dim bytes(client.ReceiveBufferSize) As Byte
            client.GetStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            Dim s() As String = clientdata.Split(";")
    
            If (s.Length < 3) Then
                MsgBox("string didn't contain ';' zero's recieved")
            End If
    
            dosomething(s(0), s(1), s(2))
    
        End Sub
    so I repeat, the server does recieve the string "SERVICE_1" correctly, but it does NOT recieve the needed string in service1 sub. What am I doing wrong?

    (when I putted breakpoints on all lines, and ran through them, apparently it did recieve it correctly, but it does not do that without breakpoints)

    Thanx
  • Gangreen
    New Member
    • Feb 2008
    • 98

    #2
    *bump*
    someone, please?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I would say that your first read on the server side is probably reading in BOTH messages, leaving nothing for the 2nd read to read.
      If you were doing proper sending and receiving, you would be checking the "actual number of bytes read/written" and validating you have read the correct ammount.

      Comment

      • Gangreen
        New Member
        • Feb 2008
        • 98

        #4
        I've put a breakpoint only on the line right after the server read the string.

        Instead of it being: "SERVICE_1" , it was "SERVICE_1.
        (without the ending parenthesis) What's up with that?

        Do you have an example of a better way of doing this?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Well I would start by using delimenators or come up with some other messaging format.
          With a deliminator you can tell your first read to only read up until the deliminator charater(s). Making sure the data is still there following it.

          Also, you tell the send and receive functions to send/receive the size of their buffer, and not the size of data you actually want, try only sending as many bytes as are in your byte[] and see if that helps.

          Comment

          Working...