SlideShare a Scribd company logo
goal: learn how to build client/server applications that
communicate using sockets
socket: door between application process and end-end-
transport protocol
Internet
controlled
by OS
controlled by
app developer
transport
application
physical
link
network
process
transport
application
physical
link
network
process
socket
Socket Programming
Two socket types for two transport services:
• UDP: unreliable datagram
• TCP: reliable, byte stream-oriented
Application Example:
1. Client reads a line of characters (data) from its
keyboard and sends the data to the server.
2. The server receives the data and converts
characters to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays
the line on its screen.
Socket Programming
UDP: no “connection” between client & server
• no handshaking before sending data
• sender explicitly attaches IP destination address and
port # to each packet
• rcvr extracts sender IP address and port# from received
packet
UDP: transmitted data may be lost or received
out-of-order
Application viewpoint:
• UDP provides unreliable transfer of groups of bytes
(“datagrams”) between client and server
Socket Programming w/ UDP
close
clientSocket
read datagram from
clientSocket
create socket:
clientSocket =
socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP and
port=x; send datagram via
clientSocket
create socket, port= x:
serverSocket =
socket(AF_INET,SOCK_DGRAM)
read datagram from
serverSocket
write reply to
serverSocket
specifying
client address,
port number
server (running on serverIP) client
Socket Programming w/ UDP
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
Python UDPClient
include Python’s socket
library
create UDP socket for
server
get user keyboard
input
Attach server name, port to
message; send into socket
print out received string
and close socket
read reply characters from
socket into string
Socket Programming w/ UDP
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print “The server is ready to receive”
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
Python UDPServer
create UDP socket
bind socket to local port
number 12000
loop forever
Read from UDP socket into
message, getting client’s
address (client IP and port)
send upper case string
back to this client
Socket Programming w/ UDP
client must contact server
• server process must first be
running
• server must have created
socket (door) that welcomes
client’s contact
client contacts server by:
• Creating TCP socket, specifying
IP address, port number of
server process
• when client creates socket:
client TCP establishes
connection to server TCP
• when contacted by client, server
TCP creates new socket for
server process to communicate
with that particular client
• allows server to talk with
multiple clients
• source port numbers used
to distinguish clients (more
in Chap 3)
TCP provides reliable, in-order
byte-stream transfer (“pipe”)
between client and server
application viewpoint:
Socket Programming w/ TCP
wait for incoming
connection request
connectionSocket =
serverSocket.accept()
create socket,
port=x, for incoming
request:
serverSocket = socket()
create socket,
connect to hostid, port=x
clientSocket = socket()
server (running on hostid) client
send request using
clientSocket
read request from
connectionSocket
write reply to
connectionSocket
TCP
connection setup
close
connectionSocket
read reply from
clientSocket
close
clientSocket
Socket Programming w/ TCP
from socket import *
serverName = ’servername’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
Python TCPClient
create TCP socket for
server, remote port 12000
No need to attach server
name, port
Socket Programming w/ TCP
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
serverSocket.listen(1)
print ‘The server is ready to receive’
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
Python TCPServer
create TCP welcoming
socket
server begins listening for
incoming TCP requests
loop forever
server waits on accept()
for incoming requests, new
socket created on return
read bytes from socket (but
not address as in UDP)
close connection to this
client (but not welcoming
socket)
Socket Programming w/ TCP
Multithreading in python
Thread Lifecycle
Multithreading in python
Multithreading in python
Multithreading in python
MultiClient TCP programming in Python
Multithreaded TCP Server in Python
Multithreaded TCP Client in Python
Ad

Recommended

Socket Programming_theory.ppt
Socket Programming_theory.ppt
mdrobinhossain4
 
Networking in Python2025 (programs allll)
Networking in Python2025 (programs allll)
PriyankaPatil919748
 
Chapter_2_part5.ppt in the department of computer science
Chapter_2_part5.ppt in the department of computer science
zahrabashir8320
 
Socket programming assignment
Socket programming assignment
Ravi Gupta
 
Socket programming-tutorial-sk
Socket programming-tutorial-sk
sureshkarthick37
 
JavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdf
jose19881
 
Networking in python by Rj
Networking in python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Os 2
Os 2
university of Gujrat, pakistan
 
EN-04 (1).pptx
EN-04 (1).pptx
TienTran779192
 
Socket programming
Socket programming
Divya Sharma
 
Socket programming using java
Socket programming using java
UC San Diego
 
Java socket programming
Java socket programming
Mohammed Abdalla Youssif
 
Socket programming in C
Socket programming in C
Deepak Swain
 
