in reply to chat between client and server

Start it off simple. I like to build the server and telnet into it to start with. Gives you full control of the client while getting the server figured out. The following works, though i think there are probably some major issues with it memory wise. It is a nice simple start though.

#!/usr/bin/perl use strict; use warnings; use threads; use IO::Socket; my $server = IO::Socket::INET->new(LocalPort => 1300, Proto => 'tcp', Listen => 10, Reuse => 1); die "Failed to create socket: $!\n" unless $server; my $client; sub listen_to_client { while (my $get = <$client>) { print "client: $get"; }; print "Client closed\n"; } while ($client = $server->accept()) { print "Client connected\n"; threads->create("listen_to_client"); }

I ran it in one terminal, then telnet-ed in from two separate windows. Works like a charm.


___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: chat between client and server
by wavenator (Novice) on Oct 11, 2007 at 18:53 UTC
    didnt get my point i didnt try to make s server program the controls more then 1 client i've tried to make server+client that chats each other without IO Blocking

      Yes, and that is what this does. The server has multiple clients connecting to it that do not block each other while waiting. After this you use the same setup to build a client. The point was to start small and add features. Single client, multiple client, non-blocking multiple client connections, etc. This already does non-blocking reads from multiple clients. Doing the same thing in a client shouldn't be much harder. For non-blocking IO i don't think you want to use threads though, I think IO::Select is a better path to take.

      Either way my point was that if you have complex client/server interactions like this, start as simple as possible adding features and testing constantly. You should then be able to pinpoint exactly where the code is broken and ask specific questions.


      ___________
      Eric Hodges

        Here is a non-blocking client that sends and receives in separate threads

        #!/usr/bin/perl use strict; use warnings; use IO::Socket; use threads; my $server = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 1300, Proto => 'tcp', Reuse => 1); die "Client Couldn't connect to server: $!\n" unless $server; threads->create(sub { while (my $msg = <$server>) { print "Recieved: $msg"; } }); threads->create(sub { while (my $msg = <STDIN>) { print "Sending: $msg"; print $server $msg; } });

        And a server modified to echo its input. Can't use STDIN because all your clients would fight over it. Though you could create a single thread that listens and then broadcasts out to all clients.

        #!/usr/bin/perl use strict; use warnings; use threads; use IO::Socket; print "Starting server: $$\n"; my $server = IO::Socket::INET->new(LocalPort => 1300, Proto => 'tcp', Listen => 10, Reuse => 1); die "Failed to create socket: $!\n" unless $server; sub listen_to_client { my ($client) = @_; my $tid = threads->tid(); while (my $get = <$client>) { print "client ($tid) : $get"; print $client $get; }; print "Client closed\n"; close $client; } while (my $client = $server->accept()) { my $thr = threads->create("listen_to_client", $client); print "Client connected ", $thr->tid,"\n"; $thr->detach; }

        ___________
        Eric Hodges