0% found this document useful (0 votes)
12 views13 pages

CN-Unit2-Scoket Programming

The document provides an overview of socket programming, focusing on building client/server applications using UDP and TCP protocols. It explains the differences between UDP (unreliable, datagram-based) and TCP (reliable, byte stream-oriented), along with example applications and Python code snippets for both client and server implementations. The document also details the socket creation process, data transmission, and the client-server interaction model for both protocols.

Uploaded by

Nikhil Ragala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

CN-Unit2-Scoket Programming

The document provides an overview of socket programming, focusing on building client/server applications using UDP and TCP protocols. It explains the differences between UDP (unreliable, datagram-based) and TCP (reliable, byte stream-oriented), along with example applications and Python code snippets for both client and server implementations. The document also details the socket creation process, data transmission, and the client-server interaction model for both protocols.

Uploaded by

Nikhil Ragala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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 applicati
socket on controlled
process by app
process developer
transport transpo
rt
network networ
k lin controlle
link Intern
k d by OS
physical et physic
al

Application Layer: 2-1


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
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

Application Layer: 2-2


Socket programming
with
UDP: noUDP
“connection” between
client and 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
Application Layer: 2-3
User Datagram Protocol(UDP):
An Analogy
UDP Postal Mail
Postal Mail
• Single socket to receive Single mailbox
• Single to receive
mailbox to receive
messages messages
letters
• No guarantee of delivery Unreliable 
• Unreliable
Not necessarily in-order delivery
• Not necessarily in-order • Not
Each necessarily
letter in-order
is independent
delivery delivery
Must address each reply
• Datagram – independent • Letters sent independently
packets
• Must address each packet • Must address each reply

Example UDP applications


Multimedia, voice over IP
Application Layer: 2-4
This line creates the client’s socket, called clientSocket . The first
parameter indicates the address family;
AF_INET indicates that the underlying network is using IPv4
SOCK_DGRAM: Type of socket here it is a UDP socket.
SOCK_STREAM: It is a TCP Socket.
Client/server socket interaction:
UDP server (running on serverIP) client
create socket:
create socket, port= x: clientSocket =
serverSocket = socket(AF_INET,SOCK_DGRAM)
socket(AF_INET,SOCK_DGRAM
) Create datagram with serverIP
address And port=x; send
datagram via clientSocket
read datagram
from
serverSocket
read datagram
write reply from clientSocket
to
serverSocket close
specifying clientSock
client et
Application Layer: 2-6
address,
Example app: UDP client
Python UDPClient
include Python’s socket from socket import *
library
serverName = ‘hostname’
serverPort = 12000
create UDP socket for clientSocket =
server
socket(AF_INET,
get user keyboard input SOCK_D
attach server name, port to message; send into GRAM)
socket message =
read reply characters from socket into raw_input(’Input lowercase
string sentence:’)
clientSocket.sendto(messag
print out received string and close
socket e.encode(),
(serverNa
me,
serverPor Application Layer: 2-7
Example app: UDP server
Python UDPServer
from socket
import *
create UDP socket serverPort = = socket(AF_INET,
serverSocket
bind socket to local port number 12000
SOCK_DGRAM) serverSocket.bind(('',
12000
serverPort))
loop print (“The server is ready to
forever
receive”) while True:
Read from
UDP socket into message, getting message, clientAddress =
client’s
sendaddress (client
upper case IP and
string port)
back to this serverSocket.recvfrom(2048) modifiedMessage
client
= message.decode().upper()
serverSocket.sendto(modifiedMessage.encode(),
clientAddress)

Application Layer: 2-8


Socket programming
with
Client TCP
must contact server  when contacted by client, server
 server process must first be TCP creates new socket for server
running process to communicate with that
 server must have created socket particular client
(door) that welcomes client’s • allows server to talk with multiple
contact clients
Client contacts server by: • source port numbers used to
distinguish clients (more in Chap 3)
 Creating TCP socket, specifying IP
address, port number of server
process Application viewpoint
 when client creates socket: client TCP provides reliable, in-order
TCP establishes connection to byte-stream transfer (“pipe”)
server TCP between client and server
processes
Application Layer: 2-9
• During the three-way handshake, the client process
knocks on the welcoming door of the server process.
• When the server “hears” the knocking, it creates a new
door— a new socket that is dedicated to that particular
client.
• The welcoming door is a TCP socket object that we call ­
serverSocket ;
• the newly created socket dedicated to the client making
the connection is called connectionSocket.
• The client process can send arbitrary bytes into its socket,
and TCP guarantees that the server process will receive
(through the connection socket) each byte in the order
sent.
• similarly, the server process not only receives bytes from
but also sends bytes into its connection socket
Client/server socket interaction:
TCP server (running on hostid) client
create socket,
port=x, for
incoming
request:
serverSocket =
socket()
wait for incoming create socket,
connection
TCP connect to hostid,
request connection port=x
connectionSocke
serverSocket.accep setup clientSocket = socket()
tt()=
send request
read request using
from clientSocket
connectionSock
et
write reply to
connectionSock read reply
et from
close clientSocket
connectionSock close
et clientSock
et Application Layer: 2-11
Example app: TCP client
Python TCPClient
from socket import *
serverName =
’servername’ serverPort
create TCP socket for = 12000
clientSocket = socket(AF_INET,
server, remote port SOCK_STREAM)
12000
clientSocket.connect((serverName,serverPor
t)) sentence = raw_input(‘Input lowercase
No need to attach server name, sentence:’)
port clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print (‘From Server:’,
modifiedSentence.decode())
clientSocket.close() Application Layer: 2-12
Example app: TCP
server Python TCPServer
from socket
import *
create TCP welcoming serverSocket
serverPort = =
socket socket(AF_INET,SOCK_STREAM)
12000
server begins listening
serverSocket.bind((‘’,serverPort))
for incoming TCP
requests serverSocket.listen(1)
loop print ‘The server is ready to
forever receive’ while True:
server waits on accept() for connectionSocket, addr =
incoming requests, new socket
created onread
return
bytes from socket sentence =
serverSocket.accept()
(but not address as in connectionSocket.recv(1024).decode()
UDP) capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.
close connection to this client encode())
(but not connectionSocket.close()
welcoming socket) Application Layer: 2-13

You might also like