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

1.Network Programming (Introduction About TCP-IP)(1)

Uploaded by

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

1.Network Programming (Introduction About TCP-IP)(1)

Uploaded by

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

Lecture 1

INTRODUCTION TO NETWORK
PROGRAMMING

Dr. Elhossiny Ibrahim 0 4th year: network programming


Introduction in Network

Computer System

Computer Network

Layering

TCP/IP layering

Network Vs. Transport layers

1
Computer system
➢ The users of a system communicate with the hardware of the system.
➢ The communication can't be direct i.e. a user can't directly give instructions to the
hardware to perform various operations because it is very difficult for the user to
convert its request or instruction into machine language.
➢ An Operating System
➢ It is a software that acts as an intermediate between the hardware and the user.
➢ It is a kind of resource manager that manages both hardware and software resources
of a system.
➢ It control and coordinate the hardware to execute an application required by users.

Figure 2.1: Computer system.


Dr. Elhossiny Ibrahim 2 4th year: network programming
Computer network
❑ Computer networks are the basis of communication in IT. They are used in a
huge variety of ways and can include many different types of network.
❑ A computer network is a set of computers that are connected together by
routers, switches and hubs so that they can share information.
❑ People are combining multiple networks together into an internetwork, or an
Internet. The easiest way to build an Internet is to connect two or more
networks with a router.
❑ Router is often a special-purpose hardware box for connecting networks. The
nice thing about routers is that they provide connections to many different
types of physical networks: Ethernet, token ring, point-to point links, and so on.

Figure 2.2: Computer network.


Dr. Elhossiny Ibrahim 3 4th year: network programming
Layering
❑ Networking protocols are normally developed in layers, with each layer
responsible for a different forms of the communications.

❑ A protocol suite, such as TCP/IP, is the combination of different protocols


at various layers. TCP/IP is normally considered to be a 4-layer system, as
shown in the Figure.

Figure 2.3: The four layers of the TCP/IP protocol suite.


Dr. Elhossiny Ibrahim 4 4th year: network programming
TCP/IP layering
❖ What is the TCP/IP Model?
• TCP/IP stands for Transmission Control Protocol/ Internet Protocol.
• TCP/IP Model helps us to determine how a specific computer should be
connected to the internet and how data should be transmitted between
them over the internet by providing end-to-end communications that
identify how it should be broken into packets, addressed, transmitted,
routed and received at the destination.
• It helps us to create a virtual network when multiple computer, from many
different vendors and running totally different operating systems, are
connected together.
• The purpose of TCP/IP model is to allow communication over large
distances.
• TCP/IP uses the client-server model of communication in which a user or
machine (a client) is provided by a service, like receiving a webpage, by
another computer (a server) in the network.
❖ TCP/IP Characteristics
• Support for a flexible TCP/IP architecture. Adding more system to a
network is easy.
• In TCP/IP protocols suite, the network remains connected while source
and destination machines were functioning properly.

Dr. Elhossiny Ibrahim 5 4th year: network programming


TCP/IP layering
➢ The link layer (data-link layer-network interface layer- physical
layer)
• It includes the device driver in the operating system and the corresponding
network interface card in the computer.
• They together handle all the hardware details of physically interfacing with the
cable (or any media used).
• This layer defines how the frames should be sent physically through the
network.
• It also includes how bits should optically be signaled by hardware devices
which directly interfaces with a network medium like coaxial cables, optical
fiber, or twisted-pair cables.

➢ The network layer (the Internet layer)


• It handles the movement of packets from any computer around the network
until they reach the destination.
• Routing of packets takes place here for example IP (Internet Protocol), ICMP
(Internet Control Message Protocol), and IGMP (Internet Group Management
Protocol) provide the network layer in the TCP/IP protocol suite.
• Message delivery at the network layer does not give any guaranteed to be
reliable network layer protocol.

Dr. Elhossiny Ibrahim 6 4th year: network programming


TCP/IP layering
➢ The transport layer
• This layer builds on the message which are received from the application
layer in order to provide a flow of data between two hosts (from a process
on a source to a process on a destination).
• It determines how much data should be sent and at what rate.
• It helps ensure that data units are delivered error-free and in sequence.
• In the TCP/IP protocol suite there are two vastly different transport
protocols (TCP, UDP)

❑ Transmission Control Protocol (TCP)


• TCP is connection-oriented protocol (establish route before sending data)
• provides a reliable and ordered flow of data between two hosts. It ensures
that data which arrives out of sequence should put back into order.
• It is concerned with things such as:
• Dividing the message passed to it from the application into
appropriately sized parts for the network layer below.
• Acknowledging received packets.
• Performing timeout and retransmission, sends and receives end-to-end
acknowledgments, and so on.

• Because this reliable flow of data is provided by the transport layer, the
application layer can ignore all these details.

Dr. Elhossiny Ibrahim 7 4th year: network programming


TCP/IP layering
❑ User Datagram Protocol (UDP)
• It is a connectionless protocol (no path established)
• It provides a much simpler service to the application layer.
• It just sends packets of data called datagrams from one host to
the other.
• It is unreliable and not ordered, there is no guarantee that the
datagrams reach the other end.
• Any desired reliability must be added by the application layer.

There is a use for each type of transport protocol, which we’ll see
when we look at the different applications that use TCP and UDP.

➢ The application layer


• It handles the details of the particular application. There are many
common TCP/IP applications that almost every implementation
provides:
• Telnet for remote login.
• FTP, the File Transfer Protocol.
• SMTP, the Simple Mail Transfer protocol, for electronic mail.
• SNMP, the Simple Network Management Protocol.

Dr. Elhossiny Ibrahim 8 4th year: network programming


TCP/IP layering
• If we have two hosts on a local area network (LAN) such as an
Ethernet, both running FTP, Figure 2.4 shows the protocols
involved.

FTP
server

Figure 2.4: Two host on a


LAN running FTP.

Dr. Elhossiny Ibrahim 9 4th year: network programming


