Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Can I read a line from a socket without the socket sending \n?

( #44171=categorized question: print w/ replies, xml ) Need Help??
Contributed by reyjrar on Nov 30, 2000 at 20:56 UTC
Q&A  > network programming


Answer: Can I read a line from a socket without the socket sending \n?
contributed by mikeock

I use a socket connection in a subroutine that closes after I send a command. The easiest way that I have found to read the text back is using the shutdown ($socket_name, 1). You can then run a

while(<$socket_name>){ print $_; }
to return whatever the prompt/responce is.

Again, for each command that I send, I create a new socket connection send the command and disconnect/kill the socket.

Also once the while loop is in place, you can use a regex to take what you want out, like the prompt, since it is making a new connection everytime.

Answer: Can I read a line from a socket without the socket sending \n?
contributed by merlyn

The hardest part about sockets is knowing when to stop reading. You can invent a line-oriented protocol, or use some other indication (like a byte count followed by that many bytes).

Knowing what to use is an art call, decided by experience and study of the prior art. Good luck in your quest!

Answer: Can I read a line from a socket without the socket sending \n?
contributed by crazyinsomniac

Ok ask your self this,
what is a line?
if you can answer it without using '\n' then you can read a line without sending '\n'.
If i were you i would seriously consider limiting the length of a line to 80 characters,
that way when you just read 80 chars you consider it a line or just stick to what works and send '\n'.

Answer: Can I read a line from a socket without the socket sending \n?
contributed by Anonymous Monk

use sysread

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2014-08-31 06:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (294 votes), past polls