Sockets

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

I am sending a byte value of Chr(128) to a Listener and it is coming back as
Chr(63) any ideas? Anything greater than 128 up to 152 is returning a
Char(63). What would cause this to happen?

Sample code is below:

Dim oTcpClient As New System.Net.Sockets.TcpClient
oTcpClient.Connect(DeviceIPAddress, DevicePort)
Dim objNetworkStream As NetworkStream = oTcpClient.GetStream()
Dim sendBytes As [Byte]()
sendBytes = Encoding.ASCII.GetBytes(Chr(intPosition))
objNetworkStream.Write(sendBytes, 0, sendBytes.Length)
 
John said:
I am sending a byte value of Chr(128) to a Listener and it is coming back as
Chr(63) any ideas? Anything greater than 128 up to 152 is returning a
Char(63). What would cause this to happen?

Sample code is below:

Dim oTcpClient As New System.Net.Sockets.TcpClient
oTcpClient.Connect(DeviceIPAddress, DevicePort)
Dim objNetworkStream As NetworkStream = oTcpClient.GetStream()
Dim sendBytes As [Byte]()
sendBytes = Encoding.ASCII.GetBytes(Chr(intPosition))

Stop right there! :)

ASCII is a 7-bit encoding, so it will not handle bytes with values
128-255 properly.

It appears here that you are convering intPosition (an Integer, I am
guessing?) to a Char, and then converting that Char to a Byte (with a
7-bit encoding, which is why it's going wrong). Why not just say

Dim sendBytes as [Byte]() = New Byte() { CByte(intPosition) }

?

And while we're here, do you have some reason for preferring to say

Dim sendBytes as [Byte]()

rather than the more traditional (in VB)

Dim sendBytes() as Byte

?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top