KEMBAR78
Network Programming-Python-13-8-2023.pptx
Tasks/ Experiments Weeks hours Learning Outcomes
Java/Python Overview 2 4 a2, b2, c1to c3 2. 1
Internet Addressing 1 2 a1 3. 2
Socket programming. 2 4 a2, b1, c2 4. 3
The User Datagram Protocol 3 6 a2, b1, c3 5. 4
Multithreaded Applications 3 6 a3, b1, b2, c3, d1to d3 6. 6
Designing Application Protocols 3 6 b1to b3, c2, d1 to d3
Revision 1 2 a1to a3, b1, c1,c2,
Weeks / hours Semester 15 30
A- Knowledge and Understanding
a1. Understand the basic concepts associated with network programming.
a2. Understand the role of a protocol in controlling the communication between hosts in a network.
a3. Know the advantages of multithreaded applications.
B- Intellectual Skills
b1. Distinguish between transport layer protocols.
b2. Design a new simple protocol.
b3. Recognize the significance of flexibility, extendibility, simplicity, and efficiency in protocol
design and implementation.
C- Professional and Practical Skills
c1. Use Java I/O streams and Java exception handling primitives.
c2. Implement practical client/server network protocols, using Java networking API.
c3. Write multithreaded UDP clients and servers.
Topics
• Introduction to Network/Socket Programming
• How socket Work?
• Implementing a server/Client socket
User and System
Programs
Kernel Support
Hardware
Internet Connections (TCP/IP)
Connection socket pair
(128.2.194.242:3479, 208.216.181.15:80)
Server
(port 80)
Client
Client socket address
128.2.194.242:3479
Server socket address
208.216.181.15:80
Client host address
128.2.194.242
Server host address
208.216.181.15
• 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”
Note: 3479 is an
ephemeral port allocated
by the kernel
Note: 80 is a well-known port
associated with Web servers
Using Ports to Identify Services
Web server
(port 80)
Client host
Server host 128.2.194.242
Echo server
(port 7)
Service request for
128.2.194.242:80
(i.e., the Web server)
Web server
(port 80)
Echo server
(port 7)
Service request for
128.2.194.242:7
(i.e., the echo server)
Kernel
Kernel
Client
Client
Client/sever model
• Client asks (request) – server provides (response)
• Typically: single server - multiple clients
• The server does not need to know anything about the client
– even that it exists
• The client should always know something about the server
– at least where it is located
Client
process
Server
process
1. Client sends request
2. Server
handles
request
3. Server sends response
4. Client
handles
response
Resource
Note: clients and servers are processes running on hosts
(can be the same or different hosts).
Network programming :Writing programs that can communicate with other programs or processes across a network.
Programmers use various programming languages, code libraries, and protocols to do the work.
Network/Socket programming
OS
Network
Interface
Client
Appl.
Socket
OS +
Network
APIs
OS
Network
Interface
Server
Appl.
Socket
OS +
Network
APIs
Internet
Server Machine
Client Machine
Network
Applications
• Access to Network via Program Interface
– Sockets make network I/O look like files
– Call system functions to control and communicate
– Network code handles issues of routing, segmentation.
What is a socket?
1. Socket API originated with the 4.2 BSD system released in 1983
2. The interface that the OS provides to its networking subsystem.
3. Sockets provide a common interface to the various protocols supported by networks.
4. Programming objects provided by the operating system that allow your programs to send and receive data.
5. 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.
6. Clients and servers communicate with each by reading from and writing to socket descriptors.
7. The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors.
8. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent.
9. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new
Socket object.
10. The socket address: is a combination of IP address and port. In the TCP/IP layer.
11. Sockets support the simultaneous connection of multiple clients to a single server machine.
12. Types of Internet Sockets
SOCK_STREAM uses TCP (Transmission Control Protocol) Connection oriented and Reliable
SOCK_DGRAM uses UDP (User Datagram Protocol) Connectionless and Unreliable
Socket Logical Structure
Socket
recv buffer
recv buffer
Socket
recv buffer
bytes
bytes
Unix Sockets:
Socket: create an endpoint of communication
Bind: bind a socket to a name or an address
Listen: listen for incoming connections on the socket
Accept: accept an incoming connection request on a socket
Connect: send a connection request to a name (or address) associated with a remote socket
Recv: receive incoming data on a socket from a remote peer
Send: send data to a remote peer via a socket
How do sockets work?
Creating Sockets
socket.socket()
S=socket.socket (socket_family, socket_type, protocol=0)
• socket_family: Represents the address (and protocol) family. It can be either AF_UNIX or AF_INET.
• socket_type: Represents the socket type, and can be either SOCK_STREAM or SOCK_DGRAM.
• protocol: This is an optional argument, and it usually defaults to 0.
Example:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SERVER Side
1-Create a server socket. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2-Bind the socket to a port. s.bind() Binds address (hostname, port number) to socket.
3-Start accepting connections on the socket. s.listen() Sets up and starts TCP listener.
4-Accept connection from clients. s.accept() Accepts TCP client connection.
5-send and receive data to/from clients
CLIENT Side:
1-create client socket : c= socket.socket( socket.AF_INET, socket.SOCK_STREAM )
2-Connect the client socket to listening Server c.connect(server_address)
3-send data to server c.sendall()
4-receive data from server c.recv()
5-close a connected socket c.close()
Implementing a server socket
SERVER Side:
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define host
host = 'localhost'
# define the communication port
port = 8080
# Bind the socket to the port
sock.bind((host, port))
# Listen for incoming connections
sock.listen(1)
# Wait for a connection
print('waiting for a connection')
connection, client = sock.accept()
print(client, 'connected')
# Receive the data in small chunks and retransmit it
data = connection.recv(16)
print('received "%s"' % data)
if data:
connection.sendall(data)
else:
print('no data from', client)
# Close the connection
connection.close()
CLIENT Side:
import socket
# Create a TCP/IP socket
stream_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define host
host = 'localhost'
# define the communication port
port = 8080
# Connect the socket to the port where the server is listening
server_address = ((host, port))
print("connecting")
stream_socket.connect(server_address)
# Send data
message = 'message'
stream_socket.sendall(message.encode())
# response
data = stream_socket.recv(10)
print(data)
print('socket closed')
Implementing a server socket

Network Programming-Python-13-8-2023.pptx

  • 1.
    Tasks/ Experiments Weekshours Learning Outcomes Java/Python Overview 2 4 a2, b2, c1to c3 2. 1 Internet Addressing 1 2 a1 3. 2 Socket programming. 2 4 a2, b1, c2 4. 3 The User Datagram Protocol 3 6 a2, b1, c3 5. 4 Multithreaded Applications 3 6 a3, b1, b2, c3, d1to d3 6. 6 Designing Application Protocols 3 6 b1to b3, c2, d1 to d3 Revision 1 2 a1to a3, b1, c1,c2, Weeks / hours Semester 15 30 A- Knowledge and Understanding a1. Understand the basic concepts associated with network programming. a2. Understand the role of a protocol in controlling the communication between hosts in a network. a3. Know the advantages of multithreaded applications. B- Intellectual Skills b1. Distinguish between transport layer protocols. b2. Design a new simple protocol. b3. Recognize the significance of flexibility, extendibility, simplicity, and efficiency in protocol design and implementation. C- Professional and Practical Skills c1. Use Java I/O streams and Java exception handling primitives. c2. Implement practical client/server network protocols, using Java networking API. c3. Write multithreaded UDP clients and servers.
  • 2.
    Topics • Introduction toNetwork/Socket Programming • How socket Work? • Implementing a server/Client socket
  • 3.
  • 4.
    Internet Connections (TCP/IP) Connectionsocket pair (128.2.194.242:3479, 208.216.181.15:80) Server (port 80) Client Client socket address 128.2.194.242:3479 Server socket address 208.216.181.15:80 Client host address 128.2.194.242 Server host address 208.216.181.15 • 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” Note: 3479 is an ephemeral port allocated by the kernel Note: 80 is a well-known port associated with Web servers
  • 5.
    Using Ports toIdentify Services Web server (port 80) Client host Server host 128.2.194.242 Echo server (port 7) Service request for 128.2.194.242:80 (i.e., the Web server) Web server (port 80) Echo server (port 7) Service request for 128.2.194.242:7 (i.e., the echo server) Kernel Kernel Client Client
  • 6.
    Client/sever model • Clientasks (request) – server provides (response) • Typically: single server - multiple clients • The server does not need to know anything about the client – even that it exists • The client should always know something about the server – at least where it is located Client process Server process 1. Client sends request 2. Server handles request 3. Server sends response 4. Client handles response Resource Note: clients and servers are processes running on hosts (can be the same or different hosts).
  • 7.
    Network programming :Writingprograms that can communicate with other programs or processes across a network. Programmers use various programming languages, code libraries, and protocols to do the work. Network/Socket programming OS Network Interface Client Appl. Socket OS + Network APIs OS Network Interface Server Appl. Socket OS + Network APIs Internet Server Machine Client Machine Network Applications • Access to Network via Program Interface – Sockets make network I/O look like files – Call system functions to control and communicate – Network code handles issues of routing, segmentation.
  • 8.
    What is asocket? 1. Socket API originated with the 4.2 BSD system released in 1983 2. The interface that the OS provides to its networking subsystem. 3. Sockets provide a common interface to the various protocols supported by networks. 4. Programming objects provided by the operating system that allow your programs to send and receive data. 5. 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. 6. Clients and servers communicate with each by reading from and writing to socket descriptors. 7. The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors. 8. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent. 9. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object. 10. The socket address: is a combination of IP address and port. In the TCP/IP layer. 11. Sockets support the simultaneous connection of multiple clients to a single server machine. 12. Types of Internet Sockets SOCK_STREAM uses TCP (Transmission Control Protocol) Connection oriented and Reliable SOCK_DGRAM uses UDP (User Datagram Protocol) Connectionless and Unreliable
  • 9.
    Socket Logical Structure Socket recvbuffer recv buffer Socket recv buffer bytes bytes Unix Sockets: Socket: create an endpoint of communication Bind: bind a socket to a name or an address Listen: listen for incoming connections on the socket Accept: accept an incoming connection request on a socket Connect: send a connection request to a name (or address) associated with a remote socket Recv: receive incoming data on a socket from a remote peer Send: send data to a remote peer via a socket
  • 10.
  • 11.
    Creating Sockets socket.socket() S=socket.socket (socket_family,socket_type, protocol=0) • socket_family: Represents the address (and protocol) family. It can be either AF_UNIX or AF_INET. • socket_type: Represents the socket type, and can be either SOCK_STREAM or SOCK_DGRAM. • protocol: This is an optional argument, and it usually defaults to 0. Example: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • 12.
    SERVER Side 1-Create aserver socket. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 2-Bind the socket to a port. s.bind() Binds address (hostname, port number) to socket. 3-Start accepting connections on the socket. s.listen() Sets up and starts TCP listener. 4-Accept connection from clients. s.accept() Accepts TCP client connection. 5-send and receive data to/from clients CLIENT Side: 1-create client socket : c= socket.socket( socket.AF_INET, socket.SOCK_STREAM ) 2-Connect the client socket to listening Server c.connect(server_address) 3-send data to server c.sendall() 4-receive data from server c.recv() 5-close a connected socket c.close() Implementing a server socket
  • 13.
    SERVER Side: import socket #Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Define host host = 'localhost' # define the communication port port = 8080 # Bind the socket to the port sock.bind((host, port)) # Listen for incoming connections sock.listen(1) # Wait for a connection print('waiting for a connection') connection, client = sock.accept() print(client, 'connected') # Receive the data in small chunks and retransmit it data = connection.recv(16) print('received "%s"' % data) if data: connection.sendall(data) else: print('no data from', client) # Close the connection connection.close() CLIENT Side: import socket # Create a TCP/IP socket stream_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Define host host = 'localhost' # define the communication port port = 8080 # Connect the socket to the port where the server is listening server_address = ((host, port)) print("connecting") stream_socket.connect(server_address) # Send data message = 'message' stream_socket.sendall(message.encode()) # response data = stream_socket.recv(10) print(data) print('socket closed') Implementing a server socket