Chris: On Tue, Aug 11, 2015 at 02:24:13PM +0200, Chris Knipe wrote: > Hi All,
Hello, > I'm reading "binary" from a socket, and just like a normal email message on > the SMTP protocol (for example), the data is terminated by \r\n.\r\n > > > > I'm saying "binary" because the data stream does include yEnc data (or > character codes > 127) > > > > I'm having issues to exit my read loop when I receive the termination > characters... > > > > my $numBytesToRead = 512; > > my $buffer; > > while ($bytesRead = read($TCPSocket, $buffer, $numBytesToRead)) { > > if ($buffer =~ m/\r\n\.\r\n$/) { > > print $buffer; > > last; > > } > > } > > > > I'm obviously doing this wrong :( Can anyone perhaps show me the light? I can think of a couple of problems. Firstly, if the handle isn't being read with binmode set then perhaps the \r\n are being converted to \n (if this is Windows)? How are you creating/initializing the socket? You used to be able to set binary mode on a file handle using binmode() like this: binmode $fh; Similarly, the character encoding of the data on the socket could matter. You said there are character codes above 127. Does that mean the encoding is 8-bit such as [extended] ASCII or latin1, or do you mean the character codes are WAY above 127? Character encoding could be another culprit if the \r and \n characters are encoded differently in the stream than you (and Perl) expects. Using the IO layers or the explicit Encode module you should be able to decode the stream into a Perl string that Perl understands properly. You can attach an IO layer to the file handle by passing an additional argument to binmode: binmode $fh, ':encoding(UTF-8)'; Lastly, you're reading from a socket so there's no guarantee that the buffer string is going to necessarily end at the termination boundary. Perhaps the protocol guarantees that, but the socket surely doesn't. You may need to look for that terminating sequence in the middle of the buffer. You could use Data::Dumper and/or various poor-man's debugging techniques to try to identify the problem here. You could write what you read to a file and inspect that file with a hex editor afterward. Alternatively, you could invoke perl's debugger with the -d flag and inspect the program state in real-time. Does any of that help? Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
signature.asc
Description: Digital signature