LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2012, 02:51 AM   #1
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Rep: Reputation: 20
Perl


hi everyone,i write a simple script with chatbot::eliza module and it reads and write with socket,the problem is instead of delivering the chatbot to the client,it runs on the server side,it seems the object accept is not working as i get can't accept,it used to work with perl version 5.12,but not with 5.14.2 [after i update it]:
any help would be appreciated.

Quote:
[majid@localhost Perl]$ perl chatbot2.pl
Server Ready .........
Connection Accepted
Eliza: Hello, I am a computer program.
you: quit
Eliza: Goodbye. It was nice talking to you.
Cant Accept
Quote:
[majid@localhost Perl]$ telnet 127.0.0.1 2009
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
and the code:
Quote:
use strict;
use IO::Socket;
use Chatbot::Eliza;
use POSIX 'WNOHANG';

$SIG{CHLD}=\&sig;
sub sig
{
while(waitpid(-1,WNOHANG)>0) {}
};

my $sver=IO::Socket::INET->new(LocalPort=>'2009',Listen=>20,Proto=>'tcp',Reuse=>1) or die 'Cant create Socket';
print "Server Ready .........\n";

my $clnt;
while(1)
{
$clnt=$sver->accept or die 'Cant Accept'."\n";
print 'Connection Accepetd'."\n";
defined (my $child=fork()) or die 'cant fork';
if ($child==0)
{
$sver->close;
interact($clnt);
exit 0; #when $child has done with sub simply exit
}
$clnt->close; #if its not child it is parent,we just close our copy of the connected socket and go back to the top of the loop to accept() another
#connection. While we are waiting for a new connection, the child is taking care of the old one.
}

sub interact #note:we read and write through the connected socket
{
my $s=shift;
my $bot=Chatbot::Eliza->new;
$bot->command_interface();
STDIN->fdopen($s,"<") or die 'stdin';
STDOUT->fdopen($s,">") or die 'stdout';
STDERR->fdopen($s,">") or die 'stderr';
$|=1; # $| the same is $s->autoflush(1);
}
 
Old 05-03-2012, 03:14 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I'm a bit confused about it.
In your quote you wrote:
Connection accepted. It means it works. And also you can see the answer of Eliza.
The message Cant Accept means the socket has been closed and therefore $sver->accept fails.

You told it worked with perl 5.12, but did not work with 5.14.2. I assume it is somehow related to the Reuse=>1. Reuse is deprecated in 5.14.1 (use ReuseAddr instead is written), maybe this is the problem).
 
Old 05-03-2012, 03:26 AM   #3
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
thanks for the answer,the right output is for the 2 quotes to be exchanged i mean,

when i run
Quote:
perl chatbot2.pl
.it should return
Quote:
Connection Accepted
Connection Accepted
for each Connection Accepted , there is a client

and when i run
Quote:
telnet 127.0.0.1 2009
, it should Deliver Eliza to the client,:
Quote:
[majid@localhost Perl]$ telnet 127.0.0.1 2009
Connection Accepted
Eliza: Hello, I am a computer program.
you: quit
Eliza: Goodbye. It was nice talking to you.
ReuseAddr=>1 doesn't work.
i don't know why but die 'cant accept', will show cant accept like its not working properly.
and also i try to install google-chrome and it tried to install and update some perl modules,can it be because of them?

Last edited by z99; 05-03-2012 at 03:29 AM.
 
Old 05-03-2012, 03:31 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I cannot find 5.14.2 at http://perldoc.perl.org. Maybe you can use 5.14.1 just for testing. I see something changed in the socket implementation, but currently unsure if it is a bug or changed behavior or ???
 
Old 05-03-2012, 03:37 AM   #5
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
download perl 5.14.2
Quote:
[majid@localhost Perl]$ perl -v

This is perl 5, version 14, subversion 2 (v5.14.2) built for i386-linux-thread-multi

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
so if roll back to the old version,would it change the behavior of the updated modules too,i mean consider i switch to the old version but not the modules,what then! [if i'm not wrong]

Last edited by z99; 05-03-2012 at 03:38 AM.
 
Old 05-04-2012, 04:47 AM   #6
z99
Member
 
Registered: Aug 2009
Location: Iran-Sari
Distribution: CentOS,Fedora,Slitaz
Posts: 136

Original Poster
Rep: Reputation: 20
Solved,it has nothing to do with the new version,this one works:
Quote:
use strict;
use IO::Socket;
use Chatbot::Eliza;
use POSIX 'WNOHANG';

$SIG{CHLD}=\&sig;
sub sig
{
while(waitpid(-1,WNOHANG)>0) {}
};

my $sver=IO::Socket::INET->new(LocalPort=>'12000',Listen=>20,Proto=>'tcp',Reuse=>1) or die 'Cant create Socket';
print "Server Ready .........\n";

my $clnt;
while(1)
{
next unless $clnt=$sver->accept;
print 'Connection Accepetd'."\n";
my $child=fork();
if ($child==0) # or if ($child),indicating,we are dealing with child
{
$sver->close;
interact($clnt);
exit 0; #when $child has done with sub simply exit
}
$clnt->close; #if its not child it is parent,we just close our copy of the connected socket and go back to the top of the loop to accept() another
#connection. While we are waiting for a new connection, the child is taking care of the old one.
}

sub interact #note:we read and write through the connected socket
{
my $s=shift;
STDIN->fdopen($s,"<") or die 'stdin';
STDOUT->fdopen($s,">") or die 'stdout';
STDERR->fdopen($s,">") or die 'stderr';
$|=1;
my $bot=Chatbot::Eliza->new;
$bot->command_interface();
}
 
  


Reply



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
install vim via yum thinks perl is required - I build perl 5.14.2 from source rubanek Linux - General 4 05-02-2012 06:42 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 PM.

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