Sending YAML over TCP

Hi,
     I'm trying to send simple data structures (hashes) to a daemon process one one machine from a client process via TCP. I thought the best way to do this would be with XML or YAML. Here's my simple server:

···

---
require 'socket'

server = TCPServer.new('127.0.0.1', 8080)

while (session = server.accept)
   session.puts session.gets
   session.close
end
---

Here's my simple client:

---
require 'socket'
require 'yaml'
require 'base64'

hash = { 'one'=>'1', 'two'=>'2'} # Send this to the server and get it back

client = TCPSocket.new('127.0.0.1', 8080)
client.puts Base64.encode64(h.to_yaml)
hash = YAML.load( Base64.decode64(client.gets) )
client.close
p hash
---

Obviously I don't know what I'm doing :slight_smile: The above client/server works for my simple purposes, but I wanted to know if there's a better way to pass hashes from the client to the server and back. I'd prefer to stick with YAML (rather than XML) if possible.

Thanks,
Ryan

why not drb - then you can pass any object and needed bother with yaml/xml,
etc. ??

-a

···

On Sun, 2 Oct 2005, lists wrote:

Hi,
   I'm trying to send simple data structures (hashes) to a daemon process one one machine from a client process via TCP. I thought the best way to do this would be with XML or YAML. Here's my simple server:

---
require 'socket'

server = TCPServer.new('127.0.0.1', 8080)

while (session = server.accept)
session.puts session.gets
session.close
end
---

Here's my simple client:

---
require 'socket'
require 'yaml'
require 'base64'

hash = { 'one'=>'1', 'two'=>'2'} # Send this to the server and get it back

client = TCPSocket.new('127.0.0.1', 8080)
client.puts Base64.encode64(h.to_yaml)
hash = YAML.load( Base64.decode64(client.gets) )
client.close
p hash
---

Obviously I don't know what I'm doing :slight_smile: The above client/server works for my simple purposes, but I wanted to know if there's a better way to pass hashes from the client to the server and back. I'd prefer to stick with YAML (rather than XML) if possible.

Thanks,
Ryan

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

Hello Ryan,

this works, but maybe drb is better suited for your needs:

bschroed@black:~/svn/projekte/yamlserver$ cat server.rb
require 'socket'
require 'yaml'

server = TCPServer.new('127.0.0.1', 8585)

while (session = server.accept)
  Thread.new(session) do | s |
    p YAML::load(s)
    s.close
  end
end
bschroed@black:~/svn/projekte/yamlserver$ cat client.rb
require 'socket'
require 'yaml'

hash = { 'one'=>'1', 'two'=>'2'}

client = TCPSocket.new('127.0.0.1', 8585)
client.puts hash.to_yaml
client.close

regards,

Brian

···

On 02/10/05, lists <lists@kalama.no-ip.org> wrote:

Hi,
     I'm trying to send simple data structures (hashes) to a daemon
process one one machine from a client process via TCP. I thought the
best way to do this would be with XML or YAML. Here's my simple server:

[snip code]

Obviously I don't know what I'm doing :slight_smile: The above client/server
works for my simple purposes, but I wanted to know if there's a
better way to pass hashes from the client to the server and back.
I'd prefer to stick with YAML (rather than XML) if possible.

Thanks,
Ryan

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Will all the clients be based in Ruby? If so, you might look at DRb.

http://segment7.net/projects/ruby/drb/introduction.html

-Robby

···

On Sun, 2005-10-02 at 08:23 +0900, lists wrote:

Hi,
     I'm trying to send simple data structures (hashes) to a daemon
process one one machine from a client process via TCP. I thought the
best way to do this would be with XML or YAML. Here's my simple server:

---
require 'socket'

server = TCPServer.new('127.0.0.1', 8080)

while (session = server.accept)
   session.puts session.gets
   session.close
end
---

Here's my simple client:

---
require 'socket'
require 'yaml'
require 'base64'

hash = { 'one'=>'1', 'two'=>'2'} # Send this to the server and get it
back

client = TCPSocket.new('127.0.0.1', 8080)
client.puts Base64.encode64(h.to_yaml)
hash = YAML.load( Base64.decode64(client.gets) )
client.close
p hash
---

Obviously I don't know what I'm doing :slight_smile: The above client/server
works for my simple purposes, but I wanted to know if there's a
better way to pass hashes from the client to the server and back.
I'd prefer to stick with YAML (rather than XML) if possible.

Thanks,
Ryan

--
/******************************************************
* Robby Russell, Owner.Developer.Geek
* PLANET ARGON, Open Source Solutions & Web Hosting
* Portland, Oregon | p: 503.351.4730 | f: 815.642.4068
* www.planetargon.com | www.robbyonrails.com
* Programming Rails | www.programmingrails.com
*******************************************************/

You could use webrick. More overhead, but HTTP is a well understood
protocol.

···

--
Into RFID? www.rfidnewsupdate.com <http://www.rfidnewsupdate.com> Simple,
fast, news.

Wow, I didn't know about drb. That does indeed do what I want more elegantly. Brian, I'm keeping you're example of sending YAML in case I need it for a different project. Thanks to all who replied.

-Ryan

···

On Oct 1, 2005, at 6:23 PM, lists wrote:

Hi,
    I'm trying to send simple data structures (hashes) to a daemon process one one machine from a client process via TCP. I thought the best way to do this would be with XML or YAML. Here's my simple server:

---
require 'socket'

server = TCPServer.new('127.0.0.1', 8080)

while (session = server.accept)
  session.puts session.gets
  session.close
end
---

Here's my simple client:

---
require 'socket'
require 'yaml'
require 'base64'

hash = { 'one'=>'1', 'two'=>'2'} # Send this to the server and get it back

client = TCPSocket.new('127.0.0.1', 8080)
client.puts Base64.encode64(h.to_yaml)
hash = YAML.load( Base64.decode64(client.gets) )
client.close
p hash
---

Obviously I don't know what I'm doing :slight_smile: The above client/server works for my simple purposes, but I wanted to know if there's a better way to pass hashes from the client to the server and back. I'd prefer to stick with YAML (rather than XML) if possible.

Thanks,
Ryan

Brian Schröder wrote:

[YAML client/server code]

Hi Brian,

While looking for something else, this caught my attention in:
http://yaml4r.sourceforge.net/doc/page/loading_yaml_documents.htm

"YAML::load_documents is the most efficient way to load streaming data.
This applies as well to TCP sockets. Client/server applications which
communcate in YAML can pass the TCPSocket object directly to
YAML::load_documents for parsing a stream over TCP/IP."

I don't know if that has any relevance to your example (?).
- If not, please ignore.

daz

Hello daz,

thanks for the pointer. I have no idea if it is applicable in this
case. Just did a quick hack to show how sockets work, but I have to
admit I have never even used yaml.

regards,

Brian

···

On 02/10/05, daz <dooby@d10.karoo.co.uk> wrote:

Brian Schröder wrote:
>
> [YAML client/server code]

Hi Brian,

While looking for something else, this caught my attention in:
http://yaml4r.sourceforge.net/doc/page/loading_yaml_documents.htm

"YAML::load_documents is the most efficient way to load streaming data.
This applies as well to TCP sockets. Client/server applications which
communcate in YAML can pass the TCPSocket object directly to
YAML::load_documents for parsing a stream over TCP/IP."

I don't know if that has any relevance to your example (?).
- If not, please ignore.

daz

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/