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