hi guys ,im new to perl programming and when i programmed tcp client-server coding in perl i encountered some problem saying :
Can't locate socket.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i386-...)
i tried this in linux-centos and version of perl im using is: v5.8.5

my program was like this:
tcp-server:

#!/bin/perl -w
use strict;
use socket;qw(INADDR_ANY AF_INET SOCK_STREAM sockaddr_in);

my $proto=getprotobyname('tcp');
socket(SOMAXCONN,SOCK,AF_INET,SOCK_STREAM,$proto)or die "socket:$!";

my $port=getservbyname('daytime','tcp');
my $paddr=sockaddr_in($port,INADDR_ANY);

bind(SOCK,$paddr)or die "bind:$!";

listen(SOCK,SOMAXCONN)or die "listen:$!";

while(1)
{
	if(accept(CLIENT,SOCK))
	{
	    print CLIENT scalar localtime,"\n";
	    close CLIENT;
	}
}


tcp-client:

#!/bin/perl -w
use strict;
use socket; qw(AF_INET SOCKSTREAM inet_aton sockaddr_in);

$proto=getprotobyname('tcp');

socket(SOCK,AF_INET,SOCK_STREAM,$proto)or die "socket:$!";
$addr=inet_aton('localhost');
$port=getservbyname('daytime','tcp');

$paddr=sockaddr_in($port,$addr);

connect(SOCK,$paddr)or die "connect:$!";
print <SOCK>;

close(SOCK)||die "close:$!";

when i ran server program ,i encountered that error.any help appreciated....

Recommended Answers

All 5 Replies

Capitalize socket in your third line. use Socket, not use socket. Also, remove the qw line following it.

thanx comatose,my program is working now ,there was also error of not specifying scope in the client program and i would like to know:
-> what exactly 'qw' is and is it necessary to include it?
->significance of scope 'my' in a program

qw (quote word) is used to generate a list of words. In the case above, where you use it as parameters to use module (which still wouldn't have worked, since you had a semicolon between use socket and qw). Basically, it allows you to specify a list with simple syntax. If I want to assign an array with values, I can do it like this @names = ('karthik.c', 'kevinadc', 'comatose'); which is ok I guess, but the syntax is a little ugly. I could use qw to simplify it like @names = qw(karthik.c kevinadc comatose); and that does the same thing. I guess simply said, qw is a quick way to specify a lot of little single-quoted words.

My my my. Ok, let's talk about scope. Scope is a particular area inside or between opening and closing braces. A variable in a given scope, is only accessible, inside those braces. Furthermore, the variable gets destroyed when the closing brace is reached. Let's look at an example:

#!/usr/bin/perl

my $name = "comatose";
{
     my $name = "karthik.c";
}
print "$name\n";

You would probably think that the last line should print karthik.c to the screen with a newline. No. Since "my" was used inside the braces $name is a different $name than the one outside of the braces. So the one that gets assigned karthik.c, gets destroyed when it reaches the }, and now the outer $name is the only one that exists. If you remove the my from $name inside of the braces, it will work like you think:

#!/usr/bin/perl

my $name = "comatose";
{
	$name = "karthik.c";
}

print "$name\n";

This prints karthik.c, not comatose as you would think. This is because we removed "my", and it is no longer local to the scope. This becomes really important when you start dealing with if's and whiles. Do you expect this to work?:

#!/usr/bin/perl

print "Enter Your Title: "; $title = <STDIN>;
print "Enter Your Name: ";  $name = <STDIN>;

chomp($title);
chomp($name);

if ($title eq "Dr.") {
    my $message = "Hello Doctor $name!!!!\n";
} elsif ($title eq "Mr.")  {
    my $message = "Mister $name, Hello!\n";
} elsif ($title eq "Mrs.") {
    my $message = "Uh Oh, Married Chick!\n";
}

print "$message";

Code looks good... should work... but uh oh. my my my. $message is declared local with my inside of each elsif or if block (between each { and }). So it shows no output.

thanx comatose,your explanation was satisfactory and i understood what qw and my is ...

hi again ,suppose if i want to connect to another m/c(server) in LAN from my m/c (client)and print the contents of a file in that(server)m/c by specifying the path of that file can i do so using tcp ??
can any one help me in writting code for reading file and printing it after getting connected to server and also i want to know how to specify clients and server's ip-address for connecting i.e do i need to give while compiling the client/server program as in C or do i need to include it in the program itself (if so where should i include that)??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.