Application Layer and Socket Programming
Application Layer and Socket Programming
elliando dias
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
UC San Diego
 
Socket programming
Socket programming
chandramouligunnemeda
 
Lecture set 7
Lecture set 7
Gopi Saiteja
 
Python networking
Python networking
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
sockets SMTP Bmsce ppt information science and engineering
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
Npc08
Npc08
vamsitricks
 
Socket programming
Socket programming
NemiRathore
 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
Ghadeer AlHasan
 
Sockets
Sockets
babu4b4u
 
Network Sockets
Network Sockets
Peter R. Egli
 
Socket Programming
Socket Programming
CEC Landran
 
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
kittycatcatcat123456
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
Maulik Borsaniya
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 

More Related Content

Similar to Basics of Socket Programming using python (20)

EN-04 (1).pptx
EN-04 (1).pptx
TienTran779192
 
Socket programming
Socket programming
Divya Sharma
 
Socket programming using java
Socket programming using java
UC San Diego
 
Java socket programming
Java socket programming
Mohammed Abdalla Youssif
 
Socket programming in C
Socket programming in C
Deepak Swain
 
Application Layer and Socket Programming
Application Layer and Socket Programming
elliando dias
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
UC San Diego
 
Socket programming
Socket programming
chandramouligunnemeda
 
Lecture set 7
Lecture set 7
Gopi Saiteja
 
Python networking
Python networking
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
sockets SMTP Bmsce ppt information science and engineering
sockets SMTP Bmsce ppt information science and engineering
UtkarshaMahajan6
 
Npc08
Npc08
vamsitricks
 
Socket programming
Socket programming
NemiRathore
 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
Ghadeer AlHasan
 
Sockets
Sockets
babu4b4u
 
Network Sockets
Network Sockets
Peter R. Egli
 
Socket Programming
Socket Programming
CEC Landran
 
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
CHAPTER 24.pdfhehbebbebebbebbeebbebbebeb
kittycatcatcat123456
 
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA
Maulik Borsaniya
 

Recently uploaded (20)

Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
Mobile database systems 20254545645.pptx
Mobile database systems 20254545645.pptx
herosh1968
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Solar thermal – Flat plate and concentrating collectors .pptx
Solar thermal – Flat plate and concentrating collectors .pptx
jdaniabraham1
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
Mobile database systems 20254545645.pptx
Mobile database systems 20254545645.pptx
herosh1968
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Ad

Basics of Socket Programming using python

  • 1. goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end-end- transport protocol Internet controlled by OS controlled by app developer transport application physical link network process transport application physical link network process socket Socket Programming
  • 2. Two socket types for two transport services: • UDP: unreliable datagram • TCP: reliable, byte stream-oriented Application Example: 1. Client reads a line of characters (data) from its keyboard and sends the data to the server. 2. The server receives the data and converts characters to uppercase. 3. The server sends the modified data to the client. 4. The client receives the modified data and displays the line on its screen. Socket Programming
  • 3. UDP: no “connection” between client & server • no handshaking before sending data • sender explicitly attaches IP destination address and port # to each packet • rcvr extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: • UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Socket Programming w/ UDP
  • 4. close clientSocket read datagram from clientSocket create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) read datagram from serverSocket write reply to serverSocket specifying client address, port number server (running on serverIP) client Socket Programming w/ UDP
  • 5. from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close() Python UDPClient include Python’s socket library create UDP socket for server get user keyboard input Attach server name, port to message; send into socket print out received string and close socket read reply characters from socket into string Socket Programming w/ UDP
  • 6. from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress) Python UDPServer create UDP socket bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client Socket Programming w/ UDP
  • 7. client must contact server • server process must first be running • server must have created socket (door) that welcomes client’s contact client contacts server by: • Creating TCP socket, specifying IP address, port number of server process • when client creates socket: client TCP establishes connection to server TCP • when contacted by client, server TCP creates new socket for server process to communicate with that particular client • allows server to talk with multiple clients • source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order byte-stream transfer (“pipe”) between client and server application viewpoint: Socket Programming w/ TCP
  • 8. wait for incoming connection request connectionSocket = serverSocket.accept() create socket, port=x, for incoming request: serverSocket = socket() create socket, connect to hostid, port=x clientSocket = socket() server (running on hostid) client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup close connectionSocket read reply from clientSocket close clientSocket Socket Programming w/ TCP
  • 9. from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() Python TCPClient create TCP socket for server, remote port 12000 No need to attach server name, port Socket Programming w/ TCP
  • 10. from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() Python TCPServer create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket) Socket Programming w/ TCP