CII3D4
SISTEM PARALEL DAN TERDISTRIBUSI
Materi 5:
Socket Programming
1
What is socket?
An Interface between application and network
• The application create socket
• The socket type dictates the style of communication
– reliable vs best effort
– connection oriented vs conectionless
Once configured, the application can
• Pass data to the socket for network transmission
• Receive data from the socket (transmitted through the
network by some other host)
2
Layering Makes it Easier
• Application programmer
– Doesn’t need to send IP packets
– Doesn’t need to send Ethernet frames
– Doesn’t need to know how TCP implements
reliability
• Only need a way to pass the data down
– Socket is the API to access transport layer
functions
3
Identify the Destination
• Addressing
– IP address
– hostname (resolve to IP address via DNS)
• Multiplexing
– port Server socket address
208.216.181.15:80
Client socket address
128.2.194.242:3479 FTP Server
(port 21)
Client HTTP Server
Connection socket pair (port 80)
(128.2.194.242:3479, 208.216.181.15:80)
Client host address Server host address
128.2.194.242 208.216.181.15
4
How to Use Sockets
• Setup socket
– Where is the remote machine (IP address, hostname)
– What service gets the data (port)
• Send and Receive
– send -- write
– recv -- read
• Close the socket
5
Two essential types of socket
SOCK_STREAM SOCK_DGRAM
- TCP - UDP
- reliable delivery - unreliable delivery
- in order guaranteed - no order guarantees
- connection oriented - connectionless
6
Middleware layers
7 CII3D4 – Sistem Paralel dan Terdistribusi
SOCKET PROGRAMMING WITH
PYTHON
8
Python Socket Module
9
Socket Module Python
Class method Description
Socket Low-level networking interface (import)
socket.socket(family, type) Create and return a new socket object
Convert a string quad dotted IP address
socket.getfqdn(name)
to a fully qualified domain name
socket.gethostbyname(hostname) Resolve a hostname to a string quad
dotted IP address
Create a socket object from an existing
socket.fromfd(fd, family, type)
file descriptor
10
sock.bind( (adrs, port) ) Bind the socket to the address and port
sock.accept() Return a client socket (with peer address
information)
Place the socket into the listening state, able
sock.listen(backlog) to pend backlogoutstanding connection
requests
Connect the socket to the defined host and
sock.connect( (adrs, port) )
port
Receive data from the socket, up
sock.recv( buflen[, flags] )
to buflen bytes
11
Receive data from the socket, up
sock.recvfrom( buflen[, flags] ) to buflen bytes, returning also the remote
host and port from which the data came
sock.send( data[, flags] ) Send the data through the socket
sock.sendto( data[, flags], addr ) Send the data through the socket
sock.close() Close the socket
sock.getsockopt( lvl, optname ) Get the value for the specified socket option
sock.setsockopt( lvl, optname,
Set the value for the specified socket option
val )
12
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as err:
print ("error")
port = 80
try:
host_ip =
socket.gethostbyname('igracias.telkomuniversity.ac.id')
except socket.gaierror:
print ("there was an error resolving the host")
sys.exit()
s.connect((host_ip, port))
print ("IP:"+ host_ip)
13
TCP Stream Comm.
Stream communication assumes that when a pair of processes
are establishing a connection, one of them plays the client role
and the other plays the server role, but thereafter they could be
peers.
Failure model : use checksums to detect and reject corrupt
packets and sequence numbers to detect and reject duplicate
packets.
Use of TCP : HTTP, FTP, and SSH
Java API for TCP streams
14
Life Cycle
15
Ex : TCP Server
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
while 1:
conn, addr = s.accept()
print ('Alamat:', addr)
data = conn.recv(BUFFER_SIZE)
print ("data diterima:", data.decode())
conn.send(data)
conn.close()
16
Ex : TCP client
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
PESAN = "Hello World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(PESAN.encode())
data = s.recv(BUFFER_SIZE)
s.close()
print ("data diterima:", data.decode())
17
UDP Datagram Comm.
a sending process to a receiving process without
acknowledgement or retries
Failure model for UDP datagrams : checksum error or because
no buffer space
Use of UDP : DNS and VoIP
18
Life Cycle
19
Ex : UDP Server
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print (addr)
print ("pesan diterima:", data.decode())
20
Ex : UDP client
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
PESAN = "Hello World!"
print ("target IP:", UDP_IP)
print ("target port:", UDP_PORT)
print ("pesan:", PESAN)
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(PESAN.encode(), (UDP_IP, UDP_PORT))
21
THANK YOU