Shehryar Ali,
Thanks for providing your code. It's necessary to consume the preamble before you start sending commands to the device. I have re-written your code for you so that it consumes this preamble.
Also, it's highly recommended to use the "try", "catch" blocks when working with sockets to ensure that Exceptions are handled correctly. I have separated the program into 2 classes, a 'TCPConnection' class to handle the telnet connection and the Program class.
I have included the code below. It is commented throughout for your convenience.
It has been tested with the Smart Videohub 12G using Visual Studio Code.
All the best,
Brendan
Blackmagic Design Developer Support
- Code: Select all
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Client01
{
//Separates the Socket functions into class/object
class TCPConnection
{
//Connect the Socket to use TCP/IP commands - returns the true or false
public bool Connect(string IpAddress, int port)
{
ClientSocket.ReceiveTimeout = 5000;
try
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IpAddress), port);
ClientSocket.Connect(ep);
Console.WriteLine("connected!");
return true;
}
catch (Exception e)
{
Console.WriteLine("Error Connecting to Socket: " + e);
return false;
}
}
//Send any Command to the Device. Returns true or false
public bool send(string messageFromClient)
{
try
{
ClientSocket.Send(Encoding.ASCII.GetBytes(messageFromClient),
0, messageFromClient.Length, SocketFlags.None);
ClientSocket.Send(Encoding.ASCII.GetBytes("\n\n"));
return true;
}
catch (Exception e)
{
Console.WriteLine("Error sending command on Socket:\n" + e);
return false;
}
}
//Receive bytes until a terminating string (such as "\n\n").
//Returns the string or "ERROR"
public string receiveUntil(string terminator)
{
string response = null;
Byte[] buffer = new byte[terminator.Length - 1];
while (true)
{
Array.Clear(buffer, 0, buffer.Length);
try
{
ClientSocket.Receive(buffer);
}
catch (Exception e)
{
Console.WriteLine("Error receiving data from socket:\n" + e);
response = "ERROR";
break;
}
response += Encoding.ASCII.GetString(buffer);
if (response.Contains(terminator))
break;
}
return response.TrimEnd('\n','\0');
}
//New socket Object
public Socket ClientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
}
class Program
{
//Get the Preamble from the Device - Returns the preamble or "ERROR"
static string getPreamble(TCPConnection telnet)
{
string preamble = telnet.receiveUntil("END PRELUDE:\n\n");
return preamble;
}
//Get the response from your command - Returns the response or "ERROR"
static string getResponse(TCPConnection telnet)
{
string response = telnet.receiveUntil("\n\n");
return response;
}
//Main function - Entry Point to program
static void Main(string[] args)
{
//Create a new Telnet Object
var telnet = new TCPConnection();
if (telnet.Connect("192.168.23.125", 9990))
{
//Get and print the preamble which is sent on successful connection
string preamble = getPreamble(telnet);
Console.WriteLine(preamble + '\n');
while (true)
{
//User input to send to the device
Console.WriteLine("Enter the command");
telnet.send(Console.ReadLine());
//Get the ACK or NAK response
string ack = getResponse(telnet);
Console.WriteLine("Server " + ack + '\n');
//If ACK then get the Response from the command
if (ack.Contains("ACK"))
{
string response = getResponse(telnet);
Console.WriteLine(response + '\n');
}
}
}
}
}
}