Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Sending multiple requests to a perl socket

by fixu2 (Initiate)
on Sep 23, 2013 at 10:32 UTC ( [id://1055265]=perlquestion: print w/replies, xml ) Need Help??

fixu2 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to send and receive requests to the same socket in the following fashion.
  1. 1) open socket
  2. 2) send LOGINPDU,
  3. 3) recv response from server and if ok send TRANSPDU
  4. 4) recv response from server
  5. 5) send LOGOUTPDU.
#Sample of what am trying to do below:
my $sock = IO::Socket::INET->new( Proto=> "tcp", PeerAddr => "$IP", PeerPort => "$port") || die "Could not connect to host => $IP: +$port \n"; print $sock $LOGINPDU."\n"; while($ans=<$sock>){ $ans1.=$ans; } $sock->flush(); if($ans1){ print $sock $transPDU."\n"; while($tns=<$sock>){ $tns.=$tns; } } $sock->close();
The problem is that am only receiving response for the first request. Am not receiving response for the second pdu

Replies are listed 'Best First'.
Re: Sending multiple requests to a perl socket
by NetWallah (Canon) on Sep 23, 2013 at 15:41 UTC
    Your read loop:
    while($ans=<$sock>)
    will not exit until the other side closes the socket.

    Try doing a single read

    $ans=<$sock>;
    in a scalar context, then "print" to the socket, followed by another "read".

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

      thanks NetWallah. I tried the following and still failed: my $ans1=<$sock>; my $ans2 = ""; $sock->flush(); if($ans1) { my $sent = $sock->send($PDU2); $ans2=<$sock>){ } print $ans2,"\n";
        "still failed" is an insufficient description - it does not help us in assisting you.

        Please add "print" statements at each step, with relevant data output. See WHERE it is failing, and with what error.

        You also need to print output for each alternative. In other words, if your "if" statement takes the "false" path, you need to add an "else" clause to print that the IF took that path.

        Please use <code> tags </code>, so your code displays in a more legible manner.

                     My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Re: Sending multiple requests to a perl socket
by wjw (Priest) on Sep 23, 2013 at 11:08 UTC
    my $sock = IO::Socket::INET->new( Proto=> "tcp", PeerAddr => "$IP", PeerPort => "$port") || die "Could not connect to host => $IP:$port \n"; print $sock $LOGINPDU."\n"; while($ans=<$sock>){ $ans1.=$ans; } $sock->flush(); if($ans1) { print $sock $transPDU."\n"; while($tns=<$sock>) { $tns.=$tns; } } $sock->close();
    What problem are you seeing?
    • ...the majority is always wrong, and always the last to know about it...
    • The Spice must flow...
    • ..by my will, and by will alone.. I set my mind in motion
Re: Sending multiple requests to a perl socket
by fixu2 (Initiate) on Sep 23, 2013 at 14:57 UTC
    The problem is that am only receiving response for the first request. Am not receiving response for the second pdu

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found