--------------050008050507030801040101
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

ara.t.howard / noaa.gov wrote:
> On Thu, 2 Feb 2006, Joel VanderWerf wrote:
> 
>> I am in the habit of using send/recv, which are unbuffered, and also
>> sending a length field before the data. If you're interested, I've got a
>> couple of classes (subclasses of TCPSocket and TCPServer) that
>> encapsulate this and are pretty well tested. (Also a C version of the
>> same thing.)
> 
> please share!  i just wrote that, for like the 10th time, last month ;-)
> 
> -a
> 

MTCP -- Message TCP --  Wrapper around TCPSocket and TCPServer that
provides a message (datagram) abstraction implemented by data stream
with added length fields.

Here's the ruby version, attached. There's a tarball at
  http://redshift.sourceforge.net/mtcp/
with the full deal: c version, tests for both, etc. The c version works
with both blocking and non-blocking sockets. The c lib code works on
windows, but the test doesn't (it forks). It's fine on linux and qnx.
The ruby version works on all platforms I've tried (windows and linux).

-- 
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

--------------050008050507030801040101
Content-Type: application/x-ruby;
 nametcp.rb"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filenametcp.rb"

cmVxdWlyZSAnc29ja2V0JwoKIyA9PT0gTVRDUCAtLSBNZXNzYWdlIFRDUAojCiMgV3JhcHBl
ciBhcm91bmQgVENQU29ja2V0IGFuZCBUQ1BTZXJ2ZXIgdGhhdCBwcm92aWRlcyBhIG1lc3Nh
Z2UgKGRhdGFncmFtKQojIGFic3RyYWN0aW9uIGltcGxlbWVudGVkIGJ5IGRhdGEgc3RyZWFt
Lgptb2R1bGUgTVRDUAoKICBjbGFzcyBNZXNzYWdlTGVuZ3RoRXJyb3IgPCBTdGFuZGFyZEVy
cm9yOyBlbmQKICBjbGFzcyBNZXNzYWdlVW5kZXJmbG93IDwgU3RhbmRhcmRFcnJvcjsgZW5k
CgogICMgT3ZlcnJpZGVzIHNvbWUgbWV0aG9kcyB0byBnaXZlIG1lc3NhZ2Utb3JpZW50ZWQg
YmVoYXZpb3IgdG8gYSBzdHJlYW0tC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VOIGV4Y2VlZGVkOiAje2xlbn0gPiAje01BWExFTn0iCiAg
ICAgICAgICBlbmQKICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgIG1zZyA9ICIiCiAgICAg
ICAgICAgIHBhcnQgPSBuaWwKICAgICAgICAgICAgd2hpbGUgbXNnLmxlbmd0aCA8IGxlbiBh
bmQgKHBhcnQgPSByZWN2KGxlbikpCiAgICAgICAgICAgICAgaWYgcGFydC5sZW5ndGggPT0g
MCAjIyB3aGF0IGNhdXNlcyB0aGlzPwogICAgICAgICAgICAgICAgcmFpc2UgTWVzc2FnZVVu
ZGVyZmxvdywKICAgICAgICAgICAgICAgICAgIlBlZXIgY2xvc2VkIHNvY2tldCBiZWZvcmUg
ZmluaXNoaW5nIG1lc3NhZ2UgLS0iICsKICAgICAgICAgICAgICAgICAgIiByZWNlaXZlZCAj
e21zZy5sZW5ndGh9IG9mICN7bGVufSBieXRlczpcbiIgKwogICAgICAgICAgICAgICAgICBt
c2dbMC4uOTldLnVucGFjaygiSCoiKVswXSArICIuLi4iCiAgICAgICAgICAgICAgZW5kCiAg
ICAgICAgICAgICAgbXNnIDw8IHBhcnQKICAgICAgICAgICAgZW5kCiAgICAgICAgICAgIG1z
Zy5lbXB0eT8gPyBuaWwgOiBtc2cKICAgICAgICAgIGVuZAogICAgICAgIGVuZAogICAgICBl
bmQKICAgIGVuZAogIGVuZAoKICBjbGFzcyBTb2NrZXQgPCBUQ1BTb2NrZXQKICAgIGluY2x1
ZGUgTWVzc2FnZWFibGUKICBlbmQKCiAgY2xhc3MgU2VydmVyIDwgVENQU2VydmVyCiAgICBp
bmNsdWRlIE1lc3NhZ2VhYmxlCgogICAgaWYgZGVmaW5lZD8oRXJybm86OkVQUk9UTykKICAg
ICAgRXByb3RvID0gRXJybm86OkVQUk9UTwogICAgZWxzZQogICAgICBjbGFzcyBFcHJvdG8g
PCBFeGNlcHRpb247IGVuZAogICAgZW5kCgogICAgIyBUaGUgc2FtZSBhcyBUQ1BTZXJ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