Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > IP connections

Reply
Thread Tools

IP connections

 
 
Hans Vlems
Guest
Posts: n/a
 
      09-05-2012
I'm asked to write a program that runs on a Windows or Linux host and
must set up a telnet connection to a network device. This network
device understands very basic commands (one or two letters) and
produces output, depending on the command. The proud owner of this hi-
tech device doesn't want to initiate a session from the command prompt
but rather run a program that issues the same command(-sequence) every
day and collects the output of the remote device.
My first idea was to have the C program build a command script and
start that script with the system() function.
Looks simple, but system() runs its script asynchronously, i.e. the
originating program has no control over the script.
Second idea: connect to the telnet port of the hi-tech device in the C
program. I'm a newbie in TCP/IP programming so there are a few
questions:
- is there a library with handy functions that I can use (Windows
based to start with)?
- does a network connection behave like a file on disk that can may be
read from or written to?
- a pointer to a book that explains these issues (preferably with easy
to understand pictures ?
Hans
 
Reply With Quote
 
 
 
 
Hans Vlems
Guest
Posts: n/a
 
      09-05-2012
On 5 sep, 16:27, David Brown <(E-Mail Removed)>
wrote:
> On 05/09/2012 16:18, Hans Vlems wrote:
>
>
>
>
>
> > I'm asked to write a program that runs on a Windows or Linux host and
> > must set up a telnet connection to a network device. This network
> > device understands very basic commands (one or two letters) and
> > produces output, depending on the command. The proud owner of this hi-
> > tech device doesn't want to initiate a session from the command prompt
> > but rather run a program that issues the same command(-sequence) every
> > day and collects the output of the remote device.
> > My first idea was to have the C program build a command script and
> > start that script with the system() function.
> > Looks simple, but system() runs its script asynchronously, i.e. the
> > originating program has no control over the script.
> > Second idea: connect to the telnet port of the hi-tech device in the C
> > program. I'm a newbie in TCP/IP programming so there are a few
> > questions:
> > - is there a library with handy functions that I can use (Windows
> > based to start with)?
> > - does a network connection behave like a file on disk that can may be
> > read from or written to?
> > - a pointer to a book that explains these issues (preferably with easy
> > to understand pictures ?
> > Hans

>
> If you can run on Linux, then just make your telnet script and run it
> with a cron job. *"expect" might be useful here.
>
> If you need more programmability, it is a pointless waste of time to use
> C for something like this. *Use a higher level language that supports
> scripting and has appropriate libraries (as well as plenty of examples
> around the web). *If you already have a favourite, such as perl, python,
> tcl, lua, etc., then use that. *If you are starting from zero, then I'd
> recommend Python with the "telnetlib" library:
>
> <http://docs.python.org/library/telnetlib.html>
>
> Even if you have never heard of Python, you will get the job done faster
> and more reliably with Python than coding in C.- Tekst uit oorspronkelijkbericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


David, I'll look into Python even though I've never seen a line of
Python in my life.
 
Reply With Quote
 
 
 
 
Angel
Guest
Posts: n/a
 
      09-05-2012
On 2012-09-05, Hans Vlems <(E-Mail Removed)> wrote:
> I'm asked to write a program that runs on a Windows or Linux host and
> must set up a telnet connection to a network device. This network
> device understands very basic commands (one or two letters) and
> produces output, depending on the command. The proud owner of this hi-
> tech device doesn't want to initiate a session from the command prompt
> but rather run a program that issues the same command(-sequence) every
> day and collects the output of the remote device.
> My first idea was to have the C program build a command script and
> start that script with the system() function.
> Looks simple, but system() runs its script asynchronously, i.e. the
> originating program has no control over the script.
> Second idea: connect to the telnet port of the hi-tech device in the C
> program. I'm a newbie in TCP/IP programming so there are a few
> questions:
> - is there a library with handy functions that I can use (Windows
> based to start with)?
> - does a network connection behave like a file on disk that can may be
> read from or written to?
> - a pointer to a book that explains these issues (preferably with easy
> to understand pictures ?
> Hans


Network programming is not standardized and vastly different on Windows
and Linux, so writing a program that will compile and run without
modifications on both is going to be very hard. You're going to have to
use tons of #ifdefs and conditional compiling for the non-portable
parts.

Note that I said "compile and run" there. Writing a program in C that
will run on both Windows and Linux is impossible because Windows uses a
not only different libraries, but also a different format for binaries
than Linux (.exe versus elf, respectively) and the two are not compatible.
(Some smartass will now probably mention Wine and/or Cygwin. Yes I know
about them, but they both provide only an emulation at best.)

You should seriously consider doing this in a scripting language like
Perl which will be far more portable. Java may also be a viable
alternative.


As for a networking library, on Linux there are libnet and sdl-net among
others, but I don't know about their availablility on Windows.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-05-2012
Hans Vlems <(E-Mail Removed)> writes:

> I'm asked to write a program that runs on a Windows or Linux host and
> must set up a telnet connection to a network device. This network
> device understands very basic commands (one or two letters) and
> produces output, depending on the command. The proud owner of this hi-
> tech device doesn't want to initiate a session from the command prompt
> but rather run a program that issues the same command(-sequence) every
> day and collects the output of the remote device.
> My first idea was to have the C program build a command script and
> start that script with the system() function.
> Looks simple, but system() runs its script asynchronously, i.e. the
> originating program has no control over the script.
> Second idea: connect to the telnet port of the hi-tech device in the C
> program. I'm a newbie in TCP/IP programming so there are a few
> questions:
> - is there a library with handy functions that I can use (Windows
> based to start with)?
> - does a network connection behave like a file on disk that can may be
> read from or written to?
> - a pointer to a book that explains these issues (preferably with easy
> to understand pictures ?


I'd use a program called "netcat" (abbreviated to nc). There may be a
version for Windows.

I'd switch to expect (as suggested) if some kind of synchronisation
between commands is needed.

Failing that, Python or Perl (Python if you don't know some Perl
already).

--
Ben.
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      09-05-2012
Angel wrote:

> Note that I said "compile and run" there. Writing a program in C that
> will run on both Windows and Linux is impossible because Windows uses a
> not only different libraries, but also a different format for binaries
> than Linux (.exe versus elf, respectively) and the two are not compatible.


[Waaay OT]

FYI, the binary format used in Windows is called PE (Portable Executable)
and is an extension of the Common Object File Format (COFF) introduced in
Unix SysV.

https://en.wikipedia.org/wiki/Portable_Executable

> (Some smartass will now probably mention Wine and/or Cygwin. Yes I know
> about them, but they both provide only an emulation at best.)


Given that people run full-blown Windows games thanks to Wine, I'm not
sure why you would be so dismissive.

https://en.wikipedia.org/wiki/Wine_%28software%29

 
Reply With Quote
 
Angel
Guest
Posts: n/a
 
      09-05-2012
On 2012-09-05, Noob <root@127.0.0.1> wrote:
> Angel wrote:
>
>> Note that I said "compile and run" there. Writing a program in C that
>> will run on both Windows and Linux is impossible because Windows uses a
>> not only different libraries, but also a different format for binaries
>> than Linux (.exe versus elf, respectively) and the two are not compatible.

>
> [Waaay OT]
>
> FYI, the binary format used in Windows is called PE (Portable Executable)
> and is an extension of the Common Object File Format (COFF) introduced in
> Unix SysV.
>
> https://en.wikipedia.org/wiki/Portable_Executable


That's interesting background information and I will admit that I didn't
know that. But it's not very relevant to the fact that such files will
not natively run on Linux. (Although, as you correctly pointed out below,
you can get pretty close.)

>> (Some smartass will now probably mention Wine and/or Cygwin. Yes I know
>> about them, but they both provide only an emulation at best.)

>
> Given that people run full-blown Windows games thanks to Wine, I'm not
> sure why you would be so dismissive.
>
> https://en.wikipedia.org/wiki/Wine_%28software%29


I am well aware of what Wine can do. If I sounded dismissive I
apologize, that was not what I intended. I have a lot of respect for
the people who created Wine, or any form of emulation/virtualization for
that matter, it's very impressive technology. My comment was meant to
preempt certain smart guys, not to dismiss Wine.


Since we're, as you also correctly observed, way off topic, let's leave
it at that.


--
"C provides a programmer with more than enough rope to hang himself.
C++ provides a firing squad, blindfold and last cigarette."
- seen in comp.lang.c
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-05-2012
On Wed, 2012-09-05, Ben Bacarisse wrote:
> Hans Vlems <(E-Mail Removed)> writes:
>
>> I'm asked to write a program that runs on a Windows or Linux host and
>> must set up a telnet connection to a network device. This network
>> device understands very basic commands (one or two letters) and
>> produces output, depending on the command. The proud owner of this hi-
>> tech device doesn't want to initiate a session from the command prompt
>> but rather run a program that issues the same command(-sequence) every
>> day and collects the output of the remote device.
>> My first idea was to have the C program build a command script and
>> start that script with the system() function.
>> Looks simple, but system() runs its script asynchronously, i.e. the
>> originating program has no control over the script.
>> Second idea: connect to the telnet port of the hi-tech device in the C
>> program. I'm a newbie in TCP/IP programming so there are a few
>> questions:

....
>
> I'd use a program called "netcat" (abbreviated to nc). There may be a
> version for Windows.
>
> I'd switch to expect (as suggested) if some kind of synchronisation
> between commands is needed.
>
> Failing that, Python or Perl (Python if you don't know some Perl
> already).


I agree with all of that, but a few more points:

- Netcat doesn't really do the telnet protocol with all its arcane
options which you can /almost/ ignore[1]. (Unless the -t option
I see in my nc manual -- OpenBSD version -- is enough.)

- /is/ it really telnet, anyway? Telnet is so old it predates TCP/IP,
and it hasn't been used much for the last decades. Check that
you haven't been given incorrect information, and it's just a plain text
protocol over TCP. The two are often confused, since you can
talk to such a server using many telnet clients (if you don't have netcat
handy).

- Does it have to run on both Windows and Linux? It seems like one of
those requirements people like to place, even though they really
will use just one of the environments. Portable code is good and
everything, but is he willing to pay double price for that?

/Jorgen

[1] According to my limited understanding. It has been in use for
43 years, after all.

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
ais523
Guest
Posts: n/a
 
      09-05-2012
Jorgen Grahn wrote:
> I agree with all of that, but a few more points:
>
> - Netcat doesn't really do the telnet protocol with all its arcane
> options which you can /almost/ ignore[1]. (Unless the -t option
> I see in my nc manual -- OpenBSD version -- is enough.)

The telnet protocol has lots of options, and gets more every now and
then, but it's designed to fall back to the lowest common denominator;
each side has to talk down to the other side's level. What -t does is
just claim, if requested, that it doesn't support any options.

This can lead to quite a problem with more modern telnet-based programs,
though. If you're trying to access a screen oriented program and tell it
that you can't turn off local echo and can't turn on full duplex (and
-t actually does this!), it's likely to at least raise a few eyebrows,
if not outright tell you that your terminal is unsupported and refuse
to communicate with you.

> - /is/ it really telnet, anyway? Telnet is so old it predates TCP/IP,
> and it hasn't been used much for the last decades. Check that
> you haven't been given incorrect information, and it's just a plain text
> protocol over TCP. The two are often confused, since you can
> talk to such a server using many telnet clients (if you don't have netcat
> handy).


Telnet is still used for some things. (I write C code and I also use
telnet interfaces, in my case to allow public access to certain features
of a program, but the telnet interface isn't written by me in C – I use
the command-line tool for the C program, although I wrote a telnet
implementation in Java a while back that supported at least enough
options for most telnet servers to at least consider talking to it.)

But it does seem quite likely to be TCP rather than telnet specifically.
(Telnet clients and servers will try to negotiate options, but apart
from the options, they'll pass bytes through apart from doubling 0xFF
bytes.)

To the OP: A simple implementation of the telnet protocol is pretty easy
to write in C, by the way. The networking code is a lot harder to
write, and in fact can't be done in any reasonably portable way in C
(and as such is off-topic here); I'd recommend looking into languages
more suitable for the task.

--
ais523
 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      09-05-2012
Hans Vlems wrote:
> I'm asked to write a program that runs on a Windows or Linux host and
> must set up a telnet connection to a network device. This network
> device understands very basic commands (one or two letters) and
> produces output, depending on the command. The proud owner of this hi-
> tech device doesn't want to initiate a session from the command prompt
> but rather run a program that issues the same command(-sequence) every
> day and collects the output of the remote device.
> My first idea was to have the C program build a command script and
> start that script with the system() function.
> Looks simple, but system() runs its script asynchronously, i.e. the
> originating program has no control over the script.
> Second idea: connect to the telnet port of the hi-tech device in the C
> program. I'm a newbie in TCP/IP programming so there are a few
> questions:
> - is there a library with handy functions that I can use (Windows
> based to start with)?
> - does a network connection behave like a file on disk that can may be
> read from or written to?
> - a pointer to a book that explains these issues (preferably with easy
> to understand pictures ?
> Hans
>


it is entirely possible that you can just open a TCP/IP socket against
port 23 and treat it like a file handle.

--
Les Cargill
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      09-06-2012
On 6 sep, 01:15, Les Cargill <(E-Mail Removed)> wrote:
> Hans Vlems wrote:
> > I'm asked to write a program that runs on a Windows or Linux host and
> > must set up a telnet connection to a network device. This network
> > device understands very basic commands (one or two letters) and
> > produces output, depending on the command. The proud owner of this hi-
> > tech device doesn't want to initiate a session from the command prompt
> > but rather run a program that issues the same command(-sequence) every
> > day and collects the output of the remote device.
> > My first idea was to have the C program build a command script and
> > start that script with the system() function.
> > Looks simple, but system() runs its script asynchronously, i.e. the
> > originating program has no control over the script.
> > Second idea: connect to the telnet port of the hi-tech device in the C
> > program. I'm a newbie in TCP/IP programming so there are a few
> > questions:
> > - is there a library with handy functions that I can use (Windows
> > based to start with)?
> > - does a network connection behave like a file on disk that can may be
> > read from or written to?
> > - a pointer to a book that explains these issues (preferably with easy
> > to understand pictures ?
> > Hans

>
> it is entirely possible that you can just open a TCP/IP socket against
> port 23 and treat it like a file handle.
>
> --
> Les Cargill- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


This is what I was looking for: the magic word is "socket". Now Google
is my friend....
I've looked at Python, but at this point there is no time to learn
another programming language.
It is not a commercial project, a friend asked for a favor. Basically
no problem if it is a routine
program; right now the few empty brain cells left are used for
aviation theory
At this point the socket approach seems the most promising way.
Thanks all for responding,
Hans
Hans
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lingering Idle Oracle Connections? Persistent connections? jobs ASP .Net 2 11-16-2007 03:37 AM
Madlax Volume One: Connections Anime DVD Review @ Ascully.co Silverstrand Front Page News 0 07-02-2005 11:33 PM
New access on the block point dumps wireless connections Jerry Paquette Wireless Networking 6 09-15-2004 06:32 PM
Restore network connections unreliable Monte Grant Wireless Networking 1 08-17-2004 08:33 PM
Network connections not automatically restored at startup =?Utf-8?B?cmp3?= Wireless Networking 0 07-31-2004 08:05 PM



Advertisments