This document provides an overview of network programming in Python using sockets. It discusses what sockets are and how they allow communication within and between processes. It describes how to create sockets in Python using the socket module, specifying the socket family, type, and protocol. It covers common server socket methods like bind(), listen(), accept(), and connect(). It also discusses general socket methods like recv(), send(), recvfrom(), sendto(), and close(). The document provides examples of an echo server and client to illustrate TCP communication and a UDP sender and receiver example. It compares TCP and UDP and how data is transmitted differently between the two protocols.
This document provides an overview of network programming in Python using sockets. It discusses what sockets are and how they allow communication within and between processes. It describes how to create sockets in Python using the socket module, specifying the socket family, type, and protocol. It covers common server socket methods like bind(), listen(), accept(), and connect(). It also discusses general socket methods like recv(), send(), recvfrom(), sendto(), and close(). The document provides examples of an echo server and client to illustrate TCP communication and a UDP sender and receiver example. It compares TCP and UDP and how data is transmitted differently between the two protocols.
communications channel. • Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. • Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. • The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. Socket Module
• To create a socket, you must use the socket.socket()
function in socket module, which has the general syntax: s = socket.socket (socket_family, socket_type, protocol=0) socket_family: This is either AF_UNIX or AF_INET, as explained earlier. socket_type: This is either SOCK_STREAM or SOCK_DGRAM. protocol: This is usually left out, defaulting to 0. • Once you have socket object, then you can use required functions to create your client or server program. Server Socket Methods
• s.bind() This method binds address
(hostname, port number pair) to socket. • s.listen() This method sets up and start TCP listener. • s.accept() This passively accept TCP client connection, waiting until connection arrives (blocking). • s.connect() This method actively initiates TCP server connection. General Socket Methods
• s.recv() This method receives TCP message
• s.send() This method transmits TCP message • s.recvfrom() This method receives UDP message • s.sendto() This method transmits UDP message • s.close() This method closes socket • socket.gethostname() Returns the hostname. Example:
• Write a server program to accept the data
from client, convert it to upper case then send it back to the client. Server Client Output How the communication is taken place? Echo Server
This is an echo server: the server that echoes
back all data it receives to a client that sent it. Echo Server Echo Client Output: TCP vs. UDP How UDP works? UDP Sender UDP Receiver Output: Thank you This presentation is created using LibreOffice Impress 4.2.8.2, can be used freely as per GNU General Public License