LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-30-2009, 06:13 AM   #1
humandynamo
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
netcat and my perl program


I am currently having some struggles with the the processing of the <STDIN> from my executable.

netcat -v -v IPaddress port -e ./myprogram.pl

Within "myprogram.pl" I do the following to get the user input:
$sLine = "";
$sLine = <>;
print = "Received: $sLine\n\r";

I would like to keep "myprogram.pl" the way it is. Although it was originally built as a server it is a module that should operate the same whether being connected to or making the connection.

I am hoping to keep this model because the actual server portion is being run by xinetd which waits for the connection and then runs "myprogram.pl"
 
Old 01-30-2009, 09:19 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You haven't stated what problem you are observing. However, with respect to your statement 'a module that should operate the same whether being connected to or making the connection', it sounds like you are trying use your code as both a server and a client, but none of the code you show functions as a client. More detail is required here.
BTW, please post source code in [CODE] tags to retain clarity.
--- rod.
 
Old 01-30-2009, 11:55 PM   #3
humandynamo
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
I apologize it was a major oversight to leave out the actual problem and I thank you for taking the time to even respond to an incomplete post.

My problem is that I have a program which is supposed to take some input from the user and then respond/manipulate the user input in some way. When it is in server mode it operates as expected. I wait for user input and then I print the user input back to ensure I have received the data correctly. However when I initiate a connection using netcat does not wait for user input; the program instead falls through and I get an error stating that the value $sLine in uninitialized.

The actual code does not function as a server but is executed by xinetd when a tcp connection is made to it on a certain port. I was attempting to do the same thing with netcat only this time I sought to initiate the connection.
 
Old 02-01-2009, 10:30 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, I think I understand what you are doing. To rephrase a bit, your perl program reads standard input and echoes to standard output. When standard input is a terminal/console, the program works as expected, but when the standard input is redirected from the output of netcat, it fails to wait for input.
My version of netcat was built without the '-e' option, so I cannot test directly with it. I tried using the bash commandline to redirect the standard output of netcat into your program, and as expected, this worked. The difference is that in your instance, netcat is the parent of your perl program. I am hypothesizing that this is causing your program to read its standard input before the parent process has been able to receive from the network, and this is causing the read from stdin to return undefined. To test this hypothesis, try this:
Code:
#! /usr/bin/perl -w
use strict;
    my $sLine = "";
    while( ! defined ( $inp ) ){
        $sLine = <>;
    }
    print "Received: $sLine\n";
Let us know how that changes things.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting to program in c or perl? memo007 Programming 12 09-19-2008 08:07 AM
Perl program control MWFlint Programming 4 12-08-2004 12:59 PM
running a program from perl surban99 Linux - Software 1 06-22-2004 02:54 PM
first perl program --need feedback farhanali Programming 5 08-04-2003 08:23 PM
Please check this program(perl) for me! Rex_chaos Programming 2 04-25-2002 06:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration