You are on page 1of 12

Socket Programming

2: Application Layer 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 application
socket controlled by
process process app developer

transport transport
network network controlled
link by OS
link Internet
physical physical

Application Layer 2-2


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.

Application Layer 2-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
 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

Application Layer 2-4


datagram
service
Client/server socket interaction: (SOCK_DGRAM)

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 server IP and
IP address port=x; send datagram via
family read datagram from clientSocket
(AF_INET) serverSocket

write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket

Application 2-5
Example app: UDP client Documentation
https://docs.python.org/

Python UDPClient
include Python’s socket
library from socket import *
serverName = ‘localhost’
serverPort = 12000
create UDP socket for
clientSocket = socket(socket.AF_INET,
server socket.SOCK_DGRAM)
get user keyboard message = raw_input(’Input lowercase sentence:’) #string
input
Attach server name, port clientSocket.sendto(message,(serverName, serverPort))
to message; send into modifiedMessage, serverAddress =
socket
clientSocket.recvfrom(2048)
address
read reply characters from
socket into string
print modifiedMessage
print out received string
and close socket
clientSocket.close()
raw_input([prompt])If the prompt argument is present, it is written to standard output without a trailing newline.
The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
socket.sendto(string, address) Send data to the socket. The socket should not be connected to a remote socket,
since the destination socket is specified by address. socket.recvfrom(bufsize)-Receive data from the socket.
The return value is a pair (string, address) where string is a string representing the data received and address is
the address of the socket sending the data.
Application Layer 2-6
Example app: UDP server
Python UDPServer
from socket import *
serverPort = 12000
create UDP socket
serverSocket = socket(AF_INET, SOCK_DGRAM)
bind socket to local port serverSocket.bind(('‘'‘ , serverPort))
number 12000 print “The server is ready to receive”
while 1:
loop forever message, clientAddress = serverSocket.recvfrom(2048)
Read from UDP socket modifiedMessage = message.upper()
into message, getting
client’s address (client IP
serverSocket.sendto(modifiedMessage, clientAddress)
and port)
send upper case string
back to this client

socket.bind(address). Bind the socket to address.

Application Layer 2-7


Socket programming with TCP
client must contact server  when contacted by client,
 server process must first server TCP creates new
be running socket for server process
 server must have created to communicate with that
socket (door) that particular client
welcomes client’s contact  allows server to talk
with multiple clients
client contacts server by:  source port numbers
 Creating TCP socket, used to distinguish
specifying IP address, clients (more in Chap
port number of server 3)
process
 when client creates application viewpoint:
socket: client TCP
establishes connection to TCP provides reliable, in-order
server TCP byte-stream transfer (“pipe”)
between client and server

Application Layer 2-8


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 request
TCP connect to hostid, port=x
connectionSocket = connection setup clientSocket = socket()
serverSocket.accept()

send request using


read request from clientSocket
connectionSocket

write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket

Application Layer 2-9


Example app: TCP client
Python TCPClient
from socket import *
serverName = ’servername’
create TCP socket for serverPort = 12000
server, remote port
12000
clientSocket = socket(AF_INET, SOCK_STREAM)
No need to attach server clientSocket.connect((serverName,serverPort))
name, port
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()

socket.recv(bufsize) Receive data from the socket. The return value is a string representing the data received. The
maximum amount of data to be received at once is specified by bufsize.

Application Layer 2-10


Example app: TCP server
Python TCPServer
from socket import *
serverPort = 12000
create TCP welcoming
socket serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
server begins listening for
serverSocket.listen(1)
incoming TCP requests print ‘The server is ready to receive’
while 1:
loop forever connectionSocket, addr = serverSocket.accept()
server waits on accept()
for incoming requests, new
socket created on return
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
read bytes from socket (but connectionSocket.send(capitalizedSentence)
not address as in UDP) connectionSocket.close()
close connection to this
client (but not welcoming
socket)
Application Layer 2-
11
Software

Version may not


be exact

You need to
learn to write
Python network
Programming
code

Application Layer 2-12

You might also like