in reply to Re^2: chat between client and server
in thread chat between client and server

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