0% found this document useful (0 votes)
2 views33 pages

1 Python TCP Socket Intro(1)

The document provides an introduction to socket programming, detailing its significance in enabling applications to communicate over networks. It covers the definition of sockets, their types, and the steps to implement client/server connections using TCP and UDP protocols. Additionally, it highlights the efficiency and flexibility of socket programming in developing a wide range of applications, from simple chat programs to complex web applications.

Uploaded by

aboodalqery10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views33 pages

1 Python TCP Socket Intro(1)

The document provides an introduction to socket programming, detailing its significance in enabling applications to communicate over networks. It covers the definition of sockets, their types, and the steps to implement client/server connections using TCP and UDP protocols. Additionally, it highlights the efficiency and flexibility of socket programming in developing a wide range of applications, from simple chat programs to complex web applications.

Uploaded by

aboodalqery10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‫‪Introduction to socket programming‬‬

‫ﻋﻠﻰ ﺍﻟﻤﺘﺪﺭﺏ ﺍﺟﺎﺩﺓ ﺍﻟﻤﻬﺎﺭﺍﺕ ﺍﻟﻤﻌﺮﻓﻴﺔ ﻭﺍﻟﻌﻤﻠﻴﺔ ﺍﻟﺘﺎﻟﻴﺔ‬


‫ﺗﻌﺮﻳﻒ ﺑﺮﻣﺠﺔ ﺍﻟﺸﺒﻜﺔ ‪ ،‬ﺑﺮﻧﺎﻣﺞ ﺍﻟﺸﺒﻜﺔ‬ ‫‪.1‬‬
‫ﺗﻌﺮﻳﻒ ﺍﻟﻤآﺧﺬ‪ ، Socket‬ﻧﺸﺄﺗﻪ ‪ ،‬ﻋﻼﻗﺘﻪ ﺑﻨﻈﺎﻡ ﺍﻟﺘﺸﻐﻴﻞ ﻭﻟﻐﺎﺕ ﺍﻟﺒﺮﻣﺠﺔ‪ .‬ﺃﻧﻮﺍﻋﻪ) ﺍﻻﺗﺼﺎﻝ ‪ ،‬ﺍﻷﺳﻠﻮﺏ(‬ ‫‪.2‬‬
‫ﻣﻌﺮﻓﺔ ﻋﻨﻮﺍﻥ ﻭﻣﻨﻔﺬ ﺍﻟﺴﻮﻛﺖ‬ ‫‪.3‬‬
‫ﺳﺮﺩ ﺍﻟﺨﻄﻮﺍﺕ ﺍﻟﻼﺯﻣﺔ ﺑﺘﺮﺗﻴﺐ ﺻﺤﻴﺢ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ )‪Client/Server(TCP‬‬ ‫‪.4‬‬
‫ﺳﺮﺩ ﺍﻷﻭﺍﻣﺮ ﺍﻟﻼﺯﻣﺔ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ )‪Client/Server(TCP‬‬ ‫‪.5‬‬
‫ﺳﺮﺩ ﻛﺎﻓﺔ ﺧﻴﺎﺭﺍﺕ ‪ ،‬ﻭﻇﺎﺋﻒ ‪ .Socket‬ﻣﻌﺮﻓﺔ ﻭﻓﻬﻢ ﺃﻭﺍﻣﺮ ﺍﻟـ‪ Socket‬ﺍﻟﺘﻲ ﺗﻌﻤﻞ ‪ Block‬ﻟﻠﺒﺮﻧﺎﻣﺞ‬ ‫‪.6‬‬
‫ﺍﻟﻘﺪﺭﺓ ﻋﻠﻰ ﻛﺘﺎﺑﺔ ﻭﺗﻨﻔﻴﺬ ﻛﻮﺩ ﺑﺎﻳﺜﻮﻥ )‪ Client/Server(TCP‬ﻛـ ‪) Echo Server‬ﻣﻬﺎﺭﺓ(‬ ‫‪.7‬‬
‫ﺍﻟﻘﺪﺭﺓ ﻋﻠﻰ ﻛﺘﺎﺑﺔ ﻭﺗﻨﻔﻴﺬ ﻛﻮﺩ ﺑﺎﻳﺜﻮﻥ )‪ Client/Server(UDP‬ﻛـ ‪) Echo Server‬ﻣﻬﺎﺭﺓ(‬ ‫‪.8‬‬
Socket Programming: The Future of Application Development
First, it allows applications to communicate with each other over a network. This makes
it possible to create distributed applications that can be accessed from anywhere in the
world.
Second, socket programming is very efficient. It allows applications to communicate
directly with each other, without going through a central server. This can improve
performance and reduce latency.
Third, socket programming is very flexible. It can be used to create a wide variety of
applications, from simple chat programs to complex web applications.

‫ ﻭﻫﺬﺍ ﻳﺠﻌﻞ ﻣﻦ ﺍﻟﻤﻤﻜﻦ ﺇﻧﺸﺎﺀ ﺗﻄﺒﻴﻘﺎﺕ ﻣﻮﺯﻋﺔ ﻳﻤﻜﻦ ﺍﻟﻮﺻﻮﻝ‬.‫ ﺗﺴﻤﺢ ﻟﻠﺘﻄﺒﻴﻘﺎﺕ ﺑﺎﻟﺘﻮﺍﺻﻞ ﻣﻊ ﺑﻌﻀﻬﺎ ﺍﻟﺒﻌﺾ ﻋﺒﺮ ﺷﺒﻜﺔ‬،‫ﺃﻭﻻ‬
.‫ﺇﻟﻴﻬﺎ ﻣﻦ ﺃﻱ ﻣﻜﺎﻥ ﻓﻲ ﺍﻟﻌﺎﻟﻢ‬
.‫ ﺩﻭﻥ ﺍﻟﻤﺮﻭﺭ ﻋﺒﺮ ﺧﺎﺩﻡ ﻣﺮﻛﺰﻱ‬،‫ ﻓﻬﻲ ﺗﺴﻤﺢ ﻟﻠﺘﻄﺒﻴﻘﺎﺕ ﺑﺎﻟﺘﻮﺍﺻﻞ ﻣﺒﺎﺷﺮﺓ ﻣﻊ ﺑﻌﻀﻬﺎ ﺍﻟﺒﻌﺾ‬.‫ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻓﻌﺎﻟﺔ ﻟﻠﻐﺎﻳﺔ‬،‫ﺛﺎﻧًﻴﺎ‬
.‫ﻭﻫﺬﺍ ﻣﻦ ﺷﺄﻧﻪ ﺗﺤﺴﻴﻦ ﺍﻷﺩﺍﺀ ﻭﺗﻘﻠﻴﻞ ﺯﻣﻦ ﺍﻟﻮﺻﻮﻝ‬
‫ ﻣﻦ ﺑﺮﺍﻣﺞ ﺍﻟﺪﺭﺩﺷﺔ ﺍﻟﺒﺴﻴﻄﺔ ﺇﻟﻰ‬،‫ ﻭﻳﻤﻜﻦ ﺍﺳﺘﺨﺪﺍﻣﻬﺎ ﻹﻧﺸﺎﺀ ﻣﺠﻤﻮﻋﺔ ﻭﺍﺳﻌﺔ ﻣﻦ ﺍﻟﺘﻄﺒﻴﻘﺎﺕ‬.‫ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻣﺮﻧﺔ ﻟﻠﻐﺎﻳﺔ‬،‫ﺛﺎﻟًﺜﺎ‬
.‫ﺗﻄﺒﻴﻘﺎﺕ ﺍﻟﻮﻳﺐ ﺍﻟﻤﻌﻘﺪﺓ‬
:‫ﻓﻴﻤﺎ ﻳﻠﻲ ﺑﻌﺾ ﺍﻷﻣﺜﻠﺔ ﺣﻮﻝ ﻛﻴﻔﻴﺔ ﺍﺳﺘﺨﺪﺍﻡ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻓﻲ ﺗﻄﻮﻳﺮ ﺍﻟﺘﻄﺒﻴﻘﺎﺕ‬
Web browsers , Email clients, File sharing programs
Introduction to socket programming
1. What is Network/Socket Programming? , Network Application?

2. What is the socket ,Its Types(connections, Modes), and how Work?

3. Socket Addresses :IP, Port

4. Steps of Implementing a Simple TCP Client/Server socket

5. Socket methods to Implementing a simple TCP/UDP Client/Server socket

6. List Socket Methods, Options, Modes ,

7. Write down python code for Echo TCP Client/Server:(Practice skill Lab1)

8. Write down python code for Echo UDP Client/Server:(Practice skill Lab1)
Socket Programming: The Future of Application Development
First, it allows applications to communicate with each other over a network. This makes
it possible to create distributed applications that can be accessed from anywhere in the
world.
Second, socket programming is very efficient. It allows applications to communicate
directly with each other, without going through a central server. This can improve
performance and reduce latency.
Third, socket programming is very flexible. It can be used to create a wide variety of
applications, from simple chat programs to complex web applications.

‫ ﻭﻫﺬﺍ ﻳﺠﻌﻞ ﻣﻦ ﺍﻟﻤﻤﻜﻦ ﺇﻧﺸﺎﺀ ﺗﻄﺒﻴﻘﺎﺕ ﻣﻮﺯﻋﺔ ﻳﻤﻜﻦ ﺍﻟﻮﺻﻮﻝ‬.‫ ﺗﺴﻤﺢ ﻟﻠﺘﻄﺒﻴﻘﺎﺕ ﺑﺎﻟﺘﻮﺍﺻﻞ ﻣﻊ ﺑﻌﻀﻬﺎ ﺍﻟﺒﻌﺾ ﻋﺒﺮ ﺷﺒﻜﺔ‬،‫ﺃﻭﻻ‬
.‫ﺇﻟﻴﻬﺎ ﻣﻦ ﺃﻱ ﻣﻜﺎﻥ ﻓﻲ ﺍﻟﻌﺎﻟﻢ‬
.‫ ﺩﻭﻥ ﺍﻟﻤﺮﻭﺭ ﻋﺒﺮ ﺧﺎﺩﻡ ﻣﺮﻛﺰﻱ‬،‫ ﻓﻬﻲ ﺗﺴﻤﺢ ﻟﻠﺘﻄﺒﻴﻘﺎﺕ ﺑﺎﻟﺘﻮﺍﺻﻞ ﻣﺒﺎﺷﺮﺓ ﻣﻊ ﺑﻌﻀﻬﺎ ﺍﻟﺒﻌﺾ‬.‫ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻓﻌﺎﻟﺔ ﻟﻠﻐﺎﻳﺔ‬،‫ﺛﺎﻧًﻴﺎ‬
.‫ﻭﻫﺬﺍ ﻣﻦ ﺷﺄﻧﻪ ﺗﺤﺴﻴﻦ ﺍﻷﺩﺍﺀ ﻭﺗﻘﻠﻴﻞ ﺯﻣﻦ ﺍﻟﻮﺻﻮﻝ‬
‫ ﻣﻦ ﺑﺮﺍﻣﺞ ﺍﻟﺪﺭﺩﺷﺔ ﺍﻟﺒﺴﻴﻄﺔ ﺇﻟﻰ‬،‫ ﻭﻳﻤﻜﻦ ﺍﺳﺘﺨﺪﺍﻣﻬﺎ ﻹﻧﺸﺎﺀ ﻣﺠﻤﻮﻋﺔ ﻭﺍﺳﻌﺔ ﻣﻦ ﺍﻟﺘﻄﺒﻴﻘﺎﺕ‬.‫ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻣﺮﻧﺔ ﻟﻠﻐﺎﻳﺔ‬،‫ﺛﺎﻟًﺜﺎ‬
.‫ﺗﻄﺒﻴﻘﺎﺕ ﺍﻟﻮﻳﺐ ﺍﻟﻤﻌﻘﺪﺓ‬
:‫ﻓﻴﻤﺎ ﻳﻠﻲ ﺑﻌﺾ ﺍﻷﻣﺜﻠﺔ ﺣﻮﻝ ﻛﻴﻔﻴﺔ ﺍﺳﺘﺨﺪﺍﻡ ﺑﺮﻣﺠﺔ ﺍﻟﻤﻘﺒﺲ ﻓﻲ ﺗﻄﻮﻳﺮ ﺍﻟﺘﻄﺒﻴﻘﺎﺕ‬
Web browsers , Email clients, File sharing programs
What is Network programming ?
 Writing programs that can communicate with other machines/ processes across a network.
 Programmers use various programming languages, code libraries, and protocols to do the work.

Socket: low-level C (BSD)Unix

Ported To All OS

Windows
Winsock
, Mac, Linux,… Library

Implemented By Many Language

Python , Java , C++ ,C#,….

Network Applications.
What is a socket?
1. low-level C networking API originated with the 4.3BSD Unix system released in 1983

2. Programming objects provided by the OSs that allow your programs to send and receive data.

3. The interface that the OS provides to its networking subsystem and various protocols supported by networks.

4. To the kernel, is an endpoint of a two-way communication link between two programs running on the network.

1. To an application, is a file descriptor that lets the application read/write from/to the network.

5. Clients and servers communicate with each by reading from and writing to socket descriptors.

6. The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.

7. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent.

8. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object.

9. The socket address: is a combination of IP address and port. In the TCP/IP layer.

10. Sockets support the simultaneous connection of multiple clients to a single server machine.
What is a socket?

NIC Kernel Socket API App/Process


Network Applications

Client Machine Server


Server Machine
Machine

Client Server
Appl. Appl.

OS + OS +
Network Socket Socket Network
APIs APIs

Network Network
Interface Internet Interface

OS OS
Internet/Network Connections Types
TCP Connection UDP Connection
For TCP protocols For UDP protocols
Reliable delivery Unrelible delivery
Guaranteed correct ordering of packets No order guaranteed
Connection-oriented No notion of connection(UDP)
Bidirectional Not Bidirectional
•Establish a reliable byte-stream between two
•Just fire off messages towards destinations
hosts
•They may not get there
•Data always arrives
•They may arrive out of order
•Data arrives in order
TCP Stream Socket (SOCK_STREAM) UDP Datagram Socket (SOCK_DGRAM)
TCP Real Application UDP Real Application
TCP Real Application UDP Real Application
1-Text Messaging: Whatsapp,instegram 1-DNS
2-FTP: Filezilla 2-VOIP: Viber, WhatsApp voice call ,Wi-Fi calling
3-HTTP 3-Video Conference: Skype ,Google meet ,Zoom
4-SMTP: Yahoo Mail, Google Mail,outlook 4-On-Line Game: all on-line Games
Client/sever model
1. Client asks (request) – server provides (response)
2. Typically: single server - multiple clients
3. The server does not need to know anything about the client
– even that it exists
4. The client should always know something about the server
– at least where it is located(Domain name /Host Name/IP Address)

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).
Internet Connections (TCP/IP) socket-address
• Address the machine on the network -By IP address
• Address the process -By the “port”-number
• The pair of IP-address + port – makes up a “socket-address”

Client socket address Server socket address


[Link]:3479 [Link]:80

Server
Client Connection socket pair (port 80)
([Link]:3479, [Link]:80)
Client host address Server host address
[Link] [Link]

Note: 3479 is an Note: 80 is a well-known port


ephemeral port allocated associated with Web servers
by the kernel
Using Ports to Identify Services
Server host [Link]

Client host Service request for Web server


[Link]:80 (port 80)
(i.e., the Web server)
Client Kernel
Echo server
(port 7)

Service request for Web server


[Link]:7 (port 80)
(i.e., the echo server)
Client Kernel
Echo server
(port 7)
Sockets and Ports
 A socket is an endpoint of a connection Identified by IP address and Port number
 To receive messages, a socket must be bound to a local port and IP address.
 Messages sent to a (address, port) can be received only by a process whose socket is
associated with that (address, port).
 A process can not share ports with others.
 May use the same socket for sending and receiving.
 Each socket is associated with a protocol.
Ports
 Each host has 65,536 ports
 Some ports are reserved for specific apps
 20,21: FTP
 23: Telnet
 80: HTTP
 see RFC 1700 :about 2000 ports are reserved
Client/Server Connection (Network Application)

Note: clients and servers are processes running on hosts


(can be the same or different hosts).
Socket Logical Structure

NIC Kernel Socket API App/Process


Python Client/Server Processing (TCP-Connection)
Python Client/Server Processing (TCP-Connection)
Socket Primitives Methods
The Python socket module

What is the steps, and the sequence of creating client/server TCP socket connection?
What is the input and output of each function? socket(),bind(),listen(),accept(),send(),recv()
‫ﺍﻟﺨﻄﻮﺍﺕ ﺍﻟﻀﺮﻭﺭﻳﺔ ﻭﺍﻟﻤﺮﺗﺒﺔ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ ‪TCP-Cient/Server connection‬‬

‫ﺟﻬﺔ ﺍﻟﺨﺎﺩﻡ‪:‬‬
‫‪ .1‬ﺍﺳﺘﻴﺮﺍﺩ ﻣﻜﺘﺒﺔ ﺍﻟﻤﻘﺒﺲ‪،‬‬

‫‪ .2‬ﺍﻧﺸﺎﺀ ﻣﻘﺒﺲ‪ TCP‬ﺟﺪﻳﺪ ﻟﻠﺨﺎﺩﻡ ﻭﺗﺤﺪﻳﺪ ﺧﻴﺎﺭﺍﺗﻪ‪،‬‬

‫‪ .3‬ﺭﺑﻂ‪/‬ﺗﺤﺪﻳﺪ ﻋﻨﻮﺍﻥ ﻭﻣﻨﻔﺬ )‪ (IP,Port‬ﻣﻘﺒﺲ ﺍﻟﺨﺎﺩﻡ ﻟﻼﺗﺼﺎﻝ ﺑﻪ‪،‬‬

‫‪ .4‬ﺗﺤﺪﻳﺪ ﻋﺪﺩ ﺍﻻﺗﺼﺎﻻﺕ ﺍﻟﻮﺍﺭﺩﺓ ﺍﻟﺘﻲ ﺳﻴﺴﺘﻤﻊ ﻟﻬﺎ ﻣﻘﺒﺲ ﺍﻟﺨﺎﺩﻡ‪،‬‬

‫‪ .5‬ﻗﺒﻮﻝ ﺍﺗﺼﺎﻝ ﺍﻟﻌﻤﻴﻞ ﻭﺣﻔﻈﻪ ﻛﻤﻘﺒﺲ ﺟﺪﻳﺪ ﻳﻤﺜﻞ ﺍﻟﻌﻤﻴﻞ‪،‬‬

‫‪ .6‬ﺍﺳﺘﻘﺒﺎﻝ‪/‬ﺍﺭﺳﺎﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﻣﻦ‪/‬ﺍﻟﻰ ﺍﻟﻌﻤﻴﻞ ﻋﺒﺮ ﻣﻘﺒﺴﻪ ‪،‬‬

‫‪ .7‬ﺍﻏﻼﻕ ﻣﻘﺒﺲ ﺍﻟﻌﻤﻴﻞ ﺍﻭ ﺍﻻﺳﺘﻤﺮﺍﺭ ﻓﻲ ﺗﺒﺎﺩﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﻣﻌﻪ ﺑﺎﻟﺮﺟﻮﻉ ﻟﻠﺨﻄﻮﺓ ‪،6‬‬

‫‪ .8‬ﺍﻏﻼﻕ ﻣﻘﺒﺲ ﺍﻟﺨﺎﺩﻡ ‪ ،‬ﺍﻭ ﺍﻻﺳﺘﻤﺮﺍﺭ ﻓﻲ ﻗﺒﻮﻝ ﺍﺗﺼﺎﻝ آﺧﺮ ﺑﺎﻟﺮﺟﻮﻉ ﻟﻠﺨﻄﻮﺓ ‪5‬‬

‫‪ .9‬ﺍﻧﻬﺎﺀ ﺗﻄﺒﻴﻖ ﺍﻟﺨﺎﺩﻡ‬

‫ﺟﻬﺔ ﺍﻟﻌﻤﻴﻞ‪:‬‬
‫‪ .1‬ﺍﺳﺘﻴﺮﺍﺩ ﻣﻜﺘﺒﺔ ﺍﻟﻤﻘﺒﺲ‪،‬‬

‫‪ .2‬ﺍﻧﺸﺎﺀ ﻣﻘﺒﺲ‪ TCP‬ﺟﺪﻳﺪ ﻟﻼﺗﺼﺎﻝ ﺑﺎﻟﺨﺎﺩﻡ ﻭﺗﺤﺪﻳﺪ ﻧﻮﻋﻪ ‪،‬‬

‫‪ .3‬ﺍﻻﺗﺼﺎﻝ ﺑﻌﻨﻮﺍﻥ ﻭﻣﻨﻔﺬ )‪ (IP,Port‬ﺍﻟﺨﺎﺩﻡ‬

‫‪ .4‬ﺍﺭﺳﺎﻝ‪/‬ﺳﺘﻘﺒﺎﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﻣﻦ‪/‬ﺍﻟﻰ ﺍﻟﺨﺎﺩﻡ ﻋﺒﺮ ﻣﻘﺒﺴﻪ ‪،‬‬

‫‪ .5‬ﺍﻏﻼﻕ ﺍﻟﻤﻘﺒﺲ ﺍﻭ ﺍﻻﺳﺘﻤﺮﺍﺭ ﻓﻲ ﺗﺒﺎﺩﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﺑﺎﻟﺮﺟﻮﻉ ﻟﻠﺨﻄﻮﺓ ‪،4‬‬

‫‪ .6‬ﺍﻧﻬﺎﺀ ﺗﻄﺒﻴﻖ ﺍﻟﻌﻤﻴﻞ‬


‫ﺍﻟﺨﻄﻮﺍﺕ ﺍﻟﻀﺮﻭﺭﻳﺔ ﻭﺍﻟﻤﺮﺗﺒﺔ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ ‪UDP-Client/Server connection‬‬

‫ﺟﻬﺔ ﺍﻟﺨﺎﺩﻡ‪:‬‬
‫‪ .1‬ﺍﺳﺘﻴﺮﺍﺩ ﻣﻜﺘﺒﺔ ﺍﻟﻤﻘﺒﺲ‪،‬‬

‫‪ .2‬ﺍﻧﺸﺎﺀ ﻣﻘﺒﺲ‪ UDP‬ﺟﺪﻳﺪ ﻟﻠﺨﺎﺩﻡ ﻭﺗﺤﺪﻳﺪ ﺧﻴﺎﺭﺍﺗﻪ‪،‬‬

‫‪ .3‬ﺭﺑﻂ‪/‬ﺗﺤﺪﻳﺪ ﻋﻨﻮﺍﻥ ﻭﻣﻨﻔﺬ )‪ (IP,Port‬ﻣﻘﺒﺲ ﺍﻟﺨﺎﺩﻡ ﻟﻼﺗﺼﺎﻝ ﺑﻪ‪،‬‬

‫‪ .4‬ﺍﺳﺘﻘﺒﺎﻝ ﺃﻱ ﺑﻴﺎﻧﺎﺕ ﻭﺍﺭﺩﺓ ﻣﻦ ﺃﻱ ﻋﻤﻴﻞ ‪/‬ﻭﺍﻻﺭﺳﺎﻝ ﺍﻟﻴﻪ ‪،‬‬

‫‪ .5‬ﺍﻏﻼﻕ ﻣﻘﺒﺲ ﺍﻟﺨﺎﺩﻡ ﺍﻭ ﺍﻻﺳﺘﻤﺮﺍﺭ ﻓﻲ ﺍﺳﺘﻘﺒﺎﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﺍﻟﻮﺍﺭﺩﺓ ﺑﺎﻟﺮﺟﻮﻉ ﻟـ‪4‬‬

‫‪ .6‬ﺍﻧﻬﺎﺀ ﺗﻄﺒﻴﻖ ﺍﻟﺨﺎﺩﻡ‬

‫ﺟﻬﺔ ﺍﻟﻌﻤﻴﻞ‪:‬‬
‫‪ .1‬ﺍﺳﺘﻴﺮﺍﺩ ﻣﻜﺘﺒﺔ ﺍﻟﻤﻘﺒﺲ‪،‬‬

‫‪ .2‬ﺍﻧﺸﺎﺀ ﻣﻘﺒﺲ‪ UDP‬ﺟﺪﻳﺪ ﻟﻼﺗﺼﺎﻝ ﺑﺎﻟﺨﺎﺩﻡ ﻭﺗﺤﺪﻳﺪ ﻧﻮﻋﻪ ‪،‬‬

‫‪.3‬ﺍﺭﺳﺎﻝ‪/‬ﺍﺳﺘﻘﺒﺎﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﺍﻟﻰ‪/‬ﻣﻦ ﺃﻱ ﺧﺎﺩﻡ ‪UDP‬‬

‫‪ .4‬ﺍﻏﻼﻕ ﺍﻟﻤﻘﺒﺲ ﺍﻭ ﺍﻻﺳﺘﻤﺮﺍﺭ ﻓﻲ ﺗﺒﺎﺩﻝ ﺍﻟﺒﻴﺎﻧﺎﺕ ﺑﺎﻟﺮﺟﻮﻉ ﻟﻠﺨﻄﻮﺓ ‪،3‬‬

‫‪ .5‬ﺍﻧﻬﺎﺀ ﺗﻄﺒﻴﻖ ﺍﻟﻌﻤﻴﻞ‬


Python Steps to Implementing a client/server connection(TCP)

SERVER Side
0-Import python Library import socket
1-Create a server listening socket. s_soc=[Link]( )
2-Bind the socket to a (IP, port). s_soc=bind(host, port)
3-Start Listening to client connections. S_soc=listen(n)
4-Accept connection from clients and create client socket. c_soc,c_add=S_soc.accept() .
5-Receive data from clients in_data=c_soc.recv(1024)
5-Send data to clients c_soc.send(mydata)
7-Close client socket c_soc.close()
8-Close Server socket s_soc.close()

CLIENT Side:
0-Import python Library import socket
1-Create client socket : s_soc=[Link]()
2-Connect client socket to listening Server s_soc.connect(host,port)
3-send data to server s_soc.send(mydata)
4-receive data from server in_data=s_soc.recv(1024)
5-close a connected socket s_soc.close()

What is the steps, and the sequence of creating client/server TCP socket connection?
What is the input and output of each function? socket(),bind(),listen(),accept(),send(),recv()
Implement TCP-Socket Echo client/server connection
import socket # Import socket module
s_soc = [Link]() #1 Create a socket object
s_soc.bind((“[Link]”, 12345)) #2 Bind to the port
s_soc.listen(1) #3 listen to one conn at a time .
c_soc, c_add = s_soc.accept() #4 Establish connection with client.
i_data= c_soc.recv(1024) #5,6-send/receive data
c_soc.send(b'Thank you for connecting') (2 SOCKETS)
c_soc.close() #7 Close the connection

import socket # Import socket module


c_soc = [Link]() #1 Create a socket object
c_soc.connect((“[Link]”, 12345)) #2 Connect to the server
o_data=b’Hello’
c_soc.send(o_data)
i_data= c_soc.recv(1024) #3,4-send/receive data
c_soc.close #5Close the socket when done

What is the steps, and the sequence of creating client/server TCP socket connection?
What is the input and output of each function? socket(),bind(),listen(),accept(),send(),recv()

Create python Code and run it within 20 minutes


Implementing TCP-Socket Echo Client/server (Blocking Methods)
import socket
s_soc = [Link]()
s_soc.bind( (IP, port) )
s_soc.listen(1)
c_soc, c_add = s_soc.accept() # Block Main Process Until client connected
i_data= c_soc.recv(1024) # Block until get data or error raised
c_soc.send(b'Thank you for connecting') # Block until sent data or error raised
c_soc.close()

import socket
c_soc = [Link]()
c_soc.connect((IP, port)) # Block Main Process Until server accept connection
o_data=b’Hello’
c_soc.send(o_data) # Block until sent data or error raised
i_data= c_soc.recv(1024) # Block until get data or error raised
c_soc.close

Why server code dose not continue running?


Read about socket modes( Block/non-Block/Timeout ) slide
Create socket Function parameters
s = [Link] (socket_family, socket_type, protocol=0)
ADDRESS FAMILIES: Domain(Layer 3 Address type)
• socket.AF_LOCAL • IPC Unix domain protocol AF_UINIX (NOT Windows…)
• socket.AF_INET • Internet IP v4 Protocol
• socket.AF_INET6 • Internet IP v6 protocol • Rather more complicated …
• socket.AF_PACKET • used to send/receive raw packet at device driver l(OSI layer 2) level

SOCKET TYPES(Layer4:TCP/UDP)
• socket.SOCK_STREAM • TCP, connection-oriented (Layer 4)
• socket.SOCK_DGRAM • UDP, connectionless(Layer 4)
• socket.SOCK_RAW • Gives access to subnetwork layer(Layer 3,2)
• socket.SOCK_SEQPACKET • sequenced packet socket

Socket Protocol :Communication protocol, if set to 0 Kernel choose


• socket.IPPROTO_TCP
• socket.IPPROTO_UDP
• socket.IPPROTO_RAW :IP layer(OSI layer 3)
• socket.IPPROTO_SCTP :stream control transmission protocol
Create socket Function parameters
s = [Link] (socket_family, socket_type, protocol=0)
OSI-Layer2 OSI-Layer3 OSI-Layer4
RAW Socket RAW Socket Socket
AF_INET AF_INET
Address Family AF_PACKET
AF_INET6 AF_INET6
SOCK_STREAM ,
Socket Type SOCK_RAW SOCK_RAW SOCK_DGRAM
SOCK_SEQPACKET
Access Only root User Only root User Any user
PDU Header access Yes, data link Yes ,IP packet Not available
or construction frame header header
Stream I/O no no Yes , except SOCK_DGRAM socket
Socket Programming challenges
Develpers must handle:
• Network errors.
• Connections timeouts
• Data serialization
• Network Security considerations
• Encryption(SSL/TLS)
• Autentication mechanisms
• Input Validation
• Connection timeout manaemnet

Concurrent Socket handling.


Asynchronous socket Programming: Techniques for non-blockin I/O operations
1. Multithreading
2. Asynio
3. Event-driven Programming

Network Programming Protocols


Protocol Characteristics Use Cases
TCP Connection oriented ,reliable Web servers, email
UDP Connectionless ,fast Streaming , gamin
WebSocket Full-duplex communication Real-time applications
Socket Programming challenges
Socket Programming patterns.
pattern Description Use Cases
Request-Response Synchronous communication REST,APIs
Client/Sever
Streaming Continues data transmission Video Streaming
Pub-Sub Message Broadcasting Chat applications
‫)‪Lab1(50 minuets‬‬

‫ﺍﻷﻫﺪﺍﻑ‪:‬‬
‫‪ .1‬ﻣﻌﺮﻓﺔ ﺍﻟﺨﻄﻮﺍﺕ ﺍﻟﻀﺮﻭﺭﻳﺔ ﺍﻟﻤﺮﺗﺒﺔ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ ﻣﺎ ﺑﻴﻦ ﺣﺎﺳﺐ ﻗﺮﻳﺐ ﻭﺣﺎﺳﺐ ﺑﻌﻴﺪ‪.‬‬

‫‪ .2‬ﻣﻌﺮﻓﺔ ﻭﻇﺎﺋﻒ ﺍﻟﺴﻮﻛﺖ ﺍﻟﻀﺮﻭﺭﻳﺔ ﻹﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ ﻣﺎ ﺑﻴﻦ ﺣﺎﺳﺐ ﻗﺮﻳﺐ ﻭﺣﺎﺳﺐ ﺑﻌﻴﺪ‪.‬‬

‫‪ .3‬ﻣﺎ ﻫﻲ ﻭﻇﺎﺋﻒ ﺍﻟﺴﻮﻛﺖ ﺍﻟﺘﻲ ﺗﻌﻤﻞ ﺣﺠﺰ ﻟﻠﺘﻄﺒﻴﻖ‪.‬‬


‫‪ .4‬ﺃﻫﻤﻴﺔ ﺗﺤﻮﻳﻞ ﺍﻟﺒﻴﺎﻧﺎﺕ ﺍﻟﻰ ﺻﻴﻐﺔ ‪ Bytes‬ﻗﺒﻞ ﺍﺭﺳﺎﻟﻬﺎ ﻋﻴﺮ ﺍﻟﺸﺒﻜﺔ‪.‬‬

‫‪ .5‬ﻣﻌﺮﻓﺔ ﻛﻴﻔﻴﺔ ﺟﻌﻞ ﺍﻟﺨﺎﺩﻡ ﻳﻨﺘﻈﺮ ﻻﺗﺼﺎﻻﺕ ﺍﻟﻌﻤﻼﺀ ﻣﻦ ﺧﻼﻝ ‪Loop‬‬

‫‪ .6‬ﻣﻌﺮﻓﺔ ﺃﻧﻮﺍﻉ ﺃﺧﻄﺎﺀ ﺍﻟﺴﻮﻛﻴﺖ ﻭﻛﻴﻔﻴﺔ ﺗﺼﻴﺪﻫﺎ‪.‬‬


‫‪ .7‬ﻣﻌﺮﻓﺔ ﺍﻧﺸﺎﺀ ﺍﺗﺼﺎﻝ ﺧﺎﺩﻡ‪/‬ﻋﻤﻴﻞ ﺑﻮﺍﺳﻄﺔ ﺍﺳﺘﺨﺪﺍﻡ ‪ ، with‬ﻭﻋﺒﺮ ﺍﻟـ‪Class‬‬

‫‪ .8‬ﺗﻘﺴﻴﻢ ﺗﻄﺒﻴﻖ ﺧﺎﺩﻡ‪/‬ﻋﻤﻴﻞ ﺍﻟﻰ ﺩﻭﺍﻝ ﻣﻨﻌﺰﻟﺔ‪.‬‬

‫‪ .9‬ﺗﻌﻴﻴﻦ ﺑﻌﺾ ﺧﻴﺎﺭﺍﺕ ﺍﻟﺴﻮﻛﻴﺖ‬

‫‪.01‬ﺃﻫﻤﻴﺔ ﺍﻇﻬﺎﺭ ﺭﺳﺎﺋﻞ ﺍﺭﺷﺎﺩﻳﺔ ﻟﻠﻤﺴﺘﺨﺪﻡ‬


Lab1(50 minuets)
Topic 1-TCP-Socket Basics: Example Echo Client/Server Application
1. Necessary Steps/socket methods To create TCP- Socket Client/Server Echo App. [Link]
2. Using Loop for accepting Clients Connection and clients data [Link] (iterative Server)
3. Catch Socket Errors by using ty-exception and Set Socket Options. [Link]
4. Client/Server Echo : using with statement (context manager). 0-4-TCPechoS_With.py
5. Client/Server Echo : using (Class:OOP). 0-5-TCPechoS_Class.py
6. Client/Server Echo : using [Link]() Function. 0-6-TCPechoS_GetAddrInfo.py
7. Client/Server Echo : Divided App. into Functions. 0-7-TCPechoS_Functions.py
8. Client/Server Echo: Set/Get Socket Timeout option . 0-8-TCPechoS_Timeout.py

.‫ ﻭﺍﻷﻓﻜﺎﺭ ﺍﻟﻮﺍﺭﺩﺓ ﻓﻴﻪ‬، ‫ﺍﺳﺘﻌﺮﺍﺽ ﻛﻞ ﻛﻮﺩ ﻋﻠﻰ ﺣﺪﻩ ﻭﺍﻟﺘﺮﻛﻴﺰ ﻋﻠﻰ ﻓﻬﻢ ﻋﻤﻠﻪ‬ .1
.‫ﺇﻋﻄﺎﺀ ﻓﺮﺻﺔ ﻟﻠﻤﺘﺪﺭﺏ ﺗﻨﻔﻴﺬﻩ ﺍﻟﻜﻮﺩ ﺍﻥ ﺍﻣﻜﻦ‬ .2
. ‫ﺍﺳﺘﻘﺒﺎﻝ ﺃﻱ ﻣﻘﺘﺮﺣﺎﺕ ﺍﻭ ﺗﺴﺎﺅﻼﺕ ﻣﻦ ﻗﺒﻞ ﺍﻟﻤﺘﺪﺭﺑﻴﻦ‬ .3
.‫ ﻭﺍﺳﺘﻘﺒﺎﻝ ﺍﻹﺟﺎﺑﺎﺕ ﻣﻨﻬﻢ‬، ‫ﻃﺮﺡ ﺑﻌﺾ ﺍﻟﺘﺴﺎﺅﻼﺕ ﻟﻠﻤﺘﺪﺭﺑﻴﻦ ﺣﻮﻝ ﺍﻟﻜﻮﺩ‬ .4
. ‫ﻃﺮﺡ ﺇﺿﺎﻓﺎﺕ ﻣﻘﺘﺮﺣﺔ ﻭﻣﻄﺎﻟﺒﺔ ﺍﻟﻤﺘﺪﺭﺑﻴﻦ ﻣﺤﺎﻭﻟﺔ ﺣﻠﻬﺎ ﻭﺗﻘﺪﻳﻤﻬﺎ ﻓﻲ ﺍﻟﻤﺤﺎﺿﺮﺓ ﺍﻟﻘﺎﺩﻣﺔ‬ .5
Display message for User in server side:
1-Start of program : Server Program Starting
2-After socket created: Server Connection Socket Created On
3-After bind method : Server Socket bounded TO IP…. , PORT ……
4-After listen method: server listening and wat for incoming connection from clients
5-After accept method :Server Accept Connection from Client that has IP:…,PORT:…..
6-when send data: Send Data
7-when read data :Read Data
8-when close socket : Server Connection closed
6-when error : for each step: use try and check if no error else end program

# Display message for User in Client side:


1-Start of program : Client Program Starting
2-After connect method : Connected to Server IP:……. On PORT:…… , From Client IP:….. ,PORT:……
3-when send data: Send data
4-when read data: Read Data
5-when close socket : Client Connection closed
6-when error: for each step: use try and check if no error else end program

After Client-Server Connection Success:


Display Client Address (ip , port)
Display Server Address (ip , port)
Socket Application As functions
Server Side

1. Create_listen_socket (host, port, address_family,socket_type) ->lsock

2. listening_Clients(lsock) client_sock,client_add

3. Receive_Data(client_sock)->data_bytes

4. Send_Data(client_sock , data_bytes)

5. Encoding_Data(data , encoding, byte_order)data_bytes

6. Dcoding_Data(data_bytes , encoding)->data

7. Prepair_Data (data ,pad_char , delimiter_char)ready_data

8. Close_Socket(sock)

9. An other utility Functions

[Link] Function
Socket Application As functions
Client Side

1. Create_listen_socket (host, port, address_family,socket_type) ->Client_sock

2. Receive_Data(client_sock)->data_bytes

3. Send_Data(client_sock , data_bytes)

4. Encoding_Data(data , encoding, byte_order)data_bytes

5. Dcoding_Data(data_bytes , encoding)->data

6. Prepair_Data(data ,pad_char , delimiter_char)ready_data

7. Close_Socket(sock)

8. An other utility Functions

9. Main Function

You might also like