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

Hello Perlmonks,
I am hoping to find some help for an issue I haven't been able to resolve on my own. I am relatively new to Perl but I had to make a socket connect to send a message then wait to receive the acknowledgment message before moving on. However, using the Perl book to guide me I am not able to read the response from the server correctly. All I get is a blank line on the first message and any following message I get the acknowledge message for the previous message.

After 1st transaction:
Received:

After 2nd transaction:
MSA|AA| MSH|^~\&|PFT|D|STAR|D|20110316115753|||JKH1157|P|2.2||||||

What I need to see is the acknowledge message for the particular message that was sent not the previous one. I am pretty sure this is a buffer block issue but still haven't been able to resolve it. I have included my code below.
#Create Socket socket(Impact, PF_INET, SOCK_STREAM, getprotobyname('tcp')); #Build the address of connection my $internet_addr = inet_aton("axtcnr") or die "Couldn't convert axtimpact into internet address\n"; my $paddr = sockaddr_in(6540, $internet_addr); #Connect connect(Impact,$paddr)or die "Couldn't conne +ct to axtimpact:6540 \n"; select ((select(Impact) , $| = 1)[0]); #enable command buffering if($msg =~ m{(\013.*\034\015)}smx){ print Impact $1; } shutdown(Impact,1); #read the remote answer my $server_message = do { local $/; <Impact>}; # get the response print "recieved: $server_message\n"; #Terminate the connection when done close(Impact);
Any help would be greatly appreciated. Thanks in advance.

Replies are listed 'Best First'.
Re: Socket buffer issue
by ikegami (Patriarch) on Mar 31, 2011 at 21:21 UTC

    «<Impact>» reads until a newline is received. If the first message is "blank", it's because there was nothing before the first newline.

    Update: Well, that's not right. (Tired today.) But then, neither is the question. What is this code you posted, and why isn't it the code that exhibits the problem? You only have one read, yet you talk of the wrong read getting the data you want.

    Update: By the way, IO::Socket::INET greatly simplifies the socket creation.

    use IO::Socket::INET qw( ); my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerHost => 'axtcnr:6540', ) or die("Can't connect to axtcnr:6540: $!\n");

    ($sock is already set to autoflush.)

      That is the correct code. We are picking up individual time stamp files. I only put the piece of code in that I am having issues with but I am picking up all the transactions that are in the directory storing them in an array and pushing them across one at a time. So I connect, check to see the file meets my requirement, push it across, and wait for acknowledgment. If the array stores one file I get a blank response. If I have two or more files the first response is blank and all responses that follow are the acknowledgment for the previous file. I am using a foreach loop based on the transaction array.
        The problem is on the server side.