TCP/IP layering
• Most network applications are designed so that one end is the client and the
other side the server.
• The server provides some types of services to clients, in this case access to
IDes on the server host.
• In the remote login application, Telnet, the service provided to the client is the
ability to login to the server’s host.
• The application layer is represented as a user process while the lower three
layers are usually implemented in the kernel (the operating system) as shown
in Figure 2.4.
• The application layer is concerned with the details of the application and not
with the movement of data across the network.
• The lower three layers know nothing about the application but handle all the
communication details.
• We show four protocols in Figure 2.4, each at a different layer. FTP is an
application layer protocol, TCP is a transport layer protocol, IP is a network
layer protocol, and the Ethernet protocols operate at the link layer.
• The TCP/IP protocol suite is a combination of many protocols such as TCP and
IP and that’s why its famous name TCP/IP protocol suite or Internet Protocol
Suite.
• The purpose of the network interface layer is to handle the details of the
communication media (Ethernet, token ring, etc.),
• While the application layer handles one specific user application (FTP, Telnet, ..
Dr. Elhossiny Ibrahim 10 4th year: network programming
Difference between Network
layer and Transport layer

Fig. 2.5 : Two networks connected with a router


Dr. Elhossiny Ibrahim 11 4th year: network programming
Difference between Network
layer and Transport layer
• To understand the difference, we have to expand our perspective from a single
network to a collection of networks (Internet).
• Figure 2.5 shows an Internet consisting of two networks: an Ethernet and a
token ring, connected with a router. Although we show only two hosts
communicating, with the router connecting the two networks, any host on the
Ethernet can communicate with any host on the token ring.
• In Figure 2.5 we can differentiate between an end system (the two hosts on
either side) and an intermediate system (the router in the middle).
• A router, by definition, has two or more network interface layers (since it
connects two or more networks).

➢ The application layer and the transport layer use end-to-end protocols and are
needed only on the end systems.
➢ The network layer, however, provides a hop-by-hop protocol and is used on the
two end systems and every intermediate system.
✓ The network layer, IP, provides an unreliable service. It does its best job of
moving a packet from its source to its final destination, but there are no
guarantees.
✓ The transport layer, TCP, provides a reliable transport layer using the unreliable
service of IP. To provide this service, TCP performs timeout and retransmission.

Dr. Elhossiny Ibrahim 12 4th year: network programming


Difference between Network
layer and Transport layer
➢ Any system with multiple interfaces is called multi-homed such as mobile phone
might be simultaneously connected to a Wi-Fi network and a 3G network.
➢ A host can be multi-homed but it doesn’t forwards packets from one interface
to another. This can be done in order to increase reliability or performance.
➢ Most TCP/IP implementations allow a multi-homed host to act as a router, but
the host needs to be specifically configured for this to happen.

➢ In this case we can call the system either a host (when an application such as
FTP or Telnet is being used) or a router (when it’s forwarding packets from one
network to another).

➢ One of the goals of an Internet is to hide all the details of the physical layout of
the Internet from the applications. It is obvious from the two network layers in
Figure 2.5 that the application layers don’t care that one host is on an Ethernet,
the other on a token ring or any type of physical interconnections, with a router
between them or many routers. The applications would run the same.
➢ This hiding of the details is what makes the concept of an Internet so powerful
and useful.
➢ Another way to connect networks is with a bridge. These connect networks at
the link layer, while routers connect networks at the network layer. Bridges
makes multiple LANs appear to the upper layers as a single LAN.

Dr. Elhossiny Ibrahim 13 4th year: network programming


Introduction to python
➢ What is Python?
• Python is a popular programming language.
➢ It is used for:
• Web development (server-side).
➢ What can Python do?
• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify files.
➢ Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry Pi).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
• Python runs on an interpreter system, meaning that code can be executed
as soon as it is written. This means that prototyping can be very quick.
• Python can be treated in a procedural way, an object-oriented way or a
functional way

Dr. Elhossiny Ibrahim 14 4th year: network programming


Python Syntax
➢ Python uses new lines to complete a command.

➢ Python relies on indentation, using whitespace, to define scope; such as the


scope of loops, functions and classes.

➢ Print
print("Hello, World!")

