fixon
fixon

Reputation: 1

Sending requests to perl socket

I am trying to send and receive requests to the same socket in the following fashion.

  1. open socket
  2. send LOGINPDU,
  3. recv response from server and if ok send TRANSPDU
  4. recv response from server
  5. send LOGOUTPDU.

Sample of what am trying to do below:

#1
my $sock = IO::Socket::INET->new( Proto=> "tcp", PeerAddr => "$IP",
               PeerPort => "$port") || 
           die "Could not connect to host => $IP:$port \n";
#2
print $sock $LOGINPDU."\n";

#3
while($ans=<$sock>) {
    $ans1.=$ans;
}
$sock->flush();
if($ans1) {
    print $sock $transPDU."\n";
    #4
    while($tns=<$sock>) {
        $tns.=$tns;
    }
}

#5
$sock->close();

The problem is that I am only receiving response for the first request.

Upvotes: 0

Views: 564

Answers (1)

Jost
Jost

Reputation: 1534

I would guess that the problem is that your script stays in the first while loop, which waits for the response lines after LOGINPDU is sent to the server (step 2 -> 3)). This is because readline (< >) is blocking and the server did not send an EOF, which is (with your) code the only option to get out of the loop, but as a side-effect it would also close the connection.

So, if the server's answer is (only) one line you can try something like this:

$ans1=<$sock>;
$sock->flush();
if($ans1) {
    ...
}

Hope that helped a bit.

Upvotes: 1

Related Questions