Hi,

I am very new to Perl Scripting. I used Shell scripting before, but very little knowledge of Perl Scripting.

I am given at task at my work: Here is info:

I need to write a program that will listen to a port which will connect from Telnet. Also, the program will read information from a Infofile. Info file has location, ip address, and port number.

sample infofile:

Newyork 127.0.0.1 2250
Chicago 127.0.0.1 2251
Floriad 192.168.12.22 2250

The program will take two arguments - the path of the infofile and the port number that the program listens on. The commands to the port of the program have the following syntax:

Record priority location filename time

..where location is the location from the infofile, filename is the file that should be recorded and time is the length of the recording (in seconds). priority is an integer 1-200. 200 is hightest. Example,

Record 19 Chicago myfile 25.

So I need to write this program in Perl and convert it .exe program. I will be really appreciate if you can help me. If you can't figure it out all, the partial help will be great too.

Thanks,
john.

Here is the code that so far I worked on it. I still don't how to read from a file and use two arguments in program.

I am also having trouble doing Recording statement.

#!/usr/bin/perl -w 
        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
        $PORT = 9000;

        $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server" unless $server;

	print "SERVER Waiting for client connection on port 9000\n";

         while ($client = $server->accept()) {
          $client->autoflush(1);
	  print $client "Welcome. Sim is ready.\n";
	  print $client "Use 'HELP' for help.\n";
	  print $client "Enter the Command:";           
          while ( <$client>) {
            if    (/quit|exit/i)    	    { exit; }             
            elsif (/"Record 19 Chicago myfile 25"/i )         { print  $client "Recording \n"; } 
            elsif (/help/i) { 
              print $client "Command: Record priority location filename time \n"; } 
	    else
            { print $client "Command is not supported. \n";}
          } continue { 
             print $client "Command? "; 
          } 
          close $client; 
        }

I will be really appreciated if you can help me on this.

Thanks.

#!/usr/bin/perl -w 
        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
        $PORT = 9000;

        $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server" unless $server;

    print "SERVER Waiting for client connection on port 9000\n";

         while ($client = $server->accept()) {
          $client->autoflush(1);
      print $client "Welcome. Sim is ready.\n";
      print $client "Use 'HELP' for help.\n";
      print $client "Enter the Command:";           
          while ( <$client>) {
            if    (/quit|exit/i)            { exit; }                                     
                                                                                         ########################
                                                                                         ## I DONT SEE ANY FILE READING TO||FROM
            elsif (/"Record 19 Chicago myfile 25"/i ) { print  $client "Recording \n"; } ## ARE YOU READING TO FILE ????
                                                                                         ## IT SEEMS THAT YOU ARE INTERACTING ON SCREEN AND ##SAVING  TO FILE YES?
                                                                                         ########################  
            elsif (/help/i) {                           
              print $client "Command: Record priority location filename time \n"; } 
        else
            { print $client "Command is not supported. \n";}
          } continue { 
             print $client "Command? "; 
          } 
          close $client; 
        }

I worked on some more on code. I still need to rearrange some code in there. and Still need to work on "Recording" part. As I understood, I just need to print it on screen whatever User input.
For example, Program should ask user for command in this format:
Record priority location filename time

If user enter: Record 19 Chicago myfile 25.
Program should print on screen. that myfile is recording at Chicago location and priority is 19.

If user enter: Record 19 Chicago myfile 25.
Program should print on screen. that myfile is recording at Chicago location and priority is 19.

If user enter: Record 100 Newyork newfile 45.
Program should print on screen. that newfile is recording at Newyork location and priority is 100.

Program should check correct location entered by user to match with description file. If the location is not in the description file, program should ask user to enter correct location.

Program also should check correct priority range (0 to 200). If user enter morethan 200 (for example, 205), the program should say incorrect priority and ask user again to enter command. I think, I should write a separte function for all of this, but not sure how do I do it?

Here is the code so far I worked on.

#!/usr/bin/perl -w 
        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
