Just couple of small changes, and now both of your client and server are happy. I added my comments at each place I touched.
That logic was wrong, and it shall be:if (( defined $buf ) || ($buf =~ /\w/))
In your logic, even if the first clause results false (not defined), Perl will still evaluate the second condition. In my logic, if the first condition results false, the second condition will not be evaluated, and that's what you really want.if (( defined $buf ) && ($buf =~ /\w/))
your server with my modification: use IO::Socket; use IO::Select; use strict; my ($server, $client, $buf, $new_sock); my ($SOCK_STREAM, $server_port); #pg: remove $buf, defined twice. { my $listener=IO::Socket::INET->new( LocalPort => 21000, Listen => +2, Reuse => 2 ); die "Can't create socket for listening: $!" unless $listener; print "Listening for connections on port 21000\n"; my $readable=IO::Select->new; $readable->add($listener); while(1) { my ($ready) = IO::Select->select($readable, undef, undef, +undef); foreach my $s (@$ready) { if($s == $listener) { $new_sock = $listener->accept; $readable->add($new_sock) if $new_sock; print $new_sock "Welcome!\r\n"; + } else { print $listener "Why don't you call me anymore?\n" +; $buf = <$s>; print "$buf" if (defined($buf)); #pg: added if def +ined if (( defined $buf ) && ($buf =~ /\w/)) #pg: it wa +s || { if ($buf =~ /goodbye/i) { print $s "See you later!\n"; $readable->remove($s); $s->close; } else { print $s "You said: $buf\n"; } } else { print $new_sock "Still there?\r\n"; $readable->remove($s); $s->close; print STDERR "Client Connection closed +, because you said $buf\n" if (defined $buf); #pg: added checking for + defined $buf } } } } } your client with my modification: use Socket; use IO::Socket; use strict; my ($remote_host, $remote_port); my ($internet_addr, $paddr); my ($soc, $answer, $socket); { use IO::Socket; $remote_host="localhost"; $remote_port=21000; $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_port, Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect to $remote_host:$remote_port : $@\n"; do { print $socket "New host\n"; $answer=<$socket>; print $answer; }while ($answer !~ /end/); #pg: was =~, it shall really be !~ close($socket); }
In reply to Re: Socket Again!!!!
by pg
in thread Socket Again!!!!
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |