Wierd Results with non textual data

Nate Bross

Contributor
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
Hi Guys:

I have an interesting delima. I am trying to send data over a network, the data I am sending contains UNICODE values, but the recieving end needs to get the ASCII value of each byte.

I have a server and client. The client sends data to the server and when it gets there it comes out as "?" question marks.

Server Recieval Code:
Visual Basic:
Dim networkStream As NetworkStream = objTCPClient.GetStream()

            ' Read the stream into a byte array
            Dim bytes(objTCPClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(objTCPClient.ReceiveBufferSize))

            ' Return the data received from the client to the console.
            Dim ClientData As String = Encoding.ASCII.GetString(bytes)
            'Call OwningForm.DebugText(("Client " & sKey & "> " & ClientData))
            Call OwningForm.ProcessIncomingTCP(ClientData, sKey)


            Dim responseString As String = "Recieved."
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            'Call OwningForm.DebugText(("Server Sent> " & responseString))

Client Code:
Visual Basic:
Message = "BIN@þ"

        Try
            ' Create a TcpClient.
            ' Note, for this client to work you need to have a TcpServer 
            ' connected to the same address as specified by the server, port
            ' combination.
            Dim port As Int32 = 222
            Dim client As New TcpClient("192.168.1.111", port)

            ' Translate the passed message into ASCII and store it as a Byte array.
            Dim data() As Byte = System.Text.Encoding.Unicode.GetBytes(Message)

            ' Get a client stream for reading and writing.
            '  Stream stream = client.GetStream();
            Dim stream As NetworkStream = client.GetStream()

            ' Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length)

            Console.WriteLine("Sent: {0}", Message)

            ' Receive the TcpServer.response.
            ' Buffer to store the response bytes.
            data = New [Byte](256) {}

            ' String to store the response ASCII representation.
            Dim responseData As [String] = [String].Empty


            ' Do Until False
            ' Read the first batch of the TcpServer response bytes.
            Dim bytes As Int32 = stream.Read(data, 0, data.Length)
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            Console.WriteLine("Received: {0}", responseData)
            Application.DoEvents() 'let windows think
            'Loop

            ' Close everything.
            client.Close()
        Catch ex As ArgumentNullException
            Console.WriteLine("ArgumentNullException: {0}", ex)
        Catch ex As SocketException
            Console.WriteLine("SocketException: {0}", ex)
        End Try

        Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
        Console.Read()

As you can see, the right three characters is the important data; however, when I recieve it it comes over as "?" question marks.

The actual ASCII values of the three bytes are 254, 18, and 28 respectively.

Any and all advice is appreciated.
 
In your code you have the lines
Visual Basic:
' Translate the passed message into ASCII and store it as a Byte array.
Dim data() As Byte = System.Text.Encoding.Unicode.GetBytes(Message)
did you mean to use Unicode there?

Would it be posible to attach the code for the client and server its (or a simple stripped down version of them)?
 
Yeah, I uploaded the code for both client and server. The basic idea is the 'server' is going to recieve data from network clients, and spit it out to a third party device through the serial port. Thanks for your help.
 

Attachments

Last edited by a moderator:
Will have a better look at it later today - but as a quick one is the mscomm control required for the server as I can't see any code that refers to a serial port in there.

Quick update:
In your server try using
Visual Basic:
' Return the data received from the client to the console.
Dim ClientData As String
ClientData = Encoding.ASCII.GetString(Encoding.ASCII.Convert(Encoding.Unicode, Encoding.ASCII, bytes))
in place of your existing
Visual Basic:
 ' Return the data received from the client to the console.
Dim ClientData As String = Encoding.ASCII.GetString(bytes)
 
Last edited:
Okay that worked except the first byte chr(254) is showing up as question mark "?" on the server side. When properly displayed in a text box it should be displayed as "þ".

In the client replace the line:
Visual Basic:
Message = "BIN@þ"
With:
Visual Basic:
Message = "BIN@" & Chr(254) & Chr(18) & Chr(28)
That string works if run directly from the server to the rs232. Like I said, the chr(254) displays as a chr(63) "?" after being sent.
 
Is there a way I can send binary data? I know ASCII only uses seven bytes, I need to use the eighth byte. I didn't have any trouble sending data via Winsock in vb 6 to the .net server. My issue is converting the string to a byte array.
 
Issue resolved!

Resolution:

Changed Existing VB code:
Visual Basic:
System.Text.Encoding.ASCII.GetBytes(Message)
To the Following VB Code:
Visual Basic:
System.Text.Encoding.UTF8.GetBytes(Message)

Thanks for your help on this issue PlausiblyDamp, I really appreciate it.
 
Back
Top