i've read this http://www.perlfect.com/articles/sockets.shtml
and found it to be a very good introduction to network programming in perl
but my problem is that what is there is not enough.
as one can see,it only permits communication in one sense and not the
other.
i've tried to improvise on what i saw there and try to make it the other
way around also.
i've succeded somehow,but it works only for...about 3 seconds and then
i think it stops.
i need help on this,i must say that i have checked a book
Network Programming in Perl and also some other articles and was not
able to found a solution.

Ok,now the code,and afterwards some comments about it.


receiver.pl
use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7070', Proto=>'tcp', Listen=>1, Reuse=>1, ); my $new_sock = $sock->accept(); $i=0; while(1){ print $new_sock "HIII CALLER!\n"; print $new_sock "stop reading ,caller!\n"; while(<$new_sock>){ print $_; if($_ == "stop reading ,receiver!\n"){ last; }; } } close($sock);



caller.pl
use IO::Socket; $sock =0; while(!$sock){ $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => '7070', Proto => 'tcp', ); } die "Could not create socket: $!\n" unless $sock; while(1){ print $sock "HIIII RECEIVER!\n"; print $sock "stop reading ,receiver!\n"; while(<$sock>){ print $_; if($_ == "stop reading ,caller!\n"){ last; }; } }; close($sock);


in caller.pl we have the first while wich is waiting for receiver to
create the socket,and when the socket will be created
!$sock will be true thus,it will exit the loop and start
writing to the socket and reading from it.
what it will do ,actually is,it will first write a message to the
receiver,and then write a message that the receiver will interpret
as to stop reading and do something else(receiver will write to
the socket his message).
and so forth,and in the end close the socket.

in receiver.pl we create the socket and we start writing and reading from
it,writing some messages to caller and reading what caller has said,
but also interpret the "stop reading receiver!\n" message wich will
mean the receiver will stop reading and will start writing some messages
to caller,but in turn he will also issue a message to caller for him to stop
reading ,the message for this will be "stop reading caller!\n".


what i want you to know is that i do not intend to say that this code is
very correct or anything ,i just want to make it work properly ,that is all.
if you know of any other methods,please inform me.

thank you


In reply to perl networking problem,no threads used by spx2

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.