Socket Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of socket programming, focusing on client-server applications using TCP and UDP protocols in Python. It explains concepts like socket APIs, different socket types, client-server interaction, and examples demonstrating how to create and use sockets.
Full Transcript
Socket Programming Socket Programming goal: learn how to build client/server applications that communicate over the Internet ○ Socket API (Application Programming Interface): The interface between the application and the network (transport layer) Part of all maj...
Socket Programming Socket Programming goal: learn how to build client/server applications that communicate over the Internet ○ Socket API (Application Programming Interface): The interface between the application and the network (transport layer) Part of all major OSes and languages; originally Berkeley (Unix) ~1983 Application developer has control of everything on the application-layer side of the socket little control of the transport-layer side of the socket the choice of transport protocol the ability to fix a few transport layer parameters such as maximum buffer and maximum segment size Sockets socket: door between application process and end-end-transport protocol Socket API Two types of sockets for two transport services ○ TCP: reliably send a stream of bytes ○ UDP: unreliably send separate messages Application Example: ○ client reads a line of characters (data) from its keyboard and sends data to server ○ server receives the data and converts characters to uppercase ○ server sends modified data to client ○ The source codes are provided in Mylearningspace Network Applications General format for many network applications ○ Simple Client Send requests to the server Wait for a reply Extract the information from the reply Continue… ○ Simple Server Wait to be contacted by the server Server handles the Client requests Multi-threaded (multi-process) Example: ○ File transfer: send name, get file ○ Web browsing: send URL, get page Socket Programming with UDP UDP: no “connection” between client & server ○ no handshaking before sending data ○ sender explicitly attaches IP destination address and port # to each packet ○ receiver extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: ○ UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Socket API: UDP Socket socket() socket() bind() sendto() recvfrom() recvfrom() sendto() close() close() Client/Server Socket Interaction: UDP Example App: UDP Client from socket import * serverName = 'hostname' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_DGRAM) message = input('Input lowercase sentence:') clientSocket.sendto(message.encode(),(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print(modifiedMessage.decode()) Example App: UDP Server from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print('The server is ready to receive') while True: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.decode().upper() serverSocket.sendto(modifiedMessage.encode(),clientAddress) Socket Programming with TCP Connection-oriented ○ Client and sever first need to handshake and establish a connection client contacts server by creating TCP socket, specifying IP address, port number of server process (and client process) server must have created socket (door) that welcomes client’s contact welcoming socket when client creates socket: client TCP establishes connection to server TCP when contacted by client, server TCP creates new socket for server process to communicate with that particular client ○ allows server to talk with multiple clients ○ client source port # and IP address used to distinguish clients To send data, client just drop data into the TCP connection via its socket In UDP, the client/server must attach a destination address to the packet before dropping it into the socket Socket Programming with TCP Socket API: TCP Socket socket() socket() bind() connect() listen() send() accept() receive receive() close() send() close() Socket API socket Create a new communication endpoint bind Associate a local address (port) with a socket listen Announce willingness to accept connections; (give queue size) accept Passively establish an incoming connection connect Actively attempt to establish a connection send Send some data over the connection receive Receive some data from the connection close Release the connection Client/Server Socket Interaction: TCP Example App: TCP Client from socket import * serverName = 'servername' serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = input('Input lowercase sentence:') clientSocket.send(sentence.encode()) modifiedSentence = clientSocket.recv(1024) print ('From Server:', modifiedSentence.decode()) clientSocket.close() Example App: TCP Server from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind(('',serverPort)) serverSocket.listen(1) print('The server is ready to receive') while True: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024).decode() capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence.encode()) connectionSocket.close() Question 1 For the client-server application over TCP, does the server program have to be executed before the client program? A. True B. False Question 2 For the client-server application over UDP, does the server program have to be executed before the client program? A. True B. False