CII3D4
SISTEM PARALEL DAN TERDISTRIBUSI
Materi 5:
Socket Programming
1
Apa itu socket?
Sebuah antarmuka aplikasi dan jaringan
• Application membuat socket
• Tipe sokcet mendikte cara komunikasi
– reliable vs best effort
– connection oriented vs conectionless
Setelah terkonfigurasi, aplikasi dapat
• Melewatkan data ke socket untuk transmisi jaringan
• Menerima data dari socket (ditransmisikan melalui jaringan
oleh host lain)
2
Layering Makes it Easier
• Application programmer
– Tidak perlu mengirim IP packets
– Tidak perlu mengirim Ethernet frames
– Tidak perlu mengetahui bagaimana TCP
mengimplementasikan reabilitas
• Hanya perlu cara untuk meneruskan data
– Socket adalah API untuk mengakses fungsi lapisan
transport
3
Identify the Destination
• Addressing
– IP address
– hostname (mendapatkan 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
– Dimana remote machine (IP address, hostname)
– Layanan apa yang mendapatkan 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
socket.getfqdn(name) Convert a string quad dotted IP address
to a fully qualified domain name
socket.gethostbyname(hostname) Resolve a hostname to a string quad
dotted IP address
socket.fromfd(fd, family, type) Create a socket object from an existing
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
sock.recv( buflen[, flags] ) Receive data from the socket, up
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,
val ) Set the value for the specified socket option
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.
Komunikasi stream mengasumsikan bahwa ketika sebuah
pasangan proses telah terjadi membuat koneksi, salah satu
berperan sebagai klien dan yang lain berperan sebagai server,
tetapi setelah itu mereka dapat menjadi peers.
Failure model : menggunakan checksum untuk mendeteksi
dan menolak paket yang rusak dan nomor urut untuk
mendeteksi dan menolak paket duplikat.
Use of TCP : HTTP, FTP, and SSH
Java API untuk 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.
proses pengiriman ke proses penerimaan tanpa
acknowledgement atau retries
Failure model for UDP datagrams : checksum error atau
karena tidak ada buffer space
Use of UDP : DNS dan 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