Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: IO Socket buffer flushing

by Arvind (Initiate)
on Apr 08, 2010 at 19:16 UTC ( [id://833596]=note: print w/replies, xml ) Need Help??


in reply to Re: IO Socket buffer flushing
in thread IO Socket buffer flushing

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

Replies are listed 'Best First'.
Re^3: IO Socket buffer flushing
by Corion (Patriarch) on Apr 08, 2010 at 19:25 UTC

    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; }
Re^3: IO Socket buffer flushing
by ikegami (Patriarch) on Apr 08, 2010 at 19:48 UTC

    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://833596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found