How to flush a stinkin' socket?

Y

your name here

You may be familar with the following code snippet as it was taken
from some posts in this group. However, whatever I do, I cannot get
this message through the socket immediately which causes the other
side to determine I am in trouble because it hasn't received a health
check. The message finally shows up after I issue the shutdown
command, which is unacceptable. Any ideas? Windows peculiarity?


use strict;
use IO::Socket;

my $msg = 'Take me to your leader';

my $conn = IO::Socket::INET->new( PeerAddr => '127.0.0.33',
PeerPort => '8089',
Proto => 'tcp');

die "Cannot connect: $!\n" unless $conn;
$conn->autoflush(1);
$conn->print($msg);
$conn->shutdown(1);
sleep 3;
$conn->shutdown(2);
 
B

Brian McCauley

your said:
You may be familar with the following code snippet as it was taken
from some posts in this group. However, whatever I do, I cannot get
this message through the socket immediately which causes the other
side to determine I am in trouble because it hasn't received a health
check. The message finally shows up after I issue the shutdown
command, which is unacceptable. Any ideas? Windows peculiarity?

How are you sure that it is not being sent? Is it possible that
there's unwanted buffering at the reading end? Perhaps the reading
application is waiting for some kind of end-of-message seqence that does
not appear in you message.
 
Y

your name here

Brian McCauley said:
How are you sure that it is not being sent? Is it possible that
there's unwanted buffering at the reading end? Perhaps the reading
application is waiting for some kind of end-of-message seqence that does
not appear in you message.

The message on the receiving end appears when the shutdown command is
issued. I am not particularly familiar with the receiving end's
application (a Delphi program) but I have the source and can step
through. The receive event is triggered when that shutdown command
occurs.
 
T

Thomas Kratz

your said:
The message on the receiving end appears when the shutdown command is
issued. I am not particularly familiar with the receiving end's
application (a Delphi program) but I have the source and can step
through. The receive event is triggered when that shutdown command
occurs.

How does the receiving application read the data? If it tries to read a
whole line it will wait until a newline is sent (which you don't) or EOF
of the socket (which you trigger with your shutdown).

So try to send a newline after your string.

Thomas
 
J

J. Romano

You may be familar with the following code snippet as it was taken
from some posts in this group. However, whatever I do, I cannot get
this message through the socket immediately which causes the other
side to determine I am in trouble because it hasn't received a health
check. The message finally shows up after I issue the shutdown
command, which is unacceptable. Any ideas? Windows peculiarity?

Dear Your Name Here,

I had a similar problem about a year ago, and it looks like your
problem might be due to that same issue.

A good way to check whether or not your Perl script is working is
to write a receiving script to run alongside your connector script and
see if they are able to talk to each other. I modified your script
slightly and included another script, called "conn.pl" and "rec.pl",
respectively. Here thy are (more follows after the scripts):


#!/usr/bin/perl
# File: conn.pl

use strict;
use warnings;
use IO::Socket;

my $msg = "Take me to your leader";

my $conn = IO::Socket::INET->new( PeerAddr => '127.0.0.1',
PeerPort => '8089',
Proto => 'tcp');

die "Cannot connect: $!\n" unless $conn;
$conn->autoflush(1);
$conn->print($msg);

sleep 100;

print "Program $0 has ended.\n";

__END__



#!/usr/bin/perl
# File: rec.pl

use strict;
use warnings;
use IO::Socket;

my $localPort = 8089;

print "Attempting to connect as server...\n";
my $server = IO::Socket::INET->new(LocalPort => $localPort,
Proto => "tcp",
Reuse => 1,
Listen => 1)
or die "\nCouldn't be a server on port $localPort: $!\n";

my $socket = $server->accept();

print "Got a connection!\n";

while (<$socket>)
{
print "Received: $_\n";
}

__END__


Now create those files in the same directory and open two command
windows (and change the directory of both to the directory you saved
them to). Now start both scripts (start the rec.pl script a few
seconds before the conn.pl script).

You will notice that the rec.pl script says that it "Got a
connection!" yet nothing is ever printed until you hit CTRL-C in the
conn.pl script. This looks like the behavior you were talking about
(in that nothing gets sent until the connection shuts down).

The information DOES get sent, but the problem lies with the
receiving script (rec.pl), not with the connector script (conn.pl).
The reason nothing ever gets printed out (until the conn.pl script is
shut down) is because the "while (<$socket>)" code keeps reading the
socket for characters until it encounters a newline character (or
until the connection is terminated, whichever comes first). It keeps
reading until it hits a newline because a newline is what's stored in
the $/ variable (read about this variable in "perldoc perlvar" to find
out more about it).

Therefore, to fix this problem, you can do one of two things.
Either change the string in conn.pl to end with a newline, like this:

my $msg = "Take me to your leader\n";

or add the line:

$/ = \1;

right before the while loop in the rec.pl script. That line
(according to "perldoc perlvar") makes the "<$socket>" code in the
while condition return every character (instead of every line that's
terminated with a newline).

Note that if you take the second approach, then the while-loop will
loop through every letter, and you'll get output like:

Received: T
Received: a
Received: k
Received: e
Received:
Received: m
Received: e
Received:
Received: t
Received: o
 
H

Henry Law

Dear Your Name Here,

I had a similar problem about a year ago, and it looks like your
problem might be due to that same issue.

I've no idea whether or not this post answers the question (sounds
plausible to me ...) but I had to post my admiration at such a
comprehensively helpful and beautifully written response. Worth a
virtual cup of coffee, maybe even a virtual beer!

Henry Law <>< Manchester, England
 
Y

your name here

How does the receiving application read the data? If it tries to read a
whole line it will wait until a newline is sent (which you don't) or EOF
of the socket (which you trigger with your shutdown).

So try to send a newline after your string.

Thomas

It shows up AFTER I send about 23000 newlines. None less will do it.
 
J

Joe Smith

Henry said:
On 8 Oct 2004 07:28:03 -0700, (e-mail address removed) (J. Romano) wrote:

I had a similar problem about a year ago,>

I've no idea whether or not this post answers the question (sounds
plausible to me ...) but I had to post my admiration at such a
comprehensively helpful and beautifully written response. Worth a
virtual cup of coffee, maybe even a virtual beer!

Agreed. And if it had been posted as
Content-type: text/plain
instead of
Content-Type: text/plain; charset=Big5
it would have been even better.
-Joe
 
J

J. Romano

Henry Law said:
I've no idea whether or not this post answers the question (sounds
plausible to me ...) but I had to post my admiration at such a
comprehensively helpful and beautifully written response. Worth a
virtual cup of coffee, maybe even a virtual beer!


Thank-you, Henry! I appreciate your comment.

Of course, some of the credit goes to others in the Perl community,
because they helped me a year ago when I had my own similar problem.
Without their help, I wouldn't have known enough to post a response.

(How about a virtual cup of hot cocoa instead?) ;)

Thanks again,

Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top