Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Lecture2 P1 Computer Networks Protocols (UDP TCP IP)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture2 P1 Computer Networks Protocols (UDP TCP IP)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

1

Module
2 Outlines

Module 2(Sockets)

1.Basics of Sockets - Low-level networking interface


 Definition, Sockets type, UDP & TCP sockets
2.Socket Methods
3.IPv4
4.Simple Client/Server Programming
5.Practical example (Client/Server)
6.H.W2

2
Module
2 Before starting …

3
Module
2 So …

4
Module
2 Definition of Sockets

• Socket is a link between two applications that can communicate with


one another (either locally on a single machine or remotely between
two machines in separate locations) —> (within a process, between
processes on the same machine or between processes on different
machines).
• Basically, sockets act as a communication link between two entities,
i.e. a server and a client. A server will give out information being
requested by a client. For example, when you (client) visited this
(https://esu.ac.ae/) page, the browser created a socket and connected
you to the server of the university website.
• So, socket is a communication mechanism.

5
Module
2 So: What is socket?

 First appeared in ARPANET in 1971, and later


became an API in the Berkeley Software
Distribution (BSD) operating system released in
1983 called Berkeley sockets (4.1 BSD UNIX
1982).
 An integer associated with a network connection.
 Implemented as file descriptor.
 Types of Internet Sockets:
• Datagram Socket for UDP communication.
• Stream Sockets for TCP connection.
• Raw Sockets.
An application programming interface (API):
is a set of protocols, routines, functions and/or commands that programmers
use to facilitate interaction between distinct software services.
APIs allow one software service to access data from another software service
without the developer's needing to know how the other service works.
6
Module
2 So: What is in a 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.

1. It can uniquely identify devices connected to the network through an addressing


system. Whenever a device gets access to the Internet (whether it's a switch, PC, or
other devices), it is assigned a unique, numerical IP address such as
192.149.252.76. The IPv4 uses a 32-bit address scheme allowing to store 2^32
addresses (4.19 billion addresses).

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

ipaddress — IPv4/IPv6 manipulation library

• Provides the capabilities to create, manipulate and operate on IPv4 and IPv6
addresses and networks.

• The functions and classes in this module make it straightforward to handle


various tasks related to IP addresses, including checking whether or not two
hosts are on the same subnet, iterating over all hosts in a particular
subnet, checking whether or not a string represents a valid IP address or
network definition, and so on.

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

# Extracting the IP version:


addr1Ver = ipaddress.ip_address('192.0.2.1') # Iterating through the “usable” addresses on a network:
print('address 1 version: ', addr1Ver.version) net4 = ipaddress.ip_network('192.0.2.0/24')
for x in net4.hosts():
addr2Ver = ipaddress.ip_address('2001:db8::1') print('host', x)
print('address 2 version: ', addr2Ver.version)
# Obtaining the netmask (i.e. set bits corresponding to the network prefix)
# Obtaining the network from an interface # or the hostmask (any bits that are not part of the netmask):
host1Net = ipaddress.ip_interface('192.0.2.1/24') net4 = ipaddress.ip_network('192.0.2.0/24')
print('host 1 network: ', host1Net.network) print('Mask network4: ', net4.netmask)
print('Mask host4: ', net4.hostmask)
host2Net = ipaddress.ip_interface('2001:db8::1/96')
print('host 2 network: ', host2Net.network) net6 = ipaddress.ip_network('2001:db8::0/96')
print('Mask network6: ', net6.netmask)
# Finding out how many individual addresses are in a network: print('Mask host6: ', net6.hostmask)
net4 = ipaddress.ip_network('192.0.2.0/24')
print('Individual network4: ', net4.num_addresses)

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

To implement socket programming in python, we need to use the


Socket module. Following is a simple syntax to create a Socket:

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

# Create a TCP socket


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the address given on the command line


# server_name = sys.argv[1] if len(sys.argv) > 1 else '.'

server_address = ('localhost', 10000)


print('starting up on port :', server_address)
sock.bind(server_address)
sock.listen(5)

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

# Create a TCP/IP socket


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening


server_address = ('localhost', 10000)
print('connecting to port :', server_address)
sock.connect(server_address)

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:

1- syntax error (1)


2- logic error (1)
3- runtime error (1)
4- operational error (1)

39
Module
2 HW 2

Q2:

40

You might also like