2

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: ToneGen set mute true.

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.

Here is the code I have so far:

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();
        }
    }
}

The output from the code is as follows:

Establishing Connection to 192.168.10.101
??↑?? ??#??'??$

So I think I have two core issues here:

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

Any help would be greatly appreciated.

UPDATE 1

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

After connecting hex1 returns a value of 15

UPDATE 2

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

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

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

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

Majickal
  • 176
  • 2
  • 16
  • 2
    Hint: take a debugger and check `Bytes` array contains the bytes you're expecting. Hint #2: always take a debugger and check that every variable has the expected value at every point in time while your program executes. – zerkms Aug 15 '16 at 01:05
  • @zerkms - So I attached a debugger and discovered the value noted in update 1 in the original post, _if_ the response F stored in `hex1` (hex/int 15) is equal to 0xFF then it appears that I am on the right track when considering the example negotiation procedure in the following [link](https://support.biamp.com/Tesira/Control/Telnet_session_negotiation_in_Tesira). I am not sure if this is actually correct though or if my logic is flawed? – Majickal Aug 15 '16 at 21:22
  • 2
    I believe `s.Receive` returns you the *length* of the received request. So `15` is how many bytes were received. – zerkms Aug 15 '16 at 21:26
  • @zerkms so I have now had a measure of success returning the `Bytes` and have received the following hex: `FF FD 18 FF FD 20 FF FD 23 FF FD 27 FF FD 24` now I need to respond with a similar request being `0xFF 0xFC 0x18 0xFF 0xFD 0x20 0xFF 0xFC 0x23 0xFF 0xFD 0x27 0xFF 0xFC 0x24` what I am not sure about here is the delimiter between hex values, as you can see I used a space in the above, I'll post the new code as update 2 now. – Majickal Aug 16 '16 at 00:40
  • Now you need to check my initial suggestion once again: `"0xFF 0xFC 0x18 0xFF 0xFD 0x20 0xFF 0xFC 0x23 0xFF 0xFD 0x27 0xFF 0xFC 0x24 \n"` it's not how you create an array of bytes. Just check the `Bytes1` and see that it is rubbish. http://stackoverflow.com/a/6150310/251311 - this would be another hint. – zerkms Aug 16 '16 at 00:48

1 Answers1

0

So I think I have two core issues here:

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

1) How to detect the handshake request from the audio device

byte[] buffer = new byte[1024];
int byteCount = s.Receive(buffer);
Console.WriteLine(byteCount + " Bytes Found");
DumpBytes(buffer, byteCount);

DumpBytes(buffer, byteCount); function found on MSDN: link displays the contents of buffer in the console window, this is useful for checking the response from the telnet server once connected.

2) How to send the appropriate response (back to the telnet server).

var handshake1 = new byte[] { 0xFF, 0xFC, 0x18, 0xFF, 0xFC, 0x20, 0xFF, 0xFC, 0x23, 0xFF, 0xFC, 0x27, 0xFF, 0xFC, 0x24 };
s.Send(handshake1);
Majickal
  • 176
  • 2
  • 16