#        $PORT = 9000;

	$numArgs = 2;
	chomp($DesFile=@ARGV[0]);
	chomp($PORT=@ARGV[1]);
	if (length($DesFile)==0||length($PORT)==0)
   	{print "You must provide a Description filename and Port number!\n";
	 print "Usage: ptel2 Filename PortNumber\n";
	 exit;
	}
	else
	{
   		if (!-e $DesFile)
      		{print "The Description file does not exist!\n";
		 exit;
		}
   		else
      		{

		print "thanks, you gave me $numArgs command-line arguments:\n";

			foreach $argnum (0 .. $#ARGV) {

   		print "$ARGV[$argnum]\n";}

		}
	}



        $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server" unless $server;

	print "SERVER Waiting for client connection on port $PORT\n";

         while ($client = $server->accept()) {
          $client->autoflush(1);
	  print $client "Welcome. Sim is ready.\n";
#	  open (FILE, 'data.txt');
	open (FILE, $DesFile);
 	while (<FILE>) {
 	chomp;
 	($name, $address, $nport) = split("\t");
 	print "Server: $name\n";
 	print "Ip Address: $address\n";
 	print "Port: $nport\n";
 	print "---------\n";
 	}
 	close (FILE);


	  print $client "Use 'HELP' for help.\n";
	  print $client "Enter the Command:";           
          while ( <$client>) {
            if    (/quit|exit/i)    	    { exit; }             
            elsif (/"Record 19 Chicago myfile 25"/i )         { print  $client "Recording \n"; } 
            elsif (/help/i) { 
              print $client "Command: Record priority location filename time \n"; } 
	    else
            { print $client "Command is not supported. \n";}
          } continue { 
             print $client "Command? "; 
          } 
          close $client; 
        }

Please help me on this. Thanks.

on line 17, please provide the full file path.

if (! -e "/home/foo/$filepath") # That is if the file check is not in the same cwd.
Also re arrange your code as the logic does not flow well.

The recording part. do you intend to record to a plain file or a DB?

Advice ;)

Also i think you must have data to cross check with user inputs. thats is

Record 19 Chicago myfile 25.

It can be a \t delimitered file or a DB.

I dont really get what you want but we will get there.

NO. I just need to display on the screen recording part.

Thanks,
John.

As I mentioned before my program ask user to enter record command. I have hard coded "Record 19 Chicago myfile 25" in my program, but actually, User enter this command.

If user enter: Record 19 Chicago myfile 25.
Program should print on screen. that myfile is recording at Chicago location and priority is 19.

If user enter: Record 100 Newyork newfile 45.
Program should print on screen. that newfile is recording at Newyork location and priority is 100.

Program should check correct location entered by user to match with description file. If the location is not in the description file, program should ask user to enter correct location.

Program also should check correct priority range (0 to 200). If user enter morethan 200 (for example, 205), the program should say incorrect priority and ask user again to enter command. I think, I should write a separte function for all of this, but not sure how do I do it?

Why dont you ask for the details one after the other?

print "Please enter Record number\n"
$record=<>;
check if the number is in range, if not, ( print "please enter record number\n"; again.

2. if all good. you ask for the second and the third.
once you got all, you can print the record or record them where ever you want.

That is if you intend to take input from a CMD line. Try and ask for info one after the other.

;)

Edited 6 Years Ago by richieking: n/a

Yes. That would be easy. But the user enter the whole command as
Record priority location filename time

so, I need to figure it out how to handle it. Thanks for any help.

John if the user enter the whole stuff, its still easy. ;)

you split them into various variables and do the check like as if they entered one after one after the other.

print " fooo bar fooo";
$line=<>;
($fo,$bar,$loo)= split(m/ /,$line);

you got what you need to check.

Edited 6 Years Ago by richieking: n/a

I tried to split them, but it doesn't work.

User enter as input: RECORD 106 NewYork myfile 40

my $line = <>;
($Record,$Priority,$Location,$filename,$time)= split(/ /,$line);

print $client "This is priority $Priority\r\n";
print $client "This is Location $Location\r\n";

but the priority and location value showing blank.. Is anything else need to be done here?

Thanks.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi,

I know now why the priority and location values are not printing.

I added another print statement to see what user input is, but it prints the first line of description file. I passed description file name as an argument of this program.

while ( <$client>) {
           if (/help|-h/i) { 
            print $client "Command: Record priority location filename time \r\n"; } 
	    else
            { 
		$nrecord = <>;
		print $client "this is usr input: $nrecord\r\n";
		($Record,$Priority,$Location,$filename,$time)= split(m/ /,$nrecord);

		print $client "This is priority $Priority\r\n";
		print $client "This is Location $Location\r\n";
}
          } continue { 
            print $client "Command? "; 
         } 
         close $client;

This is what printing:

Welcome. Sim is ready.
Use 'HELP' for help.
Enter the Command:record 23 ny my 34
this is usr input: Newyork 127.0.0.1 2250
This is priority
This is Location
Command?


