SlideShare a Scribd company logo
Deepak H B
Full-Stack Developer
Gmail: deepakhb2@gmail.com
Linkedin: http://in.linkedin.com/in/deepakhb
GitHub: https://github.com/deepakhb2
Networking
Introduction
• Ruby s networking is provided by the standard libraries
rather than by core classes.
• At the lowest level, networking is accomplished with
socket, which are a kind of IO object.
• Once you have a socket opened, you can read data from
or write data to another computer just as if you were
reading or writing to the file.
• All socket classes are part of the standard library, so to
use them in your program you must first:
require ‘socket’
A very simple client
• To write Internet client application use the TCPSocket class.
• Obtain a TCPSocket instance with the TCPSocket.open class method or
with its synonym TCPSocket.new.
• Pass the name of the host to connect as first argument and the port as
second argument.
• Different internet protocols use different ports. Web service use port 80 as
default.
require ‘socket’
host, port = ARGV
s= TCPSocket.open(host, port)
while line = s.gets
puts line.chop
end
s.Close
• Like File.open, the TCPSocket.open method can be invoked with a block.
• Socket will automatically closed when the block returns.
A very simple server
• To write internet servers, we use the TCPServer class.
• Call TCPServer.open to specify the port for your service and create a
TCPServer object.
• Next, call the accept method of the returned TCPServer object. This
method waits until a client connects to the port you specified and
then returns a TCPSocket object that connection to that client.
require ‘socket’
server = TCPServer.open(2000)
loop {
client = server.accept
client.puts(Time.now.ctime)
client.close
end
Datagrams
• Most internet protocols are implemented using
TCPSocket and TCPServer as shown earlier.
• A lower overhead alternative is to use UDP datagrams
with the UDPSocket class.
• UDP allows computers to send individual packets of
data to other computer without the overhead of
establishing a persistent connection.
• The client sends a datagram containing a string of text
to a specified host and port.
Contd..
• The server, which should be running on that host and listening on
that port, receives the text, converts it to upper case and sends it
back in a second datagram.
• Datagrams are pretty different from other IO streams.
• The argument to recvfrom specifies the maximum amount of data
we are interested in receiving.
require 'socket' # Standard library
host, port, request = ARGV # Get args from command line
ds = UDPSocket.new # Create datagram socket
ds.connect(host, port) # Connect to the port on the host
ds.send(request, 0) # Send the request text
response,address = ds.recvfrom(1024) # Wait for a response
(1kb max)
puts response
Contd..
• The server code uses the UDPSocket class just as the client
does, there is no special UDPServer class for datagram-based
servers.
• Instead of calling connect to connect the socket, our server
calls bind to tell the socket what port to listen on.
• The server then uses send and resvfrom just as the client
does, but in the opposite order.
• It calls recvffrom to wait until it receives a datagram on the
specified port. When that happens it converts the text it
receives to upcase and sends it back.
• The recvfrom method returns two values. The first is the
received data and second is the array containing information
about where the data came from.
Contd..
require 'socket' # Standard library
port = ARGV[0] # The port to listen on
ds = UDPSocket.new # Create new socket
ds.bind(nil, port) # Make it listen on the port
loop do # Loop forever
request,address=ds.recvfrom(1024) # Wait to receive something
response = request.upcase # Convert request text to
uppercase
clientaddr = address[3] # What ip address sent the request?
clientname = address[2] # What is the host name?
clientport = address[1] # What port was it sent from
ds.send(response, 0, # Send the response back...
clientaddr, clientport) # ...where it came from
# Log the client connection
puts "Connection from: #{clientname} #{clientaddr} #{clientport}"
end
A Multiplexing Server
• The simple time server shown earlier never maintained a
connection with any client.
• The sophisticated servers maintain a connection and in order to
be useful they must allow multiple clients to connect and interact
at the same time.
• One way to do this is with threads.
• The alternate that we’ll consider here is to write a multiplexing
server using the Kernel.select method.
• When a server has multiple clients connected, it cannot call a
blocking method like gets on the socket of any one client.
• If it blocks waiting for input from one client, it won’t be able to
receive input from other clients or accept new connection.
• The select method solves this problem.
Fetching Web Pages
• We can use the socket liberary to implement any internet
protocol.
• We can use a prebuilt library like Net::HTTP for working
with HTTP.
• Similar libraries exist for working with the FTP, SMTP, POP
and IMAP.

More Related Content

What's hot (20)

PPTX
Python Sockets
pythontic
 
PPT
Socket programming-tutorial-sk
sureshkarthick37
 
PDF
Computer network (4)
NYversity
 
PPT
Networking & Socket Programming In Java
Ankur Agrawal
 
PDF
Socket programming-in-python
Yuvaraja Ravi
 
PDF
What is Socket Programming in Python | Edureka
Edureka!
 
PDF
Socket Programming
elliando dias
 
PDF
Tcpdump
Mohamed Gamel
 
PPT
Tcp sockets
babak danyal
 
ODP
Building Netty Servers
Dani Solà Lagares
 
PPT
java networking
Waheed Warraich
 
PDF
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 
PPTX
Networking in Java
Tushar B Kute
 
PDF
gRPC in Go
Almog Baku
 
PPTX
Advance Java-Network Programming
ashok hirpara
 
PDF
OSMC 2018 | Handling messages and notifications from software and gadgets wit...
NETWAYS
 
PPTX
Network programming in java - PPT
kamal kotecha
 
PDF
Netcat cheat sheet_v1
aioudshflaksjdvhv
 
PPT
Netty 4-based RPC System Development
Allan Huang
 
PDF
Netty @Apple: Large Scale Deployment/Connectivity
C4Media
 
Python Sockets
pythontic
 
Socket programming-tutorial-sk
sureshkarthick37
 
Computer network (4)
NYversity
 
Networking & Socket Programming In Java
Ankur Agrawal
 
Socket programming-in-python
Yuvaraja Ravi
 
What is Socket Programming in Python | Edureka
Edureka!
 
Socket Programming
elliando dias
 
Tcpdump
Mohamed Gamel
 
Tcp sockets
babak danyal
 
Building Netty Servers
Dani Solà Lagares
 
java networking
Waheed Warraich
 
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 
Networking in Java
Tushar B Kute
 
gRPC in Go
Almog Baku
 
Advance Java-Network Programming
ashok hirpara
 
OSMC 2018 | Handling messages and notifications from software and gadgets wit...
NETWAYS
 
Network programming in java - PPT
kamal kotecha
 
Netcat cheat sheet_v1
aioudshflaksjdvhv
 
Netty 4-based RPC System Development
Allan Huang
 
Netty @Apple: Large Scale Deployment/Connectivity
C4Media
 

Similar to 10 Networking (20)

PPTX
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
PDF
l-rubysocks-a4
tutorialsruby
 
PDF
l-rubysocks-a4
tutorialsruby
 
PPTX
Socket programming
Anurag Tomar
 
KEY
Servers with Event Machine - David Troy - RailsConf 2011
David Troy
 
PDF
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
KEY
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
Eleanor McHugh
 
PDF
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
anuradhasilks
 
PPTX
Networking in Python2025 (programs allll)
PriyankaPatil919748
 
PDF
Socket programming using java
UC San Diego
 
PDF
Anchoring Trust: Rewriting DNS for the Semantic Network with Ruby and Rails
Eleanor McHugh
 
KEY
DRb and Rinda
Mark
 
DOCX
Final networks lab manual
Jaya Prasanna
 
PPT
Sockets
Gopaiah Sanaka
 
DOCX
Udp socket programming(Florian)
Flor Ian
 
PPT
Application Layer and Socket Programming
elliando dias
 
PDF
Python lecture 11
Tanwir Zaman
 
PPT
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
PDF
Socket programming assignment
Ravi Gupta
 
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
l-rubysocks-a4
tutorialsruby
 
l-rubysocks-a4
tutorialsruby
 
Socket programming
Anurag Tomar
 
Servers with Event Machine - David Troy - RailsConf 2011
David Troy
 
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
The Ruby Guide to *nix Plumbing: on the quest for efficiency with Ruby [M|K]RI
Eleanor McHugh
 
Of the variedtypes of IPC, sockets arout and awaythe foremostcommon..pdf
anuradhasilks
 
Networking in Python2025 (programs allll)
PriyankaPatil919748
 
Socket programming using java
UC San Diego
 
Anchoring Trust: Rewriting DNS for the Semantic Network with Ruby and Rails
Eleanor McHugh
 
DRb and Rinda
Mark
 
Final networks lab manual
Jaya Prasanna
 
Sockets
Gopaiah Sanaka
 
Udp socket programming(Florian)
Flor Ian
 
Application Layer and Socket Programming
elliando dias
 
Python lecture 11
Tanwir Zaman
 
Introduction to sockets tcp ip protocol.ppt
MajedAboubennah
 
Socket programming assignment
Ravi Gupta
 
Ad

More from Deepak Hagadur Bheemaraju (10)

PPTX
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
PPTX
11 Ruby Gems
Deepak Hagadur Bheemaraju
 
PPTX
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
PPTX
8 Exception Handling
Deepak Hagadur Bheemaraju
 
PPTX
7 Methods and Functional Programming
Deepak Hagadur Bheemaraju
 
PPTX
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
PPTX
5 Statements and Control Structures
Deepak Hagadur Bheemaraju
 
PPTX
4 Expressions and Operators
Deepak Hagadur Bheemaraju
 
PPTX
3 Datatypes
Deepak Hagadur Bheemaraju
 
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
9 Inputs & Outputs
Deepak Hagadur Bheemaraju
 
8 Exception Handling
Deepak Hagadur Bheemaraju
 
7 Methods and Functional Programming
Deepak Hagadur Bheemaraju
 
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
5 Statements and Control Structures
Deepak Hagadur Bheemaraju
 
4 Expressions and Operators
Deepak Hagadur Bheemaraju
 
Ad

Recently uploaded (20)

PPTX
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PPTX
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
How to Manage Expiry Date in Odoo 18 Inventory
Celine George
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
AIMA UCSC-SV Leadership_in_the_AI_era 20250628 v16.pptx
home
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Vietnam Street Food & QSR Market 2025-1.pdf
ssuserec8cd0
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
epi editorial commitee meeting presentation
MIPLM
 
infertility, types,causes, impact, and management
Ritu480198
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 

10 Networking

  • 1. Deepak H B Full-Stack Developer Gmail: [email protected] Linkedin: http://in.linkedin.com/in/deepakhb GitHub: https://github.com/deepakhb2
  • 3. Introduction • Ruby s networking is provided by the standard libraries rather than by core classes. • At the lowest level, networking is accomplished with socket, which are a kind of IO object. • Once you have a socket opened, you can read data from or write data to another computer just as if you were reading or writing to the file. • All socket classes are part of the standard library, so to use them in your program you must first: require ‘socket’
  • 4. A very simple client • To write Internet client application use the TCPSocket class. • Obtain a TCPSocket instance with the TCPSocket.open class method or with its synonym TCPSocket.new. • Pass the name of the host to connect as first argument and the port as second argument. • Different internet protocols use different ports. Web service use port 80 as default. require ‘socket’ host, port = ARGV s= TCPSocket.open(host, port) while line = s.gets puts line.chop end s.Close • Like File.open, the TCPSocket.open method can be invoked with a block. • Socket will automatically closed when the block returns.
  • 5. A very simple server • To write internet servers, we use the TCPServer class. • Call TCPServer.open to specify the port for your service and create a TCPServer object. • Next, call the accept method of the returned TCPServer object. This method waits until a client connects to the port you specified and then returns a TCPSocket object that connection to that client. require ‘socket’ server = TCPServer.open(2000) loop { client = server.accept client.puts(Time.now.ctime) client.close end
  • 6. Datagrams • Most internet protocols are implemented using TCPSocket and TCPServer as shown earlier. • A lower overhead alternative is to use UDP datagrams with the UDPSocket class. • UDP allows computers to send individual packets of data to other computer without the overhead of establishing a persistent connection. • The client sends a datagram containing a string of text to a specified host and port.
  • 7. Contd.. • The server, which should be running on that host and listening on that port, receives the text, converts it to upper case and sends it back in a second datagram. • Datagrams are pretty different from other IO streams. • The argument to recvfrom specifies the maximum amount of data we are interested in receiving. require 'socket' # Standard library host, port, request = ARGV # Get args from command line ds = UDPSocket.new # Create datagram socket ds.connect(host, port) # Connect to the port on the host ds.send(request, 0) # Send the request text response,address = ds.recvfrom(1024) # Wait for a response (1kb max) puts response
  • 8. Contd.. • The server code uses the UDPSocket class just as the client does, there is no special UDPServer class for datagram-based servers. • Instead of calling connect to connect the socket, our server calls bind to tell the socket what port to listen on. • The server then uses send and resvfrom just as the client does, but in the opposite order. • It calls recvffrom to wait until it receives a datagram on the specified port. When that happens it converts the text it receives to upcase and sends it back. • The recvfrom method returns two values. The first is the received data and second is the array containing information about where the data came from.
  • 9. Contd.. require 'socket' # Standard library port = ARGV[0] # The port to listen on ds = UDPSocket.new # Create new socket ds.bind(nil, port) # Make it listen on the port loop do # Loop forever request,address=ds.recvfrom(1024) # Wait to receive something response = request.upcase # Convert request text to uppercase clientaddr = address[3] # What ip address sent the request? clientname = address[2] # What is the host name? clientport = address[1] # What port was it sent from ds.send(response, 0, # Send the response back... clientaddr, clientport) # ...where it came from # Log the client connection puts "Connection from: #{clientname} #{clientaddr} #{clientport}" end
  • 10. A Multiplexing Server • The simple time server shown earlier never maintained a connection with any client. • The sophisticated servers maintain a connection and in order to be useful they must allow multiple clients to connect and interact at the same time. • One way to do this is with threads. • The alternate that we’ll consider here is to write a multiplexing server using the Kernel.select method. • When a server has multiple clients connected, it cannot call a blocking method like gets on the socket of any one client. • If it blocks waiting for input from one client, it won’t be able to receive input from other clients or accept new connection. • The select method solves this problem.
  • 11. Fetching Web Pages • We can use the socket liberary to implement any internet protocol. • We can use a prebuilt library like Net::HTTP for working with HTTP. • Similar libraries exist for working with the FTP, SMTP, POP and IMAP.