Hello, World!
➢ Comment syntax
# This is a comment for one line
"""
This is a comment
written in
more than just one line (put between triple quotes)
""“
➢ Variables
• A variable is created the moment you first assign a value to it.
• Variables do not need to be declared with any particular type, and can even
change type after they have been set.

x=5 # x is of type integer


y = "John" # y is now of type string and is the same as y = ‘John’
print(x) 5
print(y)
John
Dr. Elhossiny Ibrahim 15 4th year: network programming
Python Syntax
➢ Casting
• If you want to specify the data type of a variable, this can be done with
casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

y = 5.24568
y = int(y)
print(y) 5
➢ Output Variables
x = "awesome"
print("Python is " + x) Python is awesome

x = "Python is "
y = "awesome"
z= x+y
print(z) Python is awesome

x=5
y = 10
print(x + y) 15
If you try to combine a string and a number, Python will give you an error.
Dr. Elhossiny Ibrahim 16 4th year: network programming
Python User Input
➢ Python User Input
• This means we are able to ask the user for input, it gets printed on the screen.
The value entered is always string.
username = input("Enter username:") Enter username: ahmed
print("Username is: " + username) Username is: ahmed

• If the user entered number. We must change it into integer by custing.


userage = int(input("Enter userage :")) Enter userage: 20
print(userage) 20

print(“The user age is: ", userage) The user age is: 20

➢ String format()
• allows you to format selected parts of a string.
price = 49
print("The price is {} dollars". format(price))
The price is 49 dollars
• Multiple values
quantity = 3
price = 49
print("I want {} pieces for {}dollars.". format(quantity, price))
I want 3 pieces for 49 dollars.
Dr. Elhossiny Ibrahim 17 4th year: network programming
Python List
➢ Strings are Arrays
a = "Hello, World!“
print(a[0]) H
print(a[3]) L
13
print(len(a)) # length of characters and space

➢ Python List
• Lists are used to store multiple items in a single variable.
• A list is a collection which is ordered and changeable (mutable).
thislist = ["apple", "banana", "cherry"]
print(thislist)
["apple", "banana", "cherry"]
print(thislist[1])
apple
➢ Append Items: To add an item to the end of the list
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
["apple", "banana", "cherry“, "orange")]
Dr. Elhossiny Ibrahim 18 4th year: network programming
Python tuples
➢ Python tuples
• Tuples are used to store multiple items in a single variable.
• A tuple is a collection which is ordered and unchangeable (immutable).
thistuple = ("apple", "banana", "cherry")
print(thistuple)
("apple", "banana", "cherry“)
➢ Change tuple values
x = ("apple", "banana", "cherry")
x = list(x)
x[1] = "kiwi" % change the second item
x.append("orange")
x = tuple(x)
print(x)
("apple", “kiwi", "cherry“, "orange")

➢ Change list values to tuble to be immutable.


x = ["apple", "banana", "cherry"]
x = tuple(x)
("apple", "banana", "cherry“)
Dr. Elhossiny Ibrahim 19 4th year: network programming
Python If ... Else , while
➢ Python If ... Else
a = int(input("Enter the value of a "))
b = int(input("Enter the value of b "))
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
➢ The while loop
i=1 1
while i < 6: 2
print(i) 3
i += 1 # equal i=i+1 4
➢ The break statement 5
i=1
while i < 6:
print(i) 1
if i == 3: 2
break 3
i += 1
Dr. Elhossiny Ibrahim 20 4th year: network programming
Python For Loops
➢ Python For Loops
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x) apple
if x == "banana": banana
break
➢ The range() Function
• To loop through a set of code, a specified number of times.
for x in range(6): 0 2 2
print(x) 1 3 5
for x in range(2, 6): 2 4 8 0
print(x) apple
3 5
for x in range(2, 10, 3): 4 1
print(x) banana
5
2
fruits = ["apple", "banana", "cherry" , "kiwi" , "grapes"] Cherry
for i in range(len(fruits)): Kiwi
print(i) #index number 4
print(fruits[i]) #items grapes

Dr. Elhossiny Ibrahim 21 4th year: network programming


Python Functions
• A function is a block of code which only runs when it is called.
def my_function():
print("Hello from a function")
• To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")

my_function() Hello from a function


• Arguments
def my_function(fname, lname):
print(fname + " " + lname)

my_function("Ahmed", "Omar") Ahmed Omar


my_function("Emil") Error you must enter two arguments

def sum (x, y):


return x+y
x=int(input("please enter the 1st value ")) please enter the 1st value 4
y=int(input("please enter the 2nd value ")) please enter the 2nd value 8
z=sum(x,y)
print("The result is : ", z) The result is : 12
Dr. Elhossiny Ibrahim 22 4th year: network programming
Python Classes/Objects
• Python is an object oriented programming language.
• Create a class named MyClass, with a property named x:
class MyClass:
x=5
• Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x) 5
• Create an object named m, and print the value of x:
class math:
def sum(self, x, y):
return x+y
def sub(self, x, y):
return x - y
def mul(self, x, y):
return x * y
def div(self, x, y):
return x / y
m=math()
x=int(input("please enter the value of x ")) please enter the value of x 10
y=int(input("please enter the value of y ")) please enter the value of y 5
print(“The sum is ", m.sum(x,y)) The sum is 15
print(“The sub is ", m.sub(x,y)) The sub is 5
print(“The mul is ", m.mul(x,y)) The mul is 50
print(“The div is ", m.div(x,y)) The div is 2
Dr. Elhossiny Ibrahim 23 4th year: network programming
Thanks
Dr. Elhossiny Ibrahim 24 4th year: network programming

You might also like