<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: port code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 11:11:18 GMT</pubDate>
    <description>DZone Snippets: port code</description>
    <item>
      <title>Run TCPServer as a simple Web server</title>
      <link>http://snippets.dzone.com/posts/show/5345</link>
      <description>A TCPServer accepts incoming TCP connections. Here is a Web server that listens on a given port and returns the time.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'socket'&lt;br /&gt;port = (ARGV[0] || 80).to_i&lt;br /&gt;server = TCPServer.new('localhost', port)&lt;br /&gt;while (session = server.accept)&lt;br /&gt;  puts "Request: #{session.gets}"&lt;br /&gt;  session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"&lt;br /&gt;  session.print "&lt;html&gt;&lt;body&gt;&lt;h1&gt;#{Time.now}&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;\r\n"&lt;br /&gt;  session.close&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This code was copied from &lt;a href="http://www.rubycentral.com/pickaxe/lib_network.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide&lt;/a&gt; [rubycentral.com] while looking for information on Ruby CGI global variables.</description>
      <pubDate>Thu, 10 Apr 2008 14:05:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5345</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Introduction to Distributed Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5233</link>
      <description>This code demonstrates a client server architecture. I executed the file simple_service.rb on my Ubuntu server (Donatello - 192.168.1.10), then from the CLI output I copied the server uri into the clipboard. I then executed the simple_client.rb on my Ubuntu desktop (Cryton - 192.168.1.3) while passing in the uri as an argument. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;# simple_service.rb&lt;br /&gt;# A simple DRb service&lt;br /&gt;&lt;br /&gt;# load DRb&lt;br /&gt;require 'drb'&lt;br /&gt;&lt;br /&gt;# start up the DRb service&lt;br /&gt;DRb.start_service nil, []&lt;br /&gt;&lt;br /&gt;# We need the uri of the service to connect a client&lt;br /&gt;puts DRb.uri&lt;br /&gt;&lt;br /&gt;# wait for the DRb service to finish before exiting&lt;br /&gt;DRb.thread.join&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output: druby://donatello.mydomain.com:47159&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby -w&lt;br /&gt;# simple_client.rb&lt;br /&gt;# A simple DRb client&lt;br /&gt;&lt;br /&gt;require 'drb'&lt;br /&gt;&lt;br /&gt;DRb.start_service&lt;br /&gt;&lt;br /&gt;# attach to the DRb server via a URI given on the command line&lt;br /&gt;remote_array = DRbObject.new nil, ARGV.shift&lt;br /&gt;&lt;br /&gt;puts remote_array.size&lt;br /&gt;&lt;br /&gt;remote_array &lt;&lt; 1&lt;br /&gt;&lt;br /&gt;puts remote_array.size&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;from the command line&lt;br /&gt;&gt; ./simple_client.rb druby://192.168.1.10:47159&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;output:&lt;br /&gt;0&lt;br /&gt;1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note: I substituted the domain name with the ip address because the name in question was not stored within the DNS settings.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://segment7.net/projects/ruby/drb/introduction.html"&gt;Introduction to Distributed Ruby (DRb)&lt;/a&gt; [segment7.net]</description>
      <pubDate>Sat, 15 Mar 2008 00:59:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5233</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Send custom UDP packets in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4541</link>
      <description>&lt;br /&gt;From: http://www.ruby-forum.com/topic/124159&lt;br /&gt;Author: Bill Kelly&lt;br /&gt;&lt;br /&gt;For yet another nifty UDP snippet see &lt;a href="http://www.rubyinside.com/skype-style-firewall-busting-with-ruby-and-udp-399.html"&gt;Skype-Style Firewall Busting with Ruby and UDP&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;require 'socket'&lt;br /&gt;&lt;br /&gt;#abort "Usage: server_addr, server_port, cmd_str" unless ARGV.length == 3&lt;br /&gt;&lt;br /&gt;UDP_RECV_TIMEOUT = 3  # seconds&lt;br /&gt;&lt;br /&gt;def q2cmd(server_addr, server_port, cmd_str)&lt;br /&gt;  resp, sock = nil, nil&lt;br /&gt;  begin&lt;br /&gt;   cmd = "\377\377\377\377#{cmd_str}\0"&lt;br /&gt;    sock = UDPSocket.open&lt;br /&gt;    sock.send(cmd, 0, server_addr, server_port)&lt;br /&gt;    resp = if select([sock], nil, nil, UDP_RECV_TIMEOUT)&lt;br /&gt;      sock.recvfrom(65536)&lt;br /&gt;    end&lt;br /&gt;    if resp&lt;br /&gt;      resp[0] = resp[0][4..-1]  # trim leading 0xffffffff&lt;br /&gt;    end&lt;br /&gt;  rescue IOError, SystemCallError&lt;br /&gt;  ensure&lt;br /&gt;    sock.close if sock&lt;br /&gt;  end&lt;br /&gt;  resp ? resp[0] : nil&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# your firewall has to allow communication with IP address 67.19.248.74 (port 27912)&lt;br /&gt;#server, port, cmd = *ARGV&lt;br /&gt;server = "tastyspleen.net"&lt;br /&gt;port = 27912&lt;br /&gt;cmd = "status"&lt;br /&gt;&lt;br /&gt;result = q2cmd(server, port, cmd)&lt;br /&gt;puts result&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 14 Sep 2007 11:19:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4541</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>J2ME - getIPdevice</title>
      <link>http://snippets.dzone.com/posts/show/3592</link>
      <description>// Retrevie IP device &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package org.socketdemo;&lt;br /&gt;&lt;br /&gt;import javax.microedition.io.Connector;&lt;br /&gt;import javax.microedition.io.SocketConnection;&lt;br /&gt;import javax.microedition.lcdui.Alert;&lt;br /&gt;import javax.microedition.lcdui.AlertType;&lt;br /&gt;import javax.microedition.lcdui.Command;&lt;br /&gt;import javax.microedition.lcdui.CommandListener;&lt;br /&gt;import javax.microedition.lcdui.Display;&lt;br /&gt;import javax.microedition.lcdui.Displayable;&lt;br /&gt;import javax.microedition.midlet.MIDlet;&lt;br /&gt;import javax.microedition.midlet.MIDletStateChangeException;&lt;br /&gt;&lt;br /&gt;public class SocketDEMO extends MIDlet implements CommandListener&lt;br /&gt;{&lt;br /&gt;	protected SocketDEMO midlet = this;&lt;br /&gt;	&lt;br /&gt;	private Alert info;&lt;br /&gt;	&lt;br /&gt;	protected void destroyApp(boolean value) throws MIDletStateChangeException&lt;br /&gt;	{&lt;br /&gt;		notifyDestroyed();&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	protected void pauseApp()&lt;br /&gt;	{&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	protected void startApp() throws MIDletStateChangeException&lt;br /&gt;	{&lt;br /&gt;		new Thread()&lt;br /&gt;		{&lt;br /&gt;			public void run()&lt;br /&gt;			{&lt;br /&gt;				SocketConnection socket = null;&lt;br /&gt;				&lt;br /&gt;				try&lt;br /&gt;				{&lt;br /&gt;					socket = (SocketConnection) Connector.open("socket://193.204.114.233:13");&lt;br /&gt;					&lt;br /&gt;					socket.openInputStream();&lt;br /&gt;					&lt;br /&gt;					info = new Alert("Info", "Current IP: " + socket.getLocalAddress() + "\nPort: " + socket.getLocalPort(), null, AlertType.INFO);&lt;br /&gt;					info.setTimeout(Alert.FOREVER);&lt;br /&gt;					info.setCommandListener(midlet);&lt;br /&gt;					&lt;br /&gt;					getDisplay().setCurrent(info);&lt;br /&gt;				}&lt;br /&gt;				catch(Exception error)&lt;br /&gt;				{&lt;br /&gt;					info = new Alert("Info", "Current IP: N/A\nPort: N/A", null, AlertType.INFO);&lt;br /&gt;					info.setTimeout(Alert.FOREVER);&lt;br /&gt;					info.setCommandListener(midlet);&lt;br /&gt;					&lt;br /&gt;					getDisplay().setCurrent(info);&lt;br /&gt;				}&lt;br /&gt;				finally&lt;br /&gt;				{&lt;br /&gt;					if(socket != null)&lt;br /&gt;					{&lt;br /&gt;						try&lt;br /&gt;						{&lt;br /&gt;							socket.close();&lt;br /&gt;						}&lt;br /&gt;						catch(Exception error)&lt;br /&gt;						{&lt;br /&gt;							&lt;br /&gt;						}&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;		}.start();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	protected Display getDisplay()&lt;br /&gt;	{&lt;br /&gt;		return Display.getDisplay(this);&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public void commandAction(Command cmd, Displayable dsp)&lt;br /&gt;	{&lt;br /&gt;		if(cmd == Alert.DISMISS_COMMAND)&lt;br /&gt;		{&lt;br /&gt;			try&lt;br /&gt;			{&lt;br /&gt;				destroyApp(true);&lt;br /&gt;			}&lt;br /&gt;			catch(MIDletStateChangeException error)&lt;br /&gt;			{&lt;br /&gt;			&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Feb 2007 00:15:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3592</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Apache2 proxy to local port</title>
      <link>http://snippets.dzone.com/posts/show/1318</link>
      <description>Apache as the receptionist, forwarding requests to and from an internal server (e.g. webrick or lighttpd).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;VirtualHost *&gt;&lt;br /&gt;        ServerName www.example.com&lt;br /&gt;        ProxyPass / http://localhost:3000/&lt;br /&gt;        ProxyPassReverse / http://localhost:3000/&lt;br /&gt;        ProxyPreserveHost On&lt;br /&gt;&lt;/VirtualHost&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Change 3000 to whatever port you need and make sure the internal server is set up to answer requests on that port (not port 80). The ProxyPreserveHost line is critical to keep all your URLs working correctly.</description>
      <pubDate>Tue, 31 Jan 2006 11:09:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1318</guid>
      <author>brainpipe ()</author>
    </item>
  </channel>
</rss>
