<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: TCPServer code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 10 Oct 2008 08:54:43 GMT</pubDate>
    <description>DZone Snippets: TCPServer code</description>
    <item>
      <title>Post a message to identi.ca using XMPP via HTTP</title>
      <link>http://snippets.dzone.com/posts/show/5739</link>
      <description>This Ruby code posts a message to identi.ca using XMPP which uses a simple web server.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;require 'rubygems'  &lt;br /&gt;require 'socket'&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'xmpp4r-simple'&lt;br /&gt;&lt;br /&gt;server = TCPServer.new('127.0.0.1', 9090)&lt;br /&gt;messenger = Jabber::Simple.new('my-bot@gmail.com', "bot-password")&lt;br /&gt;&lt;br /&gt;while (session = server.accept)&lt;br /&gt;  request = session.gets&lt;br /&gt;  project = request[/(\w+)\?msg=(.*) HTTP\/1.1/,1]&lt;br /&gt;  msg = URI.unescape($2)&lt;br /&gt;&lt;br /&gt;  puts request&lt;br /&gt;  session.print "HTTP/1.1 200/OK\rContent-type: text/html\r\n\r\n"&lt;br /&gt;  session.print "&lt;html&gt;&lt;head&gt;&lt;title&gt;Response from Jeeves&lt;/title&gt;&lt;/head&gt;\r\n"&lt;br /&gt;  session.print "&lt;body&gt;"&lt;br /&gt;&lt;br /&gt;  if project != '' and msg != '' then&lt;br /&gt;    case project&lt;br /&gt;      when 'identica'&lt;br /&gt;        messenger.deliver("update@identi.ca", msg)&lt;br /&gt;    else&lt;br /&gt;      puts "Project #{project} wasn't found on this server"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  session.print URI.unescape(request)&lt;br /&gt;&lt;br /&gt;  session.print "&lt;/body&gt;&lt;/html&gt;"&lt;br /&gt;  session.close&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To post the message you would type your message within the browser URL e.g. http://127.0.0.1:9090/identica?msg=just%20testing%20xmpp4r-simple%20from%20tcpserver1&lt;br /&gt;</description>
      <pubDate>Tue, 08 Jul 2008 20:03:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5739</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>One-line web server in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5424</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;br /&gt;# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver&lt;br /&gt;# Author: Michael Neumann&lt;br /&gt;# ... point your browser to http://localhost:3125/etc/motd&lt;br /&gt;&lt;br /&gt;ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_&lt;&lt;"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 23 Apr 2008 13:22:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5424</guid>
      <author>ntk ()</author>
    </item>
    <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>
  </channel>
</rss>
