IP connections

Discussion in 'C Programming' started by Hans Vlems, Sep 5, 2012.

  1. Hans Vlems

    Hans Vlems Guest

    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
    Hans Vlems, Sep 5, 2012
    #1
    1. Advertising

  2. Hans Vlems

    Hans Vlems Guest

    On 5 sep, 16:27, David Brown <>
    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.
    Hans Vlems, Sep 5, 2012
    #2
    1. Advertising

  3. Hans Vlems

    Angel Guest

    On 2012-09-05, 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


    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
    Angel, Sep 5, 2012
    #3
  4. Hans Vlems <> 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.
    Ben Bacarisse, Sep 5, 2012
    #4
  5. Hans Vlems

    Noob Guest

    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_(software)
    Noob, Sep 5, 2012
    #5
  6. Hans Vlems

    Angel Guest

    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_(software)


    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
    Angel, Sep 5, 2012
    #6
  7. Hans Vlems

    Jorgen Grahn Guest

    On Wed, 2012-09-05, Ben Bacarisse wrote:
    > Hans Vlems <> 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 .
    Jorgen Grahn, Sep 5, 2012
    #7
  8. Hans Vlems

    ais523 Guest

    [OT] Re: IP connections

    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
    ais523, Sep 5, 2012
    #8
  9. Hans Vlems

    Les Cargill Guest

    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
    Les Cargill, Sep 6, 2012
    #9
  10. Hans Vlems

    Hans Vlems Guest

    On 6 sep, 01:15, Les Cargill <> 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
    Hans Vlems, Sep 6, 2012
    #10
  11. Hans Vlems

    Hans Vlems Guest

    On 6 sep, 02:42, Don't make my brown eyes China Blue
    <> wrote:
    > In article <..com>,
    >  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

    >
    > This sounds like a job for expect, which runs on multiple platforms. You can
    > also do this in plain Tcl and other scripting languages which offer a uniform
    > network interface for a variety of platforms beyond unices and Windows.
    >
    > For Tcl and expect help you can try comp.lang.tcl.
    >
    > --
    > My name Indigo Montoya.  \\        Annoying Usenet one post at a time.
    > You flamed my father.     \'         At least I can stay in character.
    > Prepare to be spanked.   //               When you look into the void,
    > Stop posting that!      `/  the void looks into you, and fulfillsyou.


    Given what others wrote about portability of a program I decided to
    stick to
    Windows and try the socket approach. If it can't be done easily in C
    then the
    person who asked for help is just as qualified to learn Python or Tcl
    as I am.
    And has plenty more time too ;-)
    Hans
    Hans Vlems, Sep 6, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Smallridge

    Board Level Bidirectional Connections

    Brad Smallridge, Sep 22, 2005, in forum: VHDL
    Replies:
    0
    Views:
    482
    Brad Smallridge
    Sep 22, 2005
  2. Eli Sidwell
    Replies:
    7
    Views:
    2,692
    Gavin Williams
    Jun 24, 2004
  3. Ricardo Matos

    Re: solve IIS limit to 10 connections problem

    Ricardo Matos, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,214
    Ricardo Matos
    Jul 18, 2003
  4. Raghu Rudra

    Maximum connections to http server

    Raghu Rudra, Jul 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    10,543
    Darren Mombourquette
    Jul 28, 2003
  5. jobs
    Replies:
    2
    Views:
    887
Loading...

Share This Page