Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27381

Difficulty with binary data over socket connection

$
0
0
I need to send a stream of binary bytes asynchronously from my Raspberry Pi client to the server on my PC and that will occur 1 byte at a time at erratic intervals. What I have read so far indicates that I must convert the data to ascii, transmit it to the server and then after receiving it I must convert it back to binary. What I have so far works somewhat. The problem is that if I repeatedly send bytes 0-99 the first set works fine but additional sets don't don't work. Here is an example of what is printed by the server. Notice that it counts to 99 just fine but then instead of starting back at 0, it prints 09, 19, 29, etc. I don't see why this is happening. I'll post the server code and I can also post the client if needed, but it is in python.
96
97
98
99
09
19
29
39
Code:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Class TCPSrv
    Shared Sub Main()
        ' Must listen on correct port- must be same as port client wants to connect on.
        Dim port As Int32 = 8222
        Dim localAddr As IPAddress = IPAddress.Any
        Dim tcpListener As New TcpListener(localAddr, 8222)
        tcpListener.Start()
        Console.WriteLine("Waiting for connection...")
        'Accept the pending client connection and return a TcpClient initialized for communication.
        Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
        Console.WriteLine("Connection accepted.")
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        ' Read the stream (single byte) into a byte array
        Dim bites(3) As Byte
        Dim h As String
        While True
            ' Get the byte
            If networkStream.DataAvailable Then
                networkStream.Read(bites, 0, 3)
                h = System.Text.Encoding.UTF8.GetString(bites)
                Console.WriteLine(h)
            End If
        End While
        tcpClient.Close()
    End Sub

End Class


Viewing all articles
Browse latest Browse all 27381

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>