Wierd Results with non textual data

Nate Bross

Well-known member
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:
Code:
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:
Code:
Message = "BIN@
 
In your code you have the lines
Code:
 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 cant see any code that refers to a serial port in there.

Quick update:
In your server try using
Code:
 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
Code:
  Return the data received from the client to the console.
Dim ClientData As String = Encoding.ASCII.GetString(bytes)
 
Last edited by a moderator:
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 "
 
Not all ANSI / UNICODE characters have an equivalent ASCII character. ASCII only defines the code up to 127 so anything beyond that isnt going to work anyway.
 
Is there a way I can send binary data? I know ASCII only uses seven bytes, I need to use the eighth byte. I didnt have any trouble sending data via Winsock in vb 6 to the .net server. My issue is converting the string to a byte array.
 
If you need to send binary data then dont convert it to a text based format both the .Read and .Write methods of a stream accept binary data.
 
Issue resolved!

Resolution:

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

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