Share via


Telnet Client

Question

Friday, March 28, 2008 12:18 PM

Hi

Is it possible to send the multiple command through Telnet?

here is my example:

static void Main(string[] args)
{
string message = "!!!";// first command
try
{
Int32 port = 10001;
TcpClient client = new TcpClient("a844.jec-ctrls.net", port);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
data = new Byte[256];
String responseData = String.Empty;
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}

This is working perfect and it respond the correct message for the given command "!!!". Now what my next requirment is to pass the another command that is "ATDT" and get the response.

Any Ideas?

 

 

 

All replies (19)

Saturday, March 29, 2008 6:12 AM âś…Answered

Is it possible to send the multiple command through Telnet?

 

Yes.

stream.Write(....)

res = stream.Read(...)

stream.Write(...)

res = stream.Read(...)

ad. infinitum. You can also keep calling Write and read independently.

Just include error handling e.g. other end disconnects, network connectivity lost, other end sends something unexpected (including something designed to use your code to bypass security). 


Monday, March 31, 2008 7:32 AM

Hi

Thanks for reply.

When I send the multiple command through the above program, the first command should work in proper manner, but when it goes for second command, it appears to be hang.

have u any idea? or can you modify the above program for me by which i can send the multiple command.

program-

public static void Connect(String server, String message)

{

try

{

Int32 port = 10001;

TcpClient client = new TcpClient(server, port);

Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);

Console.WriteLine("Sent: {0}", message);

data = new Byte[256];

String responseData = String.Empty;

String responseData1 = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);

Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("!!!");

stream.Write(data1, 0, data1.Length);

data1 = new Byte[256];

Int32 bytes1 = stream.Read(data1, 0, data1.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

responseData1 = System.Text.Encoding.ASCII.GetString(data1, 0, bytes);

Console.WriteLine("Received: {0}", responseData);Console.WriteLine("Received: {0}", responseData1);

stream.Close();

client.Close();

}

catch (ArgumentNullException e)

{

Console.WriteLine("ArgumentNullException: {0}", e);

}

catch (SocketException e)

{

Console.WriteLine("SocketException: {0}", e);

}

Console.WriteLine("\n Press Enter to continue...");

Console.Read();

}

 

 


Monday, March 31, 2008 9:57 AM

Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("!!!");

 

 It of course depends on the server, but telnet protocols often require a line ending, like carriage return. If the server requires a line ending, the Read() may well be left hanging since the server just won't respond.


Monday, March 31, 2008 10:06 AM

thanks for reply.

But I don't understand what u want to say,

 Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("!!!");

What should i have to write, instead of the above line.


Monday, March 31, 2008 10:11 AM

Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("!!!\r");

 


Monday, March 31, 2008 10:50 AM

Gr8, It's working now. Thanks.

Now I have a another problem, I have to execute the other command which is the combination of (Ctrl+C+C + string value). when i only pass the string value and press the Ctrl+C in the runtime envoirnment, it terminates the program.

Any idea, how to pass the ctrl+C+C as a command?

 

 

 


Monday, March 31, 2008 2:02 PM

Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + stringValue +"\r");

 


Monday, March 31, 2008 2:40 PM

Thanks for reply,

It is not working for me, can u check where i am making mistake.

Program:

public static void Connect(String server, String message)

{

try

{

Int32 port = 10001;

TcpClient client = new TcpClient(server, port);

Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); NetworkStream stream = client.GetStream();

stream.Write(data, 0, data.Length);

Console.WriteLine("Sent: {0}", message);

data = new Byte[256];

String responseData = String.Empty;

String responseData1 = String.Empty;

String responseData2 = String.Empty;

String responseData3 = String.Empty;

Int32 bytes = stream.Read(data, 0, data.Length);

Byte[] data1 = System.Text.Encoding.ASCII.GetBytes("ATDT\r");

stream.Write(data1, 0, data1.Length);

data1 = new Byte[256];

Int32 bytes1 = stream.Read(data1, 0, data1.Length);

Byte[] data2 = System.Text.Encoding.ASCII.GetBytes("ATCN\r");

stream.Write(data2, 0, data2.Length);

data2 = new Byte[256];

Int32 bytes2 = stream.Read(data2, 0, data2.Length);

Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "abc97\r");

stream.Write(data3, 0, data3.Length);

data3 = new Byte[256];

