Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Administrators
Posted

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)?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (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 by PlausiblyDamp

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...