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
Networking.pptx
Esubesisay
 
PPTX
5_6278455688045789623.pptx
EliasPetros
 
PPT
Md13 networking
Rakesh Madugula
 
PPT
Socket Programming
CEC Landran
 
PPTX
Chapter 4--converted.pptx
WijdenBenothmen1
 
PPTX
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
PPTX
A.java
JahnaviBhagat
 
PPT
Lan chat system
Wipro
 
PPTX
OOP Lecture 24-Network Programming-Part1.pptx
Tanzila Kehkashan
 
PDF
JavaSockets-Session10 New York university.pdf
jose19881
 
PDF
lab04.pdf
SaidiCalala
 
PPT
Net Programming.ppt
EloAcubaOgardo
 
PPT
Network programming in Java
Tushar B Kute
 
PDF
28 networking
Ravindra Rathore
 
PPT
Socket Programming in Java.ppt yeh haii
inambscs4508
 
PPT
Network Prog.ppt
EloOgardo
 
PPT
Network programming-Network for engineering
insdcn
 
PPTX
Basics of Socket Programming using python
NalinadeviKadiresan1
 
PPT
LECTURE-17(Socket Programming) Detailed.
qamarmajeed0000
 
Networking.pptx
Esubesisay
 
5_6278455688045789623.pptx
EliasPetros
 
Md13 networking
Rakesh Madugula
 
Socket Programming
CEC Landran
 
Chapter 4--converted.pptx
WijdenBenothmen1
 
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
Lan chat system
Wipro
 
OOP Lecture 24-Network Programming-Part1.pptx
Tanzila Kehkashan
 
JavaSockets-Session10 New York university.pdf
jose19881
 
lab04.pdf
SaidiCalala
 
Net Programming.ppt
EloAcubaOgardo
 
Network programming in Java
Tushar B Kute
 
28 networking
Ravindra Rathore
 
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Network Prog.ppt
EloOgardo
 
Network programming-Network for engineering
insdcn
 
Basics of Socket Programming using python
NalinadeviKadiresan1
 
LECTURE-17(Socket Programming) Detailed.
qamarmajeed0000
 
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)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
community health nursing question paper 2.pdf
Prince kumar
 
Dimensions of Societal Planning in Commonism
StefanMz
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 

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.