Perform a Telnet handshake
<p>I am writing a (hopefully simple) console app to send commands to an audio device running telnet. These commands allow me to alter the state of DSP components contained within it, for example: <code>ToneGen set mute true</code>.</p>

<p>Where I am having trouble is the telnet handshake, I understand that there are a number of commands sent from the Telnet Server to the client and that the client needs to respond to these in order to successfully negotiate the start of the session. I just don't know to send the correct commands. Below is my some what unsuccessful attempt.</p>

<p>Here is the code I have so far:</p>

<pre><code>using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace telnetTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress address = IPAddress.Parse("192.168.10.101");
            int port = 23;
            IPEndPoint endpoint = new IPEndPoint(address, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Console.WriteLine("Establishing Connection to {0}", address);

            s.Connect(endpoint);


            byte[] Bytes = Encoding.ASCII.GetBytes("0xFC\0xFC\0XFC");
            s.Send(Bytes);

            byte[] buffer = new byte[50];
            s.Receive(buffer);
            Console.WriteLine(Encoding.ASCII.GetString(buffer));
            Console.ReadKey();
        }
    }
}
</code></pre>

<p>The output from the code is as follows:</p>

<pre><code>Establishing Connection to 192.168.10.101
??↑?? ??#??'??$
</code></pre>

<p>So I think I have two core issues here:</p>

<p>1) How to detect the handshake request from the audio device
2) How to send the appropriate response.</p>

<p>Any help would be greatly appreciated.</p>

<p><strong>UPDATE 1</strong></p>

<pre><code>byte[] buffer = new byte[1024];
int vari = s.Receive(buffer);
string hex1 = vari.ToString("X");
Console.WriteLine(hex1);
</code></pre>

<p>After connecting hex1 returns a value of 15</p>

<p><strong>UPDATE 2</strong></p>

<pre><code>Console.WriteLine("Sending Bytes");
byte[] Bytes1 = Encoding.ASCII.GetBytes("0xFF 0xFC 0x18 0xFF 0xFD 0x20 0xFF 0xFC 0x23 0xFF 0xFD 0x27 0xFF 0xFC 0x24 \n");
s.Send(Bytes1);
</code></pre>

<p>After the above code I am looking for the next response from the server by using the the following code, </p>

<pre><code>byte[] buffer2 = new byte[1024];
int byteCount2 = s.Receive(buffer2);
Console.WriteLine(byteCount2 + " Bytes Found");
</code></pre>

<p>All I am seeing in the console is the Phrase "Sending Bytes" so it appears that <code>Bytes1</code> is not being sent and there are no further response bytes to read.</p>