CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Directory listing


New User

Feb 13, 2007, 5:00 AM

Post #1 of 3 (1824 views)
Directory listing Can't Post

Hi all,

I am new to perl i need this one to be solved

I have a Perl script which runs in server and a script also runs in client now when client enters a (say dirlist) in client script then the server should return the directory listing of the OS presently i am working in Windows. within the while loop we need to insert a conditional statement which yields the directory listing.. can anyone pls help me ...

my server script is

#!/usr/bin/perl -w

#This is a very simple server for tcp/ip connections.

#Only one connection at a time is processed

#It uses port 5555 by default

use Socket; #the library with sockets functions

# Define default values for host and port

$server_port = "5555";

$server_name = "localhost";

# Give the user a chance to change the settings

print "Enter servername and port or return for default (localhost:5555): ";

unless( ($input = <STDIN>) eq "\n" ) {

chomp $input;

($server_name, $server_port) = split /:/, $input; # separate the values separated with :


$ipaddress = inet_aton($server_name);

$server_address = sockaddr_in($server_port, INADDR_ANY);

$protocol = getprotobyname("tcp");

print STDOUT "Perl's server on $ipaddress\n";

print STDOUT "Perl's full address $server_address\n";

print STDOUT "Host: $server_name\tPort: $server_port\n";

socket(SERVER, PF_INET, SOCK_STREAM, $protocol)

|| die ("SOCKET: $!");

print STDOUT "... created socket on $server_name ...\n";

setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1); # so we can restart our server quickly

select(SERVER); # Make SERVER the default handler, i.e. where print prints without explicit handler

$| = 1; #Flush buffers

bind(SERVER, $server_address)

or die "Couldn't bind to port $server_port : $!\n";

print STDOUT "... now bound socket to port $server_port ...\n";

# establish a queue for incoming connections


or die "Couldn't listen on port $server_port : $!\n";

print STDOUT "... $server_name listens on port $server_port ...\n";

# accept and process connections

while (accept(CLIENT, SERVER)) {

select (CLIENT); # Make CLIENT the default handler

$| = 1;

while(1) { #For sendeing and receiving several messages


# do something with CLIENT

$intext = <CLIENT>; # read a line from the client

if($intext =~ m/^(q|Q|(quit)|(Quit)|(QUIT)|\\n)/) { # match for different quit "commands" from client

print STDOUT "client quits\n";

last; # jump out of the inner while loop that is always true


print STDOUT "=>", $intext; # print to server standard output

print CLIENT "Been on Server $counter\n"; # Send a line of text to the client





Feb 13, 2007, 11:10 AM

Post #2 of 3 (1818 views)
Re: [halmstad] Directory listing [In reply to] Can't Post

the directory functions are:


you can use glob to get a directory listing too:


you can view all the functions and lots more on the perldoc website:


Feb 25, 2007, 5:23 PM

Post #3 of 3 (1758 views)
Re: [halmstad] Directory listing [In reply to] Can't Post

Althought I havent read your script but from reading what you want to do wouldnt an easy solution be capturing the output of 'dir' abd relaying it to the client? Also instead of using a collection of if,then statements would it be easer to create a has with the command names as the keys and the currosponding functions as the values.... for example when the server receives a dirlist command it would call the %commands['dirslist'] (Sorry if this isnt the correct syntac... perl newb) sub routine that would relay the output of dir to the client?


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives