Ruby/Basic networking

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Basic Networking and web programming

Basic networking concepts

client - the browser you use for example

server - the program installed on another computer that "servers" requests to your browser

The servers identifies the client by the IP of the computer where the client resides.the IP address is a set of 4 numbers, separated by dots, some IP addresses are reserved for special purposes.

Here's a very common IP address : 127.0.0.1 , the reason why it's so common is because it's meaning is "local machine" and can be used to access program on your own computer.

For example, if you were to install a web server on your computer, you'd be able to access any site you install in the default directory by simply typing 127.0.0.1 in your browser's address bar.

You don't have to remember IP addresses of each website you want to visit...because DNS exists.What DNS does is allow people to use addresses composed of letters and/or numbers instead of having to memorize a bunch of nonsense numbers. DNS "translates" a domain address into the corresponding IP address.

Try this:

type www.google.com in your browser's address bar.

now type http://64.233.187.99/

You'll notice you get Google in English returned in exchange.So this means that www.google.com and 64.233.187.99 essentially mean the same thing.

Servers accepts connections on a specific port number. The default port number for http is 80. There is much to be said about networking in general, but this is the minimum you should know before reading what comes next.

You can write programs for different web servers, such as Apache, or the Ruby server WEBrick or Mongrel for example.Check the server dedicated chapter for this.

You should also know that applications used 2 main types of ports for communicating back and forth with a server, TCP and UDP ports.When large amounts of data needs to be transfered, the TCP port is used for persistent connections.

You should also get yourself familiar with the concept of sockets

The TCPServer, TCPSocket Classes

Usually used when a server wants to accepts connections.We use either the address of the local host of the server or the corresponding domain such as www.domain.com, and the port that accepts connections.It's important to mention that TCPSocket objects work like IO objects.

Check this code:

require ‘socket’
server = TCPServer.new(127.0.0.1”, 80)
loop do
socket = server.accept
while socket.gets.chop.length > 0
end
socket.puts “HTTP/1.1 200 OK”
socket.puts “Content-type: text/html”
socket.puts “”
socket.puts “<html>”
socket.puts “<body>”
socket.puts “<div>”
socket.puts “<b>Yay!Your first networking application!</b>”
socket.puts “</div>”
socket.puts “</body>”
socket.puts “</html>”
socket.close
end

This builds a very basic html page that will output "Your first networking application!" each time it's accessed.

Each time the loop is executed, a new TCPSocket instance connects to the client program. The request is then processed and the socket closed.When the end of the headers are reached, new ones are sent to indicate the server can handle the request and that data will be sent in HTML.After sending the data, the socket is closed.

There is much to learn about Ruby's networking capabilities or what sort of applications can Rails help you to code, but we'll stick to the minimum basics for now.DRb,SOAP,XML-RPC are subjects you might be interested in if you want to take things further.

How to access webpages with Net::HTTP

Why would you want to access a site through other means than a browser? If you've though of content scraping for example...good answer! Let's take a look at a very simple script that connects to a site, downloads and outputs the index of the site.

require ‘net/http’
mainp = Net::HTTP.new(“www.domain.tld”, 80)
response, content = mainp.get(“index.html”, nil)
puts content


Previous Next
Personal tools