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

TCP_Socket_prog

The document provides an overview of socket programming, focusing on the client-server model, addressing requirements, and the types of sockets. It explains the functions and processes involved in creating, connecting, and managing sockets for communication between clients and servers. Additionally, it includes examples of socket programming in Python, detailing how to establish connections and send/receive data.

Uploaded by

kbommidi
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)
2 views

TCP_Socket_prog

The document provides an overview of socket programming, focusing on the client-server model, addressing requirements, and the types of sockets. It explains the functions and processes involved in creating, connecting, and managing sockets for communication between clients and servers. Additionally, it includes examples of socket programming in Python, detailing how to establish connections and send/receive data.

Uploaded by

kbommidi
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/ 24

shr

Socket
Programming

1
Client-Server Model
Every network application is based on client-server model:
 A Server process (program) and one or more client
processes.
 Server manages some resources or provides some service
by using resources to clients.

1. Client sends request


Client Server
Resourc
e
4. Clientproces 3. Server sends response
proces
handles s s 2. Server
response
handles
request
Clients and Servers are processes running on hosts
(can be the same or different hosts).
Addressing
 Requirements
Two levels of addressing is required.
 Each computer (host) on a
network needs a unique global address.
 its IP address (Example: 192.118.56.25)
 Each application/process on a (multi-tasking)
computer needs a
unique address within the computer.
 known as service access point (SAP) or port
number
Each server waits for requests to arrive
on port numbers associated with that
particular service.
Port
s destination
Identifying the ultimate
 IP addresses identify hosts. Host has many
applications.
 Ports identify applications.
 Well-known ports (Standard Services): 1-
1023

Port 7: Echo server, Port 13: System’s
date/time

Port 25: E-Mail server, Port 80: Web server
 Ephemeral (temporal) ports: 1024-5000
Applicatio WWW E- Telne
 User-defined
n ports: mail5001-65535t
Por 80 25
t 23

192.18.22.1
Using Ports to Identify Services
Server host 134.173.42.2

Client host Service request


for Web
134.173.42.2:80 server
(i.e., Web Kerne (port 80)
Client
server) l E-Mail
server
(port 25)

Service request
for Web
134.173.42.2:2 server
5 Kerne (port 80)
Client
(i.e., E-mail l E-Mail
server) server
(port 25)
Socket
What is a Socket?
It is a protocolsindependent interface to multiple
transport
layer services.
 To kernel, socket is endpoint of communication.
 To application, socket is file descriptor that lets
application
read from or write to network.
 It is an interface (a “door”) into which an application
process can send and receive messages to/from
another (remote or local) application process.
 Clients and servers communicate with each other by
sending to
and receiving from socket descriptors.
 Every endpoint (socket) is identified by Address
and Port
Socket
Interface
It is the interface between application and protocol
software.

8
Internet Connections
 Clients and servers communicate by sending streams of
bytes over connections.
 Connections are point-to-point, full-duplex (2-way
communication), and reliable.

Client socket address Server socket address


128.2.194.242: 134.173.42.2: 80
51213

Client Web Server


Connection socket pair (port 80)
(128.2.194.242:51213,
134.173.42.2:80)
Client IP Server IP
address address
134.173.42.2
128.2.194.24
2
Socket Functions
For

Defining an end- point for communication

Initiate(client) and accept(server) a
connection

Send and receive data

Terminate a connection gracefully
Types of Sockets
• Two types of sockets :
– stream vs. datagram
• Stream socket : (connection- oriented socket)
– It provides reliable, connected networking
service.
– Error free; no out- of- order packets (uses
TCP).
– applications: telnet/ ssh, http, …

• Datagram socket :(connectionless socket)


– It provides unreliable, best- effort networking
service.
– Packets may be lost; may arrive out of order
(uses UDP).
Client (TCP)– high level
view
Create a socket

Connect to the server

Send/Receive data

Shutdown connection
Server (TCP) – high level
view Create a socket

Bind(register) the socket

Listen (wait) for


connections

Accept new client


connections

Send/Receive data

Shutdown connection
TCP Client-Server interaction using Sockets API

Two Separate IDLE terminals are required for client and


server programs.
TCP Client side Socket functions
Socket Programming in
Python
Creating a Socket
To create a socket, you must use the socket.socket()
function
available in socket module, which has the general syntax:

import socket
s = socket.socket (family, type, proto)(returns Socket descriptor)
 family specifies address or protocol family. Can be AF_INET
(default)
AF_INET6, etc.
 type specifies socket type. Can be SOCK_STREAM (default)(TCP)
SOCK_DGRAM (UDP) etc.
 proto (protocol) argument is normally left to zero.

sock = socket.socket (socket.AF_INET,


socket.SOCK_STREAM, 0) or 1
6
Establishing a
connection
import socket
socket.connect(address)
Connect to a remote socket at address
 This function is used by a client to establish a
connection
with a server.
 The address, contains the address information
of the server to connect to. (IP Address and
Port No).
 In general, the port number to be used on the
client’s side is chosen by the kernel (ephemeral
port).

sock.connect((‘192.168.10.52’,12000)) 1
7
Function for sending
data
import socket
socket.send(bytes)

 Send data to the socket.

 The socket must be connected to a remote


socket.
 Returns the number of bytes sent.

sock.send("this is nizam".encode())
or
sock.send(b‘this is nizam’)
1
8
Function for receiving
data
import socket
socket.recv(bufsize)
 Receive data from the socket.
The maximum amount of data to be received at
once is
specified by bufsize.
 The return value is a bytes object representing the
data received.
Note: For best match with hardware and network realities, the value
of
bufsize should be a relatively small power of 2, for example,
1024,2046 etc. =sock.recv(2046)
M (bytes)
or
M (string) =sock.recv(2046).decode()
1
9
Closing a
import socket socket
socket.close()
 Mark the socket closed.
The transport layer generally tries to flush out, i.e.
send, any
remaining data, when a close is done.
The underlying system resource is closed. Once that
happens, all future operations on the socket object will
fail. The remote end will receive no more data.

sock.close()

2
0
TCP Server side Socket functions
Associating a local address with a
socket
Import socket
socket.bind(address)
Bind the socket to address. The socket must not
already be bound.

 It is used for explicitly associating a


protocol specific local address to a
socket.
 For Internet Protocols, bind() associates
with the socket a port number
and/or one of the IP addresses of the host
on which the program is running.
Listening for a
connection
import socket
socket.listen([backlog])
 Enable a server to
accept connections.
 If backlog is
specified, it must be
at least ‘0’.
 It specifies the number of unaccepted
connections that the system will allow before
refusing new connections.
It is necessary for a server to listen for connections
on some port in order to receive connection
requests from client. listen() performs two duties:
Accepting a connection
import socket
socket.accept()
Accept a connection. The socket must be
bound to an address and listening for
 connections.
This function is called by the server to get the
next connection from the queue of completed
connections.
 If noconnection has been completed, the
function call blocks.
 The return value is a pair (conn, address) where
conn is a new socket object usable to send and
receive data on the connection.
 address is the address bound to the socket on the
Accepting a connection

 During the three-way handshake (connection


establishment), the client process knocks on the
welcoming door (initial point of contact for all clients) of
the server process.
 When the server “hears” the knocking, it creates a new

You might also like