in reply to Re^3: Can a socket listen for multiple hosts?
in thread Can a socket listen for multiple hosts?

EDIT: Not sure what I was thinking, Monday morning I guess. I just replaced the \n chars with something else, searched for that after the transmission, then replaced them with \n's again afterward. It works great.

Ikegami, thanks again for the response.
I've been trying to decode this, and I think it mostly makes sense to me, but I need to be able to send multi-line messages (ie \n chars embedded) and want to terminate the sending connection with a specific character string. In other words, I'll have a datagram that looks something like this:

&&&hostname|blue*** (the start/end chars can be anything)
&&&ipaddress|1.2.3.4***
&&&kernelrev|123456-78***
&&&metastat|line1\nline2\nline3\n***

So I basically will be sending all types of data, but I want it to start paying attention when &&& is read, and read until ***, storing the contents in a var. (Probably will end up using a hash, if it matters)

I thought I had a good grip on how exactly that code works, but when I change what I thought I should change, it doesn't work as I expected. Any thoughts to send me in the right direction?

Much appreciated!
/\ Sierpinski
  • Comment on Re^4: Can a socket listen for multiple hosts?

Replies are listed 'Best First'.
Re^5: Can a socket listen for multiple hosts?
by ikegami (Patriarch) on Feb 01, 2010 at 15:13 UTC
    Adjust
    $buf =~ s/\G(.*\n)//g
    to match your definition of record. If your record terminator is "***", you can use
    $buf =~ s/\G(.*?\*\*\*)//sg

    If your records are double quoted strings, you can use

    $buf =~ s/\G("(?:[^"\\]|\\.)*")//sg

    If you're using fixed-length records, you can use

    $buf =~ s/\G(.{128})//sg
    etc.
      I found this thread while super-searching on another Socket question I had, and realized I never updated it with how I ended up achieving this. I user Data::Dumper to flatten the hash into a scalar, sent that to the server, then used eval to turn it back into a hash, and it worked flawlessly.

      Of course now I'm trying the educational task of creating a two-way client-server app using IO::Socket without going back and copy/pasting a bunch of code. I'm learning much more this way, but it's taking me a bit longer to get it working. Thanks again for the responses ikegami!