Nate Bross
Contributor
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:
Client Code:
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.
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.