Lecture2 P1 Computer Networks Protocols (UDP TCP IP)
Lecture2 P1 Computer Networks Protocols (UDP TCP IP)
Module
2 Outlines
Module 2(Sockets)
2
Module
2 Before starting …
3
Module
2 So …
4
Module
2 Definition of Sockets
5
Module
2 So: What is socket?
7
Module
2 How do I do a socket?
8
Module
2 UDP socket
9
Module
2 TCP socket
10
Module
2 Starting with Python
import socket
host_name = socket.gethostname()
ip_address = socket.gethostbyname(host_name)
print('Host name: ', host_name)
print('IP address: ', ip_address)
import socket
remote_host = 'www.python.org'
print('IP address of :',(remote_host,socket.gethostbyname(remote_host)))
err_msg= socket.error()
print('Is there any error?')
print(remote_host, err_msg)
11
Module
2 IPv4 (INTERNET Protocol v4)
Definition
IPv4 is the fourth version of IP, which establishes the rules for
computer networks functioning on the principle of packet exchange.
2. The increasing end-users connected to the Internet leads to the exhaustion of IPv4
addresses. That’s also why the new Internet addressing system, IPv6, is being
deployed to fulfill the need for more Internet addresses.
12
Module
2 IPv4 (INTERNET Protocol v4)
13
Module
2 IPv4 (INTERNET Protocol v4)
Features of IPv4
1. Connectionless Protocol
2. Allow creating a simple virtual communication layer over diversified
devices
3. It requires less memory and ease of remembering addresses
4. Already supported protocol by millions of devices
5. Offers video libraries and conferences
14
Module
2 Public IP vs Private IP
15
Module
2 Public IP vs Private IP
16
Module
2 IPv4 in Python
• Provides the capabilities to create, manipulate and operate on IPv4 and IPv6
addresses and networks.
17
Module
2 IPv4 in Python
To practice:
ipify : get your public IP address library. Try, installing ipify in your
computer, then find your public IP address.
18
Module
2 IPv4: Built-in functions
import ipaddress
net6 = ipaddress.ip_network('2001:db8::0/96')
print('Individual network6: ', net6.num_addresses) 19
Module
network service access
2
in Python
There are two levels of network service access in Python. These are:
1. Low-Level Access
You can access the basic socket support in the underlying operating
system using python’s libraries, which allows you to implement clients and
servers for both connection-oriented and connectionless protocols.
2. High-Level Access
Python also has libraries that provide higher-level access to specific
application-level network protocols, such as FTP, HTTP, and so on.
20
Module
2 Sockets in Python
• Sockets are created using a set of programming requests called socket API
(Application Programming Interface). Python's socket library offers classes
for handling common transports as a generic interface.
• Sockets use protocols for determining the connection type for port-to-port
communication between client and server machines. The protocols are used
for:
1. Domain Name Servers (DNS)
2. IP addressing
3. E-mail
4. FTP (File Transfer Protocol) etc...
21
Module
2 IP & Ports
22
Module
2 Standard ports in Python
import socket
def find_service_name():
protocolname = ‘tcp'
for port in [21, 22, 23, 25, 80, 110, 119, 443]:
print ("Port: %s => service name: %s" %(port, socket.getservbyport(port, protocolname)))
print ("Port: %s => service name: %s" %(53, socket.getservbyport(53,'udp')))
if __name__ == '__main__':
find_service_name()
23
Module
2 Sockets terms
1.Domain
Domain is the family of protocols that is used as the transport mechanism. These values are
constants such as AF_INET for IPv4, AF_INET6 for IPv6, PF_UNIX, PF_X25, and so on.
2. Type
Type means the kind of communication between two endpoints, typically SOCK_STREAM (TCP) for
connection-oriented protocols and SOCK_DGRAM (UDP) for connectionless protocols.
3. Protocol
This may be used to identify a variant of a protocol within a domain and type. Its default
value is 0. This is usually left out.
4. Hostname
This works as the identifier of a network interface. A hostname may be a string, a dotted-
quad address, or an IPV6 address in colon (and possibly dot) notation.
5. Port
Each server listens for clients calling on one or more ports. A port may be a Fixnum
24 port
number, a string containing a port number, or the name of a service.
Module
2 Python’s Socket Module
25
Module
2 Python’s Socket Module
Socket_family:
AF_INET is used for IPv4 Internet addressing.
AF_INET6 is used for IPv6 Internet addressing.
AF_UNIX is the address family for Unix Domain Sockets (UDS)
Socket_type:
SOCK_DGRAM for user datagram protocol (UDP) connection-less.
SOCK_STREAM for transmission control protocol (TCP) connection-
oriented protocols.
26
Module
2 Create Socket in Python
import socket
UDP socket
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
import socket
TCP socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
27
Module Sockets Methods
2 (Server)
28
Module Sockets Methods
2 (Server)
import socket
import sys
while True:
print('waiting for a connection')
connection, client_address = sock.accept()
,
,
,
29
Module Sockets Methods
2 (Client)
30
Module Sockets Methods
2 (Client)
import socket
import sys
31
Module Sockets Methods
2 (General)
32
Module Simple Client/Server
2 Programming
The Steps in Establishing a Socket on Server
1. Create a socket with the socket() system call
2. Bind the socket to an address using the bind() system call. For a server
socket, an address consists of a port number on the host machine
3. Listen for connections with listen() system call
4. Accept a connection with the accept() system call. This call typically
blocks until a client connects with the server
5. Send and receive data with the send(), and finally close the connection
with the close()
The Steps in Establishing a Socket on Client
1. Create a socket with the socket() system call
2. Connect the socket to the address of the server using the connect() system
call
3. Send and receive data. There are a number of ways to do this, but the
simplest is to use the read() and write() or send() and recv() system calls
33
Module Practical example
2 (ClientTCP/ServerTCP)
34
Module Practical example
2 (ClientTCP/ServerTCP)
35
Module Practical example
2 (ClientTCP/ServerTCP)
36
Module Practical example
2 (ClientTCP/ServerTCP)
37
Module Practical example
2 (ClientTCP/ServerTCP)
38
Module
2 HW 2
Q1: Find the following errors in HW2 program between the client and the
server:
39
Module
2 HW 2
Q2:
40