0% found this document useful (0 votes)
6 views

Assignment1point2 Socket Programming

The document provides an overview of socket programming, detailing how processes communicate over networks using sockets as interfaces. It explains the differences between UDP and TCP protocols, including their respective characteristics and example applications for each. Additionally, it outlines assignments for creating TCP and UDP client-server applications, requiring evidence of functionality through screenshots and program submissions.

Uploaded by

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

Assignment1point2 Socket Programming

The document provides an overview of socket programming, detailing how processes communicate over networks using sockets as interfaces. It explains the differences between UDP and TCP protocols, including their respective characteristics and example applications for each. Additionally, it outlines assignments for creating TCP and UDP client-server applications, requiring evidence of functionality through screenshots and program submissions.

Uploaded by

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

19ECE311 - Computer

Networks Tutorial 1.2 and


Assignment 1.2

Socket Programming
Sockets [1,2.1]
§ a process sends/receives messages to/from a software interface
called a socket
§ also called Application Programming Interface (API) between a
network application and the network
§ analogous to sending parcels through a transport service
§ the parcel becomes the responsibility of the transport service
§ we have almost no control over how the transport service operates
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


Sockets [1,2.1]
§ the application developer has control of everything on the application-
layer side of the socket but has little control of the transport-layer side
§ the only control that the application developer has on the transport
layer side are:
(1) the choice of transport protocol, and
(2) perhaps the ability to fix a few transport-layer parameters
(e.g., maximum buffer and maximum segment sizes)
application application
socket controlled by
process process app developer

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

Application Layer: 2-3


Addressing processes [1,2.1]
§ to receive messages, a process § So, the process identifier includes both
must have an identifier the IP address and the port number
§ host device has unique 32-bit associated with the process on the host
IP address § example port numbers:
§ Q: Does IP address of host on • HTTP server: 80
which the process runs suffice • mail server: 25
for identifying the process? § to send HTTP messages to
§ A: no, many processes gaia.cs.umass.edu web server:
can be running on • IP address: 128.119.245.12
same host • port number: 80

More precisely, socket address = IP address + port number


Application Layer: 2-4
Port Numbers

Transport Layer: 3-5


Socket Programming [1,2.7]
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

Transport Layer: 3-6


Socket Programming [1,2.7]
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

Transport Layer: 3-7


Socket Programming with UDP [1,2.7]
UDP: no “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 (“datagrams”)
between client and server processes
Transport Layer: 3-8
Client/server socket interaction: UDP [1,2.7]
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
read datagram from clientSocket
serverSocket

write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Transport Layer: 3-9
Example app: UDP server [1,2.7]
Python UDPServer
from socket import *
serverPort = 12000
create UDP socket serverSocket = socket(AF_INET, SOCK_DGRAM)
bind socket to local port number 12000 serverSocket.bind(('', serverPort))
print (“The server is ready to receive”)
loop forever while True:
Read from UDP socket into message, getting message, clientAddress = serverSocket.recvfrom(2048)
client’s address (client IP and port)
modifiedMessage = message.decode().upper()
send upper case string back to this client serverSocket.sendto(modifiedMessage.encode(),
clientAddress)

Transport Layer: 3-10


Example app: UDP client [1,2.7]
Python UDPClient
include Python’s socket library from socket import *
serverName = ‘hostname’
serverPort = 12000
create UDP socket for server clientSocket = socket(AF_INET,
SOCK_DGRAM)
get user keyboard input message = raw_input(’Input lowercase sentence:’)
attach server name, port to message; send into socket clientSocket.sendto(message.encode(),
(serverName, serverPort))
read reply characters from socket into string modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print out received string and close socket print modifiedMessage.decode()
clientSocket.close()

Transport Layer: 3-11


Socket Programming with TCP [1,2.7]
Client 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 (also see 3.2 in [1])
• 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
Transport Layer: 3-12
Client/server socket interaction: TCP [1,2.7]
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
Transport Layer: 3-13
Example app: TCP server [1,2.7]
Python TCPServer
from socket import *
serverPort = 12000
create TCP welcoming socket serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
server begins listening for
incoming TCP requests
serverSocket.listen(1)
print ‘The server is ready to receive’
loop forever while True:
server waits on accept() for incoming connectionSocket, addr = serverSocket.accept()
requests, new socket created on return

read bytes from socket (but sentence = connectionSocket.recv(1024).decode()


not address as in UDP) capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.
encode())
close connection to this client (but not connectionSocket.close()
welcoming socket)
Transport Layer: 3-14
Example app: TCP client [1,2.7]
Python TCPClient
from socket import *
serverName = ’servername’
serverPort = 12000
create TCP socket for server, clientSocket = socket(AF_INET, SOCK_STREAM)
remote port 12000
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence.encode())
No need to attach server name, port modifiedSentence = clientSocket.recv(1024)
print (‘From Server:’, modifiedSentence.decode())
clientSocket.close()

Transport Layer: 3-15


Assignment 1.2.A
Create a TCP client program that creates three clients on your PC/laptop.
Create a TCP server on the same PC/laptop that would support all the three
TCP clients as follows:
a. When Client 1 sends a string, the server echoes back the string to Client 1.
b. When Client 2 sends a string, the server capitalizes and sends back the
capitalized string to Client 2.
c. When Client 3 sends a string, the server counts the number of characters
in the string and sends the count back to Client 3.
Attach programs as well as screenshots of outputs as evidence to show that
your program works fine.
The IP address of your machine should be displayed on each attached
screenshot using a command line command such as ifconfig. This is to
ensure that you have not copied outputs from others.
Assignment 1.2.B
i). Create a UDP server and a UDP client on two different machines. Make
them communicate such that the UDP server capitalizes the string sent
by the UDP client and sends the capitalized string back to the UDP client.
ii). Create a TCP server and a TCP client on two different machines. Make
them communicate such that the TCP server counts the number of
characters in the string and sends back the count of the string sent by
the TCP client.
Attach programs as well as screenshots of outputs as evidence to show
that your program works fine.
The IP address of the two machines should be displayed on each
attached screenshot using a command line command such as ifconfig.
References
[1] James Kurose and Keith Ross, “Computer Networking: A Top-down
Approach” 8th edition, Addison Wesley 2021.

You might also like