ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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);
}
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).
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?
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 ???
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]
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();
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.