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
[Link]:80
Client socket address
[Link]:3479 FTP Server
(port 21)
Client HTTP Server
Connection socket pair (port 80)
([Link]:3479, [Link]:80)
Client host address Server host address
[Link] [Link]
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)
[Link](family, type) Create and return a new socket object
[Link](name) Convert a string quad dotted IP address
to a fully qualified domain name
[Link](hostname) Resolve a hostname to a string quad
dotted IP address
[Link](fd, family, type) Create a socket object from an existing
file descriptor
10
[Link]( (adrs, port) ) Bind the socket to the address and port
[Link]() Return a client socket (with peer address
information)
Place the socket into the listening state, able
[Link](backlog) to pend backlogoutstanding connection
requests
Connect the socket to the defined host and
[Link]( (adrs, port) )
port
[Link]( buflen[, flags] ) Receive data from the socket, up
to buflen bytes
11
Receive data from the socket, up
[Link]( buflen[, flags] ) to buflen bytes, returning also the remote
host and port from which the data came
[Link]( data[, flags] ) Send the data through the socket
[Link]( data[, flags], addr ) Send the data through the socket
[Link]() Close the socket
[Link]( lvl, optname ) Get the value for the specified socket option
[Link]( lvl, optname,
val ) Set the value for the specified socket option
12
import socket
import sys
try:
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
except [Link] as err:
print ("error")
port = 80
try:
host_ip =
[Link]('[Link]')
except [Link]:
print ("there was an error resolving the host")
[Link]()
[Link]((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 = '[Link]'
TCP_PORT = 5005
BUFFER_SIZE = 1024
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((TCP_IP, TCP_PORT))
[Link](1)
while 1:
conn, addr = [Link]()
print ('Alamat:', addr)
data = [Link](BUFFER_SIZE)
print ("data diterima:", [Link]())
[Link](data)
[Link]()
16
Ex : TCP client
import socket
TCP_IP = '[Link]'
TCP_PORT = 5005
BUFFER_SIZE = 1024
PESAN = "Hello World!"
s = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link]((TCP_IP, TCP_PORT))
[Link]([Link]())
data = [Link](BUFFER_SIZE)
[Link]()
print ("data diterima:", [Link]())
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 = "[Link]"
UDP_PORT = 5005
sock = [Link](socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
[Link]((UDP_IP, UDP_PORT))
while True:
data, addr = [Link](1024)
print (addr)
print ("pesan diterima:", [Link]())
20
Ex : UDP client
import socket
UDP_IP = "[Link]"
UDP_PORT = 5005
PESAN = "Hello World!"
print ("target IP:", UDP_IP)
print ("target port:", UDP_PORT)
print ("pesan:", PESAN)
sock = [Link](socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
[Link]([Link](), (UDP_IP, UDP_PORT))
21
THANK YOU