Int32 bytes3 = stream.Read(data3, 0, data3.Length);

responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

responseData1 = System.Text.Encoding.ASCII.GetString(data1, 0, bytes1);

responseData2 = System.Text.Encoding.ASCII.GetString(data2, 0, bytes2);

responseData3 = System.Text.Encoding.ASCII.GetString(data3, 0, bytes3);

Console.WriteLine("Received: {0}", responseData);

Console.WriteLine("Received: {0}", responseData1);

Console.WriteLine("Received: {0}", responseData2);

 

stream.Close();

client.Close();

}

catch (ArgumentNullException e)

{

Console.WriteLine("ArgumentNullException: {0}", e);

}

catch (SocketException e)

{

Console.WriteLine("SocketException: {0}", e);

}

Console.WriteLine("\n Press Enter to continue...");Console.Read();

}

 

Any Idea?


Monday, March 31, 2008 2:53 PM

It is not working for me, can u check where i am making mistake.

I'm afraid not. I have no idea what kind of server you're trying to talk to via telnet, or what it expects or what you expect to happen - or what happens instead.

Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "abc97\r");

stream.Write(data3, 0, data3.Length);

This should send a sequence of bytes like this: { 3, 67, 97, 98, 99, 57, 55, 13 }, or expressed differently: <Ctrl-C>Cabc97<return>

You might want to verify that this in fact is so, by inspecting the data3 byte array in the debugger.

If this is correct, or what is supposed to happen I have no idea. If this is what you want to send, and what the server expects to receive is impossible for me to say - but it is what should be sent.

For more help: Please provide information as follows:

  • What you're trying to send.
  • What you're exepcting to happen.
  • What happens instead that you say is wrong.

If you are not aware of it, please try using the debugger. Set a break point on the first line of the program by pressing F9. Start the program with F5. When it breaks, you can step a line at a time with F10, and inspect the results and current values of variables etc.


Monday, March 31, 2008 3:11 PM

Thanks.

I need to execute the command (Ctrl-cc + string like abc97), and it should return the result as expected prompt like  '>'

then after i have to pass the command like DD5- to download the 5 minutes data from the machine, or DT- to show the current temperature of the machine, etc.

when I tried to pass the command as like this

Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "abc97\r"); Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "abc97\r");

it is not responding me, or it hang.

Any Idea?

 

 


Monday, March 31, 2008 3:20 PM

Ctrl-cc + string like abc97

Exactly what does this mean? I tried to be precise in the previous response. Just what is the byte sequence, expressed as decimal numbers? (perhaps: 3, 67, 97, 98, 99, 57, 55)

it should return the result as expected prompt like  '>'

What does this mean? That it should respond with exactly one character, '>' ?

Does it?

Another thing to note is that your code as posted does not actually print the result in the data3 buffer.

Did you run with the debugger??


Monday, March 31, 2008 4:13 PM

Savante - we are not connecting to a server, but rather we are connecting to a proprietary remote device.  but it should not matter either way.  The remote device allows telnet as an interface.  For our project what we want to do is access the remote device via tcpip/telnet and send it a series of commands.  Based on what we tell it to do we will receive data back.

here's a more detailed explanation.   

 We have a fairly elaborate application in which we have an automated process that that connects to numerous remote devices via telnet.  through VBscript, we establish connections to the remote device, send a series of commands, as follows:

we have a host location.
we have a basestation,
we have several remote devices connected to the basestation 

 
    1) establish connection to the basestation from the host location, (dns address and port)  THIS WORKS
    2) wake up the (basestation) ("!!!\r)  THIS WORKS
    3) wake up the remote device by using (ctrl-cc) + remote machine name.  THIS FAILS, it appears to go into an infinite loop
    4) send commands to the remote device to receive data (dd5l\r)
    5) receive the data as it comes back (we capture what comes back from the session)
    6) log out of the remote device
    7) end session with basestation (quit)

we have the ability for you to see the problem via a citrix solution.  can we send you an invite?  send us your e-mail 

 

 


Monday, March 31, 2008 4:33 PM

we are not connecting to a server, but rather we are connecting to a proprietary remote device

Yes, I understand. Same, same from the client perspective.

wake up the remote device by using (ctrl-cc) + remote machine name.  THIS FAILS, it appears to go into an infinite loop

This is where we need to know more! Just *how* does it fail? Using the debugger, you should be able to find out more exactly how it fails. Single-step the program and check the results. Also, you first specified <Ctrl-C>C, now you're saying <Ctrl-C>c. These are two different things (upper and lower case 'c' may not be the same for the device).

The use of <Ctrl-C> as a 'wake-up' call also indicates that the device at least historically has a manual terminal/serial interface (not necessarily, I'm just guessing) - and if so, there might be a need for a delay after the <Ctrl-C> to give the device time to react. This is loose speculation from my part.

The problem might also simply be with the communication between the basestation and the remote device. Can you try this using some other mechanism such as a serial port terminal? Are you sure that it's your controlling program that's at fault?

can we send you an invite?  send us your e-mail 

Sorry, no. I only do forum support, no private support, for a variety of reasons - one being risk of legal liability.


Monday, March 31, 2008 4:52 PM

The Ctrl-CC is the combinational key (that means we have to hold down the Ctrl key and press the C twice) then release the Ctrl key and press the adc97, it will give me the command prompt for that particular machine. then after i will pass the further command like DD5 to download the machine data for previous 5 minutes in csv format.

(note: our machine is not case sensitive, so ctrl-cc or ctrl-Cc is the same)

when i debug the line
Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "abc97\r");

I got the following response in array data3 as

data3   dimensions[8]
0           3 
1          67
2          101 
3          100
4          99
5          57 
6          55
7          13 

but when it goes for the below line it hung

Int32 bytes3 = stream.Read(data3, 0, data3.Length);

I don't know why. have u any idea?

 

 


Tuesday, April 1, 2008 12:46 AM

The Ctrl-CC is the combinational key (that means we have to hold down the Ctrl key and press the C twice)

Ok. That's very different. That is <Ctrl-C><Ctrl-C>, which means you might want to try:

Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003\x00003" + "abc97\r");

 

I got the following response in array data3

I don't think so. 3,67,101,100,99,57,55,13 indicates that the code is written as:

Byte[] data3 = System.Text.Encoding.ASCII.GetBytes("\x0003C" + "dec97\r");

 But your real problem is the 'attention sequence' which should be 3, 3, not 3, 67 .


Tuesday, April 1, 2008 2:24 AM

note: our machine is not case sensitive, so ctrl-cc or ctrl-Cc is the same

 

Control-C is always case insensitive, and sends a byte 0x03.

This could easily be messing up the encoding (there is no text codepoint U-0003 in Unicode), I would suggest (as Svante has) (1) focusing on the bytes (add lots and lots of tracing). (2) Sending the byte[] { 3, 3 } sequence separately and confirming you get the prompt.

Any kind of interfacing like this is hard to get right, and very hard to get reliable; you need to have massive error and consistency checking otherwise your client and remote device will get out of sync..


Tuesday, April 1, 2008 12:24 PM

Thanks Again. It is working now.

I have implemented that code in my web application and it is working fine when i executed the program without the user interface that means all the command in a static sequence manner, Now the next goal is to design the web interface by which a user can enter the command in a command text box and press the command button and it should return the output in output text box and then clear the command text box to accept the another command.

Program:

public void Connect()
{
try
{
Int32 port = Convert.ToInt32(txtPort.Text);
client = new TcpClient(txtServer.Text, port);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}

public void passcommand(TcpClient client, NetworkStream stream)
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes(txtcommand.Text);
stream = client.GetStream();
stream.Write(data, 0, data.Length);
data = new Byte[256];
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
txtOutput.Text = responseData;
stream.Close();
client.Close();
txtcommand.Text = "";
}

protected void Button1_Click(object sender, EventArgs e)
{
Connect();
passcommand(client, stream);
}

Problem:
           when i try to pass the command using passcommand function, it works fine for first command but it not execute the second one and throws the exception.

Any Idea?

 


Tuesday, April 1, 2008 1:31 PM

when i try to pass the command using passcommand function, it works fine for first command but it not execute the second one and throws the exception.

Please start a new thread for a new question - and in that new thread, do include just what exception is thrown.


Monday, May 6, 2013 6:39 AM

Hi Svante,

I tried to use the tcpclinet class in asp.net webform and i tried to connect to network device through telnet

The connection is scuess,but i am not able to read or write the data through telnet

While reading i am getting the message ???????????????