Nate Bross Posted June 22, 2005 Posted June 22, 2005 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: 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: 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted June 22, 2005 Administrators Posted June 22, 2005 In your code you have the lines ' 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)? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted June 23, 2005 Author Posted June 23, 2005 (edited) 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.ClientServer.zip Edited June 23, 2005 by PlausiblyDamp Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted June 23, 2005 Administrators Posted June 23, 2005 (edited) 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 ' 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 ' Return the data received from the client to the console. Dim ClientData As String = Encoding.ASCII.GetString(bytes) Edited June 23, 2005 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted June 23, 2005 Author Posted June 23, 2005 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: Message = "BIN@þ" With: 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted June 23, 2005 Administrators Posted June 23, 2005 Not all ANSI / UNICODE characters have an equivalent ASCII character. ASCII only defines the code up to 127 so anything beyond that isn't going to work anyway. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted June 23, 2005 Author Posted June 23, 2005 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted June 23, 2005 Administrators Posted June 23, 2005 If you need to send binary data then don't convert it to a text based format both the .Read and .Write methods of a stream accept binary data. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted June 23, 2005 Author Posted June 23, 2005 Issue resolved! Resolution: Changed Existing VB code: System.Text.Encoding.ASCII.GetBytes(Message) To the Following VB Code: System.Text.Encoding.UTF8.GetBytes(Message) Thanks for your help on this issue PlausiblyDamp, I really appreciate it. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.