You are on page 1of 28

01 Network Programming

• Client-Server Transactions
• Programmer’s View of Internet
• Internet Connections
• Clients
• Using Ports to Identify Services
• Servers
Client-Server Transactions

Every network application is based on client-server model:

Server process and one or more client processes.

Server manages some resource.

Server provides service by manipulating resource


for clients.

2
12
Client-Server Transactions

1. Client sends request

Client Server
process process Resource

4. Client 3. Server sends response 2. Server


handles handles
response request

Note: clients and servers are processes running on hosts


(can be the same or different hosts).

3
12
Programmer’s View of Internet

Hosts are mapped to set of 32-bit or 128-bit


IP addresses

IP addresses are mapped to identifiers called


Internet domain names

Process on one Internet host can communicate


with process on another over a connection

4
12
Internet Connections

Clients and servers communicate by sending streams of


bytes over connections

point-to-point Full-duplex
(2-way
Communication)
Connections

Reliable

5
12
Internet Connections

Server socket
Client socket address
address
128.2.194.242:51213
134.173.42.2:80

Server
Client
(port 80)

Connection socket pair


(128.2.194.242:51213, 134.173.42.2:80)

Client host address Server host address


128.2.194.242 134.173.42.2

Note: 51213 is an ephemeral port Note: 80 is a well-known port


allocated by the kernel associated with Web servers
6
12
Clients

Examples of client programs

Web
ftp telnet ssh
browsers

7
12
Clients

How does a client find the server?

IP address in server socket address identifies host


(more precisely, an adapter on the host)

Well-known port in server socket address identifies


service, and thus implicitly identifies server process
that provides it

8
12
Clients

Examples of well-known ports

Port 7: Echo server

Port 23: Telnet server

Port 25: Mail server

Port 80: Web server

9
12
Using Ports to Identify Services

Server host 134.173.42.2


Service request for
Client host 134.173.42.2:80 Web server
(i.e., Web server) (port 80)
Client Kernel
Echo server
(port 7)

Service request for


Web server
134.173.42.2:7 (port 80)
(i.e., echo server)
Kernel
Client
Echo server
(port 7)

10
12
Servers

Servers are long-running processes (daemons).

Created at boot time (typically) by init process


(process 1)

Run continuously until machine is turned off

Or spawned by inetd in response to connection


to port

11
12
Servers
Each server waits for requests to arrive on well-known
port associated with that particular service

Port 7: Echo server

Port 23: Telnet server

Port 25: Mail server

Port 80: Web server

Machine that runs a server process is also often


12 referred to as a “server”
12
02 Sockets
• Python Network Programming
• Sockets: Communication Endpoints
• Overview of Sockets Interface
• Connection-Oriented Sockets
• Socket process for connection oriented
clients and servers
• socket() Module Function
• Socket Object (Built-In) Methods
Python Network Programming

Python provides two levels of access to network services.

At a low level can access the basic socket support in the


underlying operating system, which allows to implement
clients and servers for both connection-oriented and
connectionless protocols.

2
15
Sockets: Communication Endpoints

Sockets are the endpoints of a bidirectional


communications channel.

Sockets may communicate within a process, between


processes on the same machine, or between processes
on different continents.

3
15
Sockets: Communication Endpoints

Sockets may be implemented over a number of different


channel types: Unix domain sockets, TCP, UDP, and so
on.

The socket library provides specific classes for handling


the common transports as well as a generic interface for
handling the rest.

4
15
Overview of Sockets Interface

5
15
Connection-Oriented Sockets

Connection-oriented communication offers sequenced,


reliable, and unduplicated delivery of data, without record
boundaries.

Each message may be broken up into multiple pieces,


which are all guaranteed to arrive at their destination, put
back together and in order, and delivered to the waiting
application.

6
15
Connection-Oriented Sockets

Transmission Control Protocol (TCP)

To create TCP sockets, one must use SOCK_STREAM


as the socket type.

The networked version of these sockets use the Internet


Protocol (IP) to find hosts in the network, the entire
system generally goes by the combined names of both
protocols (TCP and IP), or TCP.

7
15
Socket process for connection
oriented clients and servers

8
15
socket() Module Function
To create a socket, you must use the socket.socket()
function, which has the general syntax:

Socket (socket_family, socket_type, protocol=0)

The socket_family is either AF_UNIX or AF_INET,


and the socket_type is SOCK_STREAM.

9
15
socket() Module Function
The protocol is usually left out, defaulting to 0.

To create a TCP/IP socket,

tcpSock = socket.socket (socket.AF_INET,


socket.SOCK_STREAM)

10
15
Socket Object (Built-In) Methods
Server Socket Methods

Bind address (hostname, port number


s.bind()
pair) to socket

s.listen() Set up and start TCP listener

Passively accept TCP client connection,


s.accept()
waiting until connection arrives (blocking)

11
15
Socket Object (Built-In) Methods
Client Socket Methods

s.connect() Actively initiate TCP server connection

s.connect_ex() Extended version of connect (),


where problems returned as error
codes rather than an exception
being thrown

12
15
Socket Object (Built-In) Methods
General Socket Methods

s.recv () Receive TCP message

s.recv _into() Receive TCP message into specified buffer

s.send () Transmit TCP message

s. sendall() Transmit TCP message completely

13
15
Socket Object (Built-In) Methods
General Socket Methods

s.getpeername ()
Remote address connected to socket (TCP)

s.getsockname()
Address of current socket

s.getsockopt()
Return value of given socket option

s.setsockopt()
Set value for given socket option
14
15
Socket Object (Built-In) Methods
General Socket Methods

s.shutdown ()
Shut down the connection

s.close()
Close socket

s.detach()
Close socket without closing file descriptor,
return the latter
s.ioctl()
Control the mode of a socket
15 (Windows only)
15
03 Connection Oriented Sockets

You might also like