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

In reply to Sync messages for chatting peers by kzle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.