SlideShare a Scribd company logo
Socket programming
goal: learn how to build client/server applications
that communicate using sockets
socket: door between application process and
end-end-transport protocol
Application Layer 2-1
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 Layer 2-2
Application Example:
1. client reads a line of characters (data) from its
keyboard and sends data to server
2. server receives the data and converts
characters to uppercase
3. server sends modified data to client
4. client receives modified data and displays line
on its screen
Socket programming with UDP
UDP: no “connection” between client &
server
 no handshaking before sending data
 sender explicitly attaches IP destination address
and port # to each packet
 receiver 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
Application Layer 2-3
Client/server socket interaction: 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
Application 2-4
server (running on serverIP) client
Application Layer 2-5
Example app: UDP client
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(AF_INET,
SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message.encode(),
(serverName, serverPort))
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print modifiedMessage.decode()
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
Application Layer 2-6
Example app: UDP server
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
while True:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.decode().upper()
serverSocket.sendto(modifiedMessage.encode(),
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 with TCP
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)
Application Layer 2-7
TCP provides reliable, in-order
byte-stream transfer (“pipe”)
between client and server
application viewpoint:
Client/server socket interaction: TCP
Application Layer 2-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
Application Layer 2-9
Example app: TCP client
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.encode())
modifiedSentence = clientSocket.recv(1024)
print (‘From Server:’, modifiedSentence.decode())
clientSocket.close()
Python TCPClient
create TCP socket for
server, remote port 12000
No need to attach server
name, port
Application Layer 2-10
Example app: TCP server
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 True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.
encode())
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)

More Related Content

PPTX
Client server chat application
PPTX
IOT System Management with NETCONF-YANG.pptx
PPT
ipv6 ppt
PPT
Cisco Packet Tracer Overview 20 Jul09
PPTX
Intel galileo gen 2
PPT
Loopback address
PPTX
Internet Protocol version 6
Client server chat application
IOT System Management with NETCONF-YANG.pptx
ipv6 ppt
Cisco Packet Tracer Overview 20 Jul09
Intel galileo gen 2
Loopback address
Internet Protocol version 6

What's hot (20)

PDF
Advanced computer network lab manual (practicals in Cisco Packet tracer)
PPT
Packet tracer
PDF
IoT and m2m
PPT
Basics Of Networking (Overview)
PPT
DOCX
Linux server administration syllabus
PPT
PPTX
Sub Netting
PDF
IPv6 and IoT
DOCX
Packet tracer practical guide
PPT
Tcp ip
PPT
PDF
IoT Networking Part 2
PDF
netconf and yang
PPTX
Data aggregation in wireless sensor network , 11751 d5811
PPTX
Private VLANs
DOC
Task assignment approach
PPTX
Ccna PPT
PPT
Chapter 5 : Link Layer
PPTX
Advanced computer network lab manual (practicals in Cisco Packet tracer)
Packet tracer
IoT and m2m
Basics Of Networking (Overview)
Linux server administration syllabus
Sub Netting
IPv6 and IoT
Packet tracer practical guide
Tcp ip
IoT Networking Part 2
netconf and yang
Data aggregation in wireless sensor network , 11751 d5811
Private VLANs
Task assignment approach
Ccna PPT
Chapter 5 : Link Layer
Ad

Similar to Socket Programming_theory.ppt (20)

PPT
Chapter_2_part5.ppt in the department of computer science
PPTX
Basics of Socket Programming using python
PPTX
Networking in Python2025 (programs allll)
PDF
JavaSockets-Session10 New York university.pdf
PPTX
EN-04 (1).pptx
PPTX
Chuong5_Networking_updated.Networking_updatedpptx
PDF
Socket programming using java
PPT
Lan chat system
PPTX
Java Network Programming.pptx
PPT
Chapter2 l2 modified_um
PPTX
Lecture 3 computer communications and networks
PPT
Socket programming in C
PDF
Lecture set 7
PDF
Socket Programming
PPT
Application Layer.pptand documents of co
PPTX
PPT
Network programming in Java
PPT
Sockets
PPT
Socket programming
PPTX
02_Chapter_2_V6_LV.pptx
Chapter_2_part5.ppt in the department of computer science
Basics of Socket Programming using python
Networking in Python2025 (programs allll)
JavaSockets-Session10 New York university.pdf
EN-04 (1).pptx
Chuong5_Networking_updated.Networking_updatedpptx
Socket programming using java
Lan chat system
Java Network Programming.pptx
Chapter2 l2 modified_um
Lecture 3 computer communications and networks
Socket programming in C
Lecture set 7
Socket Programming
Application Layer.pptand documents of co
Network programming in Java
Sockets
Socket programming
02_Chapter_2_V6_LV.pptx
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPH.pptx obstetrics and gynecology in nursing
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
human mycosis Human fungal infections are called human mycosis..pptx
VCE English Exam - Section C Student Revision Booklet
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
Sports Quiz easy sports quiz sports quiz
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems

Socket Programming_theory.ppt

  • 1. Socket programming goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end-end-transport protocol Application Layer 2-1 Internet controlled by OS controlled by app developer transport application physical link network process transport application physical link network process socket
  • 2. Socket programming Two socket types for two transport services: • UDP: unreliable datagram • TCP: reliable, byte stream-oriented Application Layer 2-2 Application Example: 1. client reads a line of characters (data) from its keyboard and sends data to server 2. server receives the data and converts characters to uppercase 3. server sends modified data to client 4. client receives modified data and displays line on its screen
  • 3. Socket programming with UDP UDP: no “connection” between client & server  no handshaking before sending data  sender explicitly attaches IP destination address and port # to each packet  receiver 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 Application Layer 2-3
  • 4. Client/server socket interaction: 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 Application 2-4 server (running on serverIP) client
  • 5. Application Layer 2-5 Example app: UDP client from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message.encode(), (serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage.decode() 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
  • 6. Application Layer 2-6 Example app: UDP server from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print (“The server is ready to receive”) while True: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.decode().upper() serverSocket.sendto(modifiedMessage.encode(), 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
  • 7. Socket programming with TCP 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) Application Layer 2-7 TCP provides reliable, in-order byte-stream transfer (“pipe”) between client and server application viewpoint:
  • 8. Client/server socket interaction: TCP Application Layer 2-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
  • 9. Application Layer 2-9 Example app: TCP client 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.encode()) modifiedSentence = clientSocket.recv(1024) print (‘From Server:’, modifiedSentence.decode()) clientSocket.close() Python TCPClient create TCP socket for server, remote port 12000 No need to attach server name, port
  • 10. Application Layer 2-10 Example app: TCP server 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 True: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024).decode() capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence. encode()) 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)