Sending YAML over TCP

L

lists

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 :) 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
 
A

Ara.T.Howard

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 :) 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
--
===============================================================================
| 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
===============================================================================
 
B

Brian Schröder

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 :) 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

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 =3D TCPServer.new('127.0.0.1', 8585)

while (session =3D 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 =3D { 'one'=3D>'1', 'two'=3D>'2'}

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

regards,

Brian
 
L

Lyndon Samson

------=_Part_7225_21748492.1128226968425
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

daz

Brian said:
[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
 
B

Brian Schröder

[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
 
L

lists

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top