Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: IO Socket buffer flushing

by ikegami (Patriarch)
on Jan 29, 2010 at 16:06 UTC ( [id://820364]=note: print w/replies, xml ) Need Help??


in reply to IO Socket buffer flushing

Some simple stuff first:
  • IO::Socket sets the socket to autoflush, so all your ->flush() are useless.

  • $nonblocking = 1; ioctl($sock, 0x8004667e, \$nonblocking);

    is an awful way of writing

    $sock->blocking(0);

If I understand correctly, your problem is that two writes in quick succession end up being picked up by the second read of two reads in quick succession. If you consider that a problem, you're doing something majorly wrong. Maybe we can help you fix your actual problem if you tell us about it.

Replies are listed 'Best First'.
Re^2: IO Socket buffer flushing
by Arvind (Initiate) on Apr 08, 2010 at 19:16 UTC
    Yes I am also encountering the issue where two successive writes are picked up by second read. Please let me know how to handle this. My scenario is this one. I have multiple files whose data has to be sent via a socket and on the same connection the response data has to be picked up. I am able to get responses for the first two files. But for third file 40 % of the response data is being written to one output file and the rest to another file. There is no consistency in the recv of response and then writing the data to the output files. any help is appreciated. Thanks

      You don't show us any code. You don't show us how you're writing the files. If you use a delimiter between the two files, it's far more likely that there is an error in your (non-socket) program logic that mixes up the content of the files. Also, I wouldn't bet on a single read corresponding to a single write on the other end.

        Here is the code. I am new to perl programming. I have to spawn four sub processes but initially trying to get 1 process to send the files. So the same socket connection is used to send the data and wait till they give us response and receive the response and write to a file.

        my $BUFSIZE = 32000; my $varhex3= "\x03"; $SIG{USR1} = "doneit"; # I added the below for trial and error not sure what this #does. my $nonblocking = 1; select(S); $| = 1; select(STDOUT); for( my $childcounter = 1; $childcounter <= 1; $childcounter++ ) { my $pid = fork(); if ($pid) { # parent push(@childs, $pid); } elsif ($pid == 0) { # child if ($childcounter == 1) { @mylist1 = @{$hash1{$childcounter}}; my $socket1 = &opensock(); $myport = $socket1->sockport(); foreach $File (@mylist1){ $BaseFName = basename($File); my $outputdir = "directory goes here"; my $outputfile = "$outputdir\/$BaseFName".".res"; open(INFILE,"$File"); $data=<INFILE>; close INFILE; # print $sock "$data"; $socket1->send ($data); open (RESPONSEFILE, ">$outputfile") or die $!; my $lastchar = ""; my $Stop_Read = 0; while (! $Stop_Read) { $lastchar = ""; $data1 = ""; recv($socket1,$data1,$BUFSIZE,0); sleep 2; my $mylen = length($data1); $lastchar = substr ($data1, $mylen-1, 1); # print STDOUT $data1; print RESPONSEFILE $data1; if ($lastchar == $varhex3){ $Stop_Read = 1; } } close RESPONSEFILE; } close($socket1); } } sub doneit { $gotone = 1; } sub opensock{ my $sock = new IO::Socket::INET ( PeerAddr => 'IP address', PeerPort => 'Port number', Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1, Timeout => 60, ); die "Could not create socket: $!\n" unless $sock; #select (sock); $| = 1; }

      I am also encountering the issue where two successive writes are picked up by second read.

      Not surprisingly, the same answer applies to you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found