By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

Simple Telnet commands using C# and tcpclient

P: n/a
I'd like to do the following in C# and prefer using tcpclient rather
than raw sockets...

Connect to a unix box
Login
run date +%H%M%S
retrieve the response.

That's it, nothing more. This shouldn't be too complicated, I
thought... I have yet to find any examples of being able to do this.
Any suggestions?

Thanks!
Greg
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<FrameworkSDK>\Samples\Applications\WinTalk is sample in MSDN, it provides
demo of Socket wrapped class.
No doubt you can do following that way, yet perhaps simpler way does exist.

"Greg Martz" <a-*****@microsoft.com>
??????:MP************************@msnews.microsoft .com...
I'd like to do the following in C# and prefer using tcpclient rather
than raw sockets...

Connect to a unix box
Login
run date +%H%M%S
retrieve the response.

That's it, nothing more. This shouldn't be too complicated, I
thought... I have yet to find any examples of being able to do this.
Any suggestions?

Thanks!
Greg

Nov 15 '05 #2

P: n/a
If you are looking for a decent book that covers Sockets and associated
classes try to pick up a copy of "Professional .NET Network Programming"
from (now defunct) Wrox Press. (ISBN 1-86100-735-3)
DL
"Greg Martz" <a-*****@microsoft.com> wrote in message
news:MP************************@msnews.microsoft.c om...
I'd like to do the following in C# and prefer using tcpclient rather
than raw sockets...

Connect to a unix box
Login
run date +%H%M%S
retrieve the response.

That's it, nothing more. This shouldn't be too complicated, I
thought... I have yet to find any examples of being able to do this.
Any suggestions?

Thanks!
Greg

Nov 15 '05 #3

P: n/a
Dave Loynd writes:
...from (now defunct) Wrox Press.


Do you know if this means that books they had in the pipeline are dead? In
particular I expected to see their "Professional C# Design Patterns Applied"
(or whatever it was going to be called) last month. Is someone else taking
over their properties?

Michael Roper
Nov 15 '05 #4

P: n/a
Greg Martz <a-*****@microsoft.com> wrote in message news:<MP************************@msnews.microsoft. com>...
I'd like to do the following in C# and prefer using tcpclient rather
than raw sockets...

Connect to a unix box
Login
run date +%H%M%S
retrieve the response.

That's it, nothing more. This shouldn't be too complicated, I
thought... I have yet to find any examples of being able to do this.
Any suggestions?

Greg -

I guess it depends on how you define "complicated". Connecting to
the Unix box is easy, trying to follow the Telnet protocol will be
your challenge.

The basics to get you connected to the server are easy:

TcpClient sock = new TcpClient("remotehostIP", 23);
NetworkStream ns = sock.GetStream();
byte[] data = new byte[1024];
int recv = ns.Read(data, 0, data.Length);

However, what you will see returned in the data array will not be
what you expect to see. When a Telnet session is first established,
the host and client send a series of negotiation packets back and
forth to determine what capabilities are supported by each (see RFC
854). The first packet of data received from the host will contain a
byte stream containing the negotiation request. It's your job to
correctly answer the negotiation with an appropriate byte stream
before you will see the login message text from the server.

If you are always connecting to the same Unix box, you might be
able to capture a Telnet session using a sniffer (or the WinPcap
analyzer program on your PC), then duplicate the session in your
connection. Usually there are two or three back-and-forth sessions
before the host sends the login message text. Once you see that you
are mostly home free (although Telnet does allow for the host to
renegotiate during the session, so watch out for that).

Hope this helps point you in the right direction to solve your
problem. Good luck in your network programming.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyT...471433012.html
Nov 15 '05 #5

P: n/a
In article <cc**************************@posting.google.com >,
ri*******@juno.com says...
Greg Martz <a-*****@microsoft.com> wrote in message news:<MP************************@msnews.microsoft. com>...
I'd like to do the following in C# and prefer using tcpclient rather
than raw sockets...

Connect to a unix box
Login
run date +%H%M%S
retrieve the response.

That's it, nothing more. This shouldn't be too complicated, I
thought... I have yet to find any examples of being able to do this.
Any suggestions?

Greg -

I guess it depends on how you define "complicated". Connecting to
the Unix box is easy, trying to follow the Telnet protocol will be
your challenge.

The basics to get you connected to the server are easy:

TcpClient sock = new TcpClient("remotehostIP", 23);
NetworkStream ns = sock.GetStream();
byte[] data = new byte[1024];
int recv = ns.Read(data, 0, data.Length);

However, what you will see returned in the data array will not be
what you expect to see. When a Telnet session is first established,
the host and client send a series of negotiation packets back and
forth to determine what capabilities are supported by each (see RFC
854). The first packet of data received from the host will contain a
byte stream containing the negotiation request. It's your job to
correctly answer the negotiation with an appropriate byte stream
before you will see the login message text from the server.

That's what I was doing all right. And yes, I noticed the data that I
was getting back, just didn't know it was a negotiation routine. I
don't really want to write an entire telnet client just to do this
simple task, so I'll look at trying a different way. Thanks for your
assistance!

Thanks!
Greg
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.