Sending Command with CR to TCP server in C#

public void ClientSend(string msg)
{
    stream = client.GetStream(); //Gets The Stream of The Connection
    byte[] data; // creates a new byte without mentioning the size of it cuz its a byte used for sending
    data = Encoding.Default.GetBytes(msg); // put the msg in the byte ( it automaticly uses the size of the msg )
    int length = data.Length; // Gets the length of the byte data
    byte[] datalength = new byte[4]; // Creates a new byte with length of 4
    datalength = BitConverter.GetBytes(length); //put the length in a byte to send it
    stream.Write(datalength, 0, 4); // sends the data's length
    stream.Write(data, 0, data.Length); //Sends the real data
}

I am trying to send commands to Videojet Printer TTO which is TCP server in my case when I send GST<cr> through hercules (TCP Client) it send command properly to the printer as GST where as when I send textbox.text = "GST" + "\r" from my C# TCP Client utility Printer doesn't responds. It is observed in Hercules that when I send textbox.text = "GST" + "\r" in Hercules TCP server it recieves as GST\r not as GST that means CR is passing with the String it is going as a string.

richtextbox.AppendText("\r\n" + now.ToString() + " Sent : \r\n" + textbox.Text + "\r");
Jon Skeet
people
quotationmark

Basically it sounds like you need to change how you're handling the text from the RichTextBox. If you want to use \r as a line-break, use:

string text = string.Join("\r", richtextbox.Lines);
ClientSend(text);

As mentioned in comments, you almost certainly don't want to use Encoding.Default... you should find out what encoding the server is expecting, and use that. If you can specify that yourself, I'd use UTF-8:

// Note: there's no benefit in declaring the variable in one statement
// and then initializing it in the next.
byte[] data = Encoding.UTF8.GetBytes(message);

people

See more on this question at Stackoverflow