C# Telnet Library
Best C# Telnet Lib I've found is called Minimalistic Telnet. Very easy to understand, use and modify. It works great for the Cisco routers I need to configure.
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
Simple Telnet console that listens and accepts connections
Use the TCPListener class. The example echoes the string sent to it from the client provided in the second set of code so it sends the response Howdy. You can test this when you run the client code below, which calls Socket.Receive to get the string back from the server:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcplistener
{
class Program
{
static void Main(string[] args)
{
TcpListener server = null;
try
{
Int32 port = 9090;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Hello!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace tcpconnect
{
class Program
{
static void Main(string[] args)
{
Connect1("127.0.0.1", 9090);
Console.Read();
}
// Synchronous connect using IPAddress to resolve the
// host name.
public static void Connect1(string host, int port)
{
IPAddress[] IPs = Dns.GetHostAddresses(host);
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
Console.WriteLine("Establishing Connection to {0}",
host);
s.Connect(IPs[0], port);
byte[] howdyBytes = Encoding.ASCII.GetBytes("Howdy");
s.Send(howdyBytes);
byte[] buffer = new byte[50];
s.Receive(buffer);
Console.Write(Encoding.ASCII.GetString(buffer));
Console.WriteLine("Connection established");
}
}
}
How can I open a telnet connection and run a few commands in C#
C# 2.0 and Telnet - Not As Painful As It Sounds
http://geekswithblogs.net/bigpapa/archive/2007/10/08/C-2.0-and-Telnet---Not-As-Painful-As-It.aspx
Or this alternative link.
If you're going to use the System.Net.Sockets class, here's what you do:
- Create an IPEndpoint, which points to the specified server and port.
You can query DNS.GetHostEntry to change a computer name to an
IPHostEntry object. - Create a socket object with the following
parameters: AddressFamily.InterNetwork (IP version 4),
SocketType.Stream (rides on InterNetwork and Tcp parameters),
ProtocolType.Tcp (reliable, two-way connection) - Open the socket like
this: socket.Connect(endpoint); //yup, it's that simple Send your
data using socket.Send(... wait, I forgot something. You have to
encode the data first so it can fly across them wires. - Use
Encoding.ASCII.GetBytes to convert the nice message you have for the
server into bytes. Then use socket.Send to send those bytes on their
way. - Listen for a response (one byte at a time, or into a byte array)
using socket.Receive Don't forget to clean up by calling
socket.Close()
You can [also] use a System.Net.Sockets.TcpClient object instead of a
socket object, which already has the socket parameters configured to
use ProtocolType.Tcp. So let's walk through that option:
- Create a new TcpClient object, which takes a server name and a port (no IPEndPoint necessary, nice).
- Pull a NetworkStream out of the TcpClient by calling GetStream()
- Convert your message into bytes using Encoding.ASCII.GetBytes(string)
Now you can send and receive data using the stream.Write and stream.Read methods, respectively. The stream.Read method returns the number of bytes written to your receiving array, by the way. - Put the data back into human-readable format using Encoding.ASCII.GetString(byte array).
- Clean up your mess before the network admins get mad by calling stream.Close() and client.Close().
Related Topics
What Is the Use of Observablecollection in .Net
How Do Arrays in C# Partially Implement Ilist<T>
Formatting a Float to 2 Decimal Places
What Is the Purpose of a Marker Interface
Find Size of Object Instance in Bytes in C#
Does Disposing Streamreader Close the Stream
Can a Byte[] Array Be Written to a File in C#
Most Efficient Way to Test Equality of Lambda Expressions
Does the Use of Async/Await Create a New Thread
Method Cannot Be Translated into a Store Expression
Dependency Injection Unity - Conditional Resolving
Best /Fastest Way to Read an Excel Sheet into a Datatable
Finding All Positions of Substring in a Larger String in C#
Visual Studio 2010 Suddenly Can't See Namespace