in reply to Socket Again!!!!

Just couple of small changes, and now both of your client and server are happy. I added my comments at each place I touched.

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); }

Replies are listed 'Best First'.
Re: Re: Socket Again!!!!
by Anonymous Monk on Oct 17, 2003 at 04:52 UTC
    Bows... With the right hand touching the chest!
    One more question pg ....
    You had given me a same example using threads...
    Suppose you had to choose between the threads and the set of the scripts above, which will it be?
    Thanks!

    (Bows again ;-) )

      Depends on, if you only expect one client (or one client a time), your way is just fine. If you expect multiple clients talking to the server at the same time (and client might come and go ), then sooner or later you have to go with thread. For this kind of application, using thread makes your code more neat, clear, easy to understand, and easy to maintain.

      Play with thread. If you have problem with threading, come and ask, someone here will help.