kzle has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, I am working on a script that enable chatting features, like IM. I am using socket for msgs sending and receiving. The hardest part for me, is how to display msgs synchronously? I tried many ways, but none of them works. Has someone got the experience of writing such simple chatting scripts?

Here's my test code, I used two sockets,

The following codes work under WinXP, which from original thread by BrowserUK: Re: Sockets and threads, oh my!
## codes from BrowserUK #------------------------------------------------ # For server #------------------------------------------------ #!/usr/bin/env perl use strict; use warnings; use threads; use IO::Socket qw(:DEFAULT :crlf); my $port = 1985; my $socket = new IO::Socket::INET ( LocalPort => $port, Proto => 'tcp', Listen => 5, Reuse => 1, ) || die "Not able to create socket, $!\n"; my $client = $socket->accept(); my $arg = 1; ioctl( $client, 0x8004667e, \$arg ); my $listenerThread = threads->create(\&listener, $client) or die "$@\n ++"; while (<STDIN>){ chomp; print $client $_, CRLF; } exit(0); sub listener($) { my $s = shift; my $data; $/ = CRLF; while( 1 ) { ## try to read something. my $read = sysread($s, $data, 2048); ## Quit if sysread gave an error (returned undef) other than 1 +0035 ## 10035 => A non-blocking socket operation could not be comp +eted immediately last unless defined $read or 0+$^E == 10035; ## If there was nothing to read, sleep a while and retry #sleep 1 and redo unless $read; ## Got something, so display it syswrite(STDOUT, $data); } print "\nListener exiting\n"; }; #-------------------------------------------------- # For client #-------------------------------------------------- use strict; use warnings; use threads; use IO::Socket qw(:DEFAULT :crlf); my $host = shift || 'localhost'; my $socket = IO::Socket::INET->new( PeerAddr => $host, PeerPort => 1985, Proto => 'tcp', Type => SOCK_STREAM, Timeout => 5 ) or die "Couldn't connect to remote host: $@\n"; my $arg = 1; ioctl( $socket, 0x8004667e, \$arg ); my $listenerThread = threads->create(\&listener, $socket) or die "$@\n ++"; while (<STDIN>) { chomp; print $socket $_, CRLF; }; exit(0); sub listener($) { my $s = shift; my $data; $/ = CRLF; while( 1 ) { ## try to read something. my $read = sysread($s, $data, 2048); ## Quit if sysread gave an error (returned undef) other than 1 +0035 ## 10035 => A non-blocking socket operation could not be comp +eted immediately last unless defined $read or 0+$^E == 10035; ## If there was nothing to read, sleep a while and retry #sleep 1 and redo unless $read; ## Got something, so display it syswrite(STDOUT, $data); } print "\nListener exiting\n"; };
Any suggestion would be graceful! Thanks. Thanks, Yun

Replies are listed 'Best First'.
Re: Sync messages for chatting peers
by almut (Canon) on Mar 24, 2010 at 12:48 UTC

    Your basic problem is that the my $in = <STDIN> is blocking until a complete line has been entered. So nothing else will happen in either the server or client during that time...

    Maybe try reading/assembling messages on a character by character basis (non-blocking) — see Term::ReadKey.  Or use separate processes or threads to handle reading the input being typed plus sending it, and printing stuff received from the other side. Generally, watch out not to create a deadlock, i.e. that both sides are waiting for the other side to make the next step  (this is by no means a trivial exercise).

    (At the moment, I don't have the time to compose a complete example. Maybe later... unless someone else has posted something by then.  Also, it often helps to look at how existing working code does it. IOW, search CPAN — I would be surprised if no one else has written something like this already...)

Re: Sync messages for chatting peers
by rowdog (Curate) on Mar 24, 2010 at 21:01 UTC

      To Almut:

      Thanks for pointing out the problem. I am trying to solve it as you explained. But it seems that Term::ReadKey module non-blocking readline feature is NOT well supported in Windows either.

      To rowdog:

      Yes, I read the doc, unforturnely, my script works under windows, but fork() is NOT well supported in Wins. I tested it under both Ubuntu and Windows XP with the same code.
      #!/usr/bin/perl my $i = 0; my $in; while( 1 ){ ++$i; my $pid = fork(); if ( $pid ){ $in = <>; chomp( $in ); print "Child: $in\n"; } else{ print "Parent: $i \n"; sleep 5; } }
      In Linux, it works fine, while screen update still hangs at reading input, requires <enter> key to trigger the update.

      Thanks,

      Yun

        I don't do Windows but I believe the usual advice would be to use threads. BrowserUk posted a nice example of a (non-blocking) threaded client at Re: Sockets and threads, oh my!

        That client code is 4 years old and I'm not sure if you still need to do the ioctl song and dance to get Windows to give you a non-blocking socket (if you even want one), but the principles should still be relevant.