Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Bidirectional Socket, Blocking

by DanielPHuber (Initiate)
on Oct 13, 2012 at 19:25 UTC ( #998872=perlquestion: print w/ replies, xml ) Need Help??
DanielPHuber has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a simple server in a rather standard way:

In princip it works, you can connect with a telnet client, send data from the client to the server and vice versa. But there is a problem sending data from the server to the client. The data is not sent (print $new_sock $_;) until the server recieves a line from the client. Somthing is blocking. As you can see in the inactive lines, I tried all sort if thing to unblock this, to no avail. I seem to miss something concerning blocking of sockets.

use IO::Socket; use warnings; use strict; #use Term::ReadKey; my $flush = 1; my $sock = new IO::Socket::INET ( LocalHost => 'PK5', LocalPort => '7070', Proto => 'tcp', Listen => 1, # max concurrent caller Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; print "SERVER Waiting for client connection on port 7070\n"; my $new_sock = $sock->accept(); # $new_sock->autoflush(1); # so output gets there right +away my $peer_address = $new_sock->peerhost(); my $peer_port = $new_sock->peerport(); # $new_sock->blocking(0); # $| = 1; print "Accepted New Client Connection From : $peer_address, $peer_port +\n"; my $pid = fork(); if (not defined $pid) {print "resources not available.\n";} elsif ($pid == 0) { # child, receiver while(<$new_sock>) { print $_; if($_ eq "end\n"){last;} } exit(0); } else { # parent, sender while(<>){ # $new_sock->flush; # ReadMode 1; print $new_sock $_; # $new_sock->flush; if($_ eq "end\n"){last;} } waitpid($pid,0); }

Comment on Bidirectional Socket, Blocking
Download Code
Re: Bidirectional Socket, Blocking
by BrowserUk (Pope) on Oct 13, 2012 at 20:48 UTC

    You cannot send to a socket whilst the same end that you are trying to send to is in a receive state. Even if the receive is being done on a dup() of the socket in a fork or thread.

    Most of the time, that isn't a problem because comms protocols tend to be a conversation where one end speaks and the other replies. If you need to be able to initiate sends from both ends, then your reads will need to be non-blocking.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      Hi Uk, thank's for the replay. I tried all sort of ways using unblocking reads, but to no avail. I could not get a real satisfactory bidirectional communication. If you know an example where this works, I would be gratful. I now resort to a rather clumsy way, by opening two ports, using one to send, the other to receive. This works, but it does not seem to be too clever and if there is a better way, I am happy to engulf it. cheers, Daniel
        I tried all sort of ways using unblocking reads, but to no avail.

        If you posted one of your attempts, we may be able to sort that out for you.

        If you know an example where this works,

        The classic method of performing non-blocking IO is to use select. For examples, scan a few of these hits site:perlmonks.org IO::Socket IO::Select.

        I now resort to a rather clumsy way, by opening two ports, using one to send, the other to receive. This works, but it does not seem to be too clever

        If it is good enough for the FTP-family of protocols, it is good enough for me.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998872]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-09-01 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls