wavenator:

I've loaded up the code, and added a couple of traces and fixed a minor bug. You're having some interaction problems with your console I/O. It works ... sort of. I've run out of time, so I'll give you back the code that I've tried, and show you the symptoms.

First, I added some traces, so I could see what was happening--Rather than wait for successful console input, I sent a message on both the client and server side so I could see that they both sent the expected messages. Then I discovered the minor bug: you were using $so instead of $client in the server.

Once I did that, the code started almost working. Basically, I could type some text on the server side, and it would appear on the client side. Similarly, I could type text on the client side and it would appear on the server side. But you have to alternate client & server, as successive messages aren't sent until it receives one from the other side.

Here's the slightly modified server:

#!/usr/bin/perl -w use warnings; use strict; use threads; use IO::Socket; my $so = new IO::Socket::INET(LocalPort => '1300', Proto => 'tcp', Listen => 10, Reuse => 1,); my $client; die "Socket Error: $!\n" unless $so; sub start_thread1 { print "thread1 started\n"; while(my $get=<$client>) { print "client : $get"; } print "thread1 ended\n"; } sub start_thread { print "thread started\n"; print $client "OK ... whaddaya want?\n"; while (my $msg = <STDIN>) { print $client $msg; } print "thread ended\n"; } while ($client = $so->accept()) { my $thread = threads->create("start_thread"); my $thread1 = threads->create("start_thread1"); # This message appears to go nowhere ... we should use $client +! print $so "connected ... whatcha want?\n"; print "connected\n"; $thread->join(); }
And here's the client:

#!/usr/bin/perl -w use warnings; use strict; use threads; use IO::Socket; my $so = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 1300, Proto => 'tcp', Reuse => 1,); die "Socket Error: $!\n" unless $so; my $thread = threads->create(\&start_thread); my $thread1 = threads->create(\&start_thread1); sub start_thread { print "thread\n"; while (my $txt=<$so>) { print "server : $txt"; } print "endthread\n"; } sub start_thread1 { print "thread1\n"; print $so "Client connected!\n"; while (my $msg = <STDIN>) { print $so "$msg"; } print $so "Client disconnected!\n"; print "thread1end\n"; } $thread->join(); $thread1->join();

Unfortunately, I don't know why they're blocking. I don't use threads, so there are several possibilities: Blocking in the IO::Socket::INET module, blocking on your console I/O, console I/O interaction with threads, and others that I can't think of.

You might want to dig through perlipc and read perlfunc for more clues. I was expecting to see read and write calls in your code, so when I initially saw it, I was wondering how on earth it was supposed to work. I also didn't see any accept() calls, but apparently the IO::Socket::INET module takes care of that since I clearly see a connection and traffic taking place.

If I get an opportunity, I'll check into it some more after work. (Unless, of course, you've already got it solved by then....)

...roboticus


In reply to Re^3: chat between client and server by roboticus
in thread chat between client and server by wavenator

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.