How the program capture user input and split instead of using file from argument? How can clear it out?

Thanks.

Hi,
Before you can work on a data, the user must enter the data or you must have the data source.

on line 6 if the $nrecord takes the cmd arg. The code must work with the split.

OK run the code again and print $nrecord if it contains any data.

;)

Hi,

The user enter the data.

record 23 ny my 34

but, the $nrecord prints "Newyork 127.0.0.1 2250" which is first line of the description file which I passed in as argument when I start my program.

print $client "Use 'HELP' for help.\r\n";
print $client "Enter the Command:"; 

while ( <$client>) {           
if (/help|-h/i) 
{             print $client "Command: Record priority location filename time \r\n"; } 	    
else            { 		
$nrecord = <>;		
print $client "this is usr input: $nrecord\r\n";($Record,$Priority,$Location,$filename,$time)= split(m/ /,$nrecord); 	
print $client "This is priority $Priority\r\n";		
print $client "This is Location $Location\r\n";}          
} continue 
{             print $client "Command? ";          }          
close $client;

As you see, line 2 ask user to enter data.

This is capture of my screen:
Welcome. Sim is ready.
Use 'HELP' for help.
Enter the Command:record 23 ny my 34
this is usr input: Newyork 127.0.0.1 2250
This is priority
This is Location
Command?


As you see above, I entered "record 23 ny my 34", but it shows under usr input as "
Newyork 127.0.0.1 2250"

Do you know what's is wrong here? Thanks for helping me.

very strange. My works as expected.
chomp $nrecord before split ok?

And make sure split / / is only a space not more.
:)

Yes. I have only a space between //.

I am still having exact same result.
Please see my complet code.

#!/usr/bin/perl -w 

#use strict;
use warnings;


        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
#        $PORT = 9000;

	$numArgs = 2;
	chomp($DesFile=@ARGV[0]);
	chomp($PORT=@ARGV[1]);
	if (length($DesFile)==0||length($PORT)==0)
   	{print "You must provide a Description filename with it's full path and Port number!\n";
	 print "Usage: ptel2 Filename PortNumber\n";
	 exit;
	}
	else
	{
   		if (!-e $DesFile)
      		{print "The Description file does not exist!\n";
		 exit;
		}
   		else
      		{

		print "thanks, you gave me $numArgs command-line arguments:\n";

			foreach $argnum (0 .. $#ARGV) {

   		print "$ARGV[$argnum]\n";}

		}
	}



        $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server" unless $server;

	print "SERVER Waiting for client connection on port $PORT\n";

         while ($client = $server->accept()) {
          $client->autoflush(1);
	  print $client "Welcome. Sim is ready.\r\n";
#	  open (FILE, 'data.txt');
	open (FILE, $DesFile);
 	while (<FILE>) {
 	chomp;
 	($name, $address, $nport) = split("\t");
 	print "Server: $name\n";
 	print "Ip Address: $address\n";
 	print "Port: $nport\n";
 	print "---------\n";
 	}
 	close (FILE);


	  print $client "Use 'HELP' for help.\r\n";
	  print $client "Enter the Command:";     

#print "Enter inputs separated by comma\n";


      
          while ( <$client>) {

           if (/help|-h/i) { 
            print $client "Command: Record priority location filename time \r\n"; } 
	    else
            { 
		
		$nrecord = <>;
		chomp;
		print $client "this is usr input: $nrecord\r\n";
		($Record,$Priority,$Location,$filename,$time)= split(m/ /,$nrecord);

		print $client "This is priority $Priority\r\n";
		print $client "This is Location $Location\r\n";
		}
          } continue { 
            print $client "Command? "; 
         } 
         close $client; 
       }

Also, This what I have in description file:

Newyork 127.0.0.1 2250
Chicago 127.0.0.1 2251
Floriad 192.168.12.22 2250

I have tab beween them.

Please see if you can figure it out. Why it prints "Newyork 127.0.0.1 2250" instead of "record 23 ny my 34" that I entered on command line.

Thanks for your help.

#!/usr/bin/perl
print "enter details\n\n";
		$nrecord = <>;
		chomp;
		print "this is usr input: $nrecord\r\n";
		($Record,$Priority,$Location,$filename,$time)= split(m/ /,$nrecord);

		print  "This is priority $Priority\r\n";
		print "This is Location $Location\r\n";

i just put on try this piece of your code and it works. check it out.
run this via a cmd prompt. ;)

Yes. I tried this. and it works. but for some reason, my code doesn't work on telnet window.

I have no clue why it print the first of the description file. If you can try to run my code, you will see what I am saying.


This is how I am running"

"perl ptel2.pl data2.txt 9000"

starts the program.

now on command window, I type: "telnet localhost 9000"
this connects to the my program.

Please help if you can find this out. I will be really appreciated.

Thanks.

you have a bug in your code.

on line 12,13.

That is not how to read an array.
when reading scalar from array its like this

$foo= $foo[0]

## if its a hash
$foo= $foo{bar};

Allow strict. ok?

what ide are you using?
;)

I don't understand your question:
"What ide are you using?"

I am running on windows platform. Is that what you mean?

I am trying to narrow down the problem.
I have now simple program that listen to port and connect from telnet. I am not using any argument or read from the file.

at telnet session, I enter the command, but it doesn't do anything. Here is the code.

#!/usr/bin/perl -w 

#use strict;
use warnings;


        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
	my $PORT = 9000;




        my $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server" unless $server;

	print "SERVER Waiting for client connection on port $PORT\n";

         while (my $client = $server->accept()) {
          $client->autoflush(1);
	  print $client "Welcome. Sim is ready.\r\n";


print $client "enter details\r\n\n";		
my $nrecord = <>;		
chomp;		
print $client "this is usr input: $nrecord\r\n";		
(my $Record,my $Priority, my $Location,my $filename,my $time)= split(m/ /,$nrecord); 		
print  $client "This is priority $Priority\r\n";		
print $client "This is Location $Location\r\n";

}

If I use strict, my program doesn't work. so, I commented it out.
Notice my print statement has $client.
For example: print $client "this is usr input: $nrecord\r\n";

if i take out $client word out from my print statement. it prints correct value on server window, but not on client (telnet) window.

so, is there may be another way to capture user input on client (Telnet) window??


Thanks.

Try this ,

I have fix the errors .

#!/usr/bin/perl -w 

use strict;
use warnings;

sub nap {
    sleep 1;
    }

        use IO::Socket; 
        use Net::hostent;              # for OO version of gethostbyaddr 
    my $PORT = 9000;




        my $server = IO::Socket::INET->new( Proto     => 'tcp',
                                         LocalPort => $PORT,
                                         Listen    => 5,
                                         Reuse     => 1) or die "can't setup server";

    print "SERVER Waiting for client connection on port $PORT\n";

         while (my $client = $server->accept()) {
          $client->autoflush(1);
      print $client "Welcome. Sim is ready.\r\n";
         &nap;##Waiting    

print $client, "enter details\r\n\n";    
&nap;    ##waiting    
my $nrecord = <>;        
chomp;    
&nap;##Waiting    
        
(my $Record,my $Priority, my $Location,my $filename,my $time)= split(m/ /,$nrecord); 
print $client ,"this is usr input: $nrecord\r\n";
    
&nap;##Waiting        
print  $client, "This is priority $Priority\r\n";    
&nap;##waiting    
print $client ,"This is Location $Location\r\n";

}

hope it helps ;)

Edited 6 Years Ago by richieking: n/a

i meant the tool you are using to write your perl code. Active perl for windows.??

I just dont remember the last time i prog. on windows....

thats all good ;)

Yes. I am using ActivePerl.

I am still having issue. I used your code, and it gives me error message saying

Global symbol "$server" requires explicit package name at ptel3.pl line 18. Execution of ptel13.pl aborted due to compilation errors.

Line 18 is "Reuse => 1) or die "can't setup server" unless $server;"

If I comment out Use Strict line, it runs the program, but same result. It doesn't print anything on display.

Not sure what's wrong here..

Did you run this in CMD prompt?

The line 18 got nothing to do with syntax. Check with you module and how you initialize the socket.

I dont have IO Socket , only IO Socket SSl.

just a litle busy with thing but i wanna help you.

The problem is check your socket defination. The rest of the code must work. thats 100%

Edited 6 Years Ago by richieking: n/a

yes. I am running in command line.

I wrote socket connection code using online. But if you know any better way to write, I am really happy to use it.

I hope, we can figure it out. Thanks for all your help.

I tried all combination, but just same thing.

It doesn't print anything on client (telnet window). After I entered command, it just blank after that; nothing happen.

This question has already been answered. Start a new discussion instead.