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