Download presentation
Presentation is loading. Please wait.
Published byCorey Black Modified over 9 years ago
1
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012 Part 6: Socket programming
2
2: Application Layer2 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail SMTP, POP3, IMAP r 2.5 DNS r 2.6 P2P applications r 2.7 Socket programming with UDP r 2.8 Socket programming with TCP
3
2: Application Layer3 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API: UDP TCP A application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process socket Goal: learn how to build client/server application that communicate using sockets
4
Socket programming basics r Server must be running before client can send anything to it. r Server must have a socket (door) through which it receives and sends segments r Similarly client needs a socket r Socket is locally identified with a port number Analogous to the apt # in a building r Client needs to know server IP address and socket port number. 2: Application Layer4
5
Application Layer2-5 Socket programming goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end- end-transport protocol Internet controlled by OS controlled by app developer transport application physical link network process transport application physical link network process socket
6
Application Layer2-6 Socket programming Two socket types for two transport services: UDP: unreliable datagram TCP: reliable, byte stream-oriented Application Example: 1. Client reads a line of characters (data) from its keyboard and sends the data to the server. 2. The server receives the data and converts characters to uppercase. 3. The server sends the modified data to the client. 4. The client receives the modified data and displays the line on its screen.
7
Application Layer2-7 Socket programming with UDP UDP: no “connection” between client & server r no handshaking before sending data r sender explicitly attaches IP destination address and port # to each packet r rcvr extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: rUDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server
8
Client/server socket interaction: UDP close clientSocket read datagram from clientSocket create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram via clientSocket create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) read datagram from serverSocket write reply to serverSocket specifying client address, port number Application 2-8 server (running on serverIP ) client
9
Application Layer2-9 Example app: UDP client from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto (message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close() Python UDPClient include Python’s socket library create UDP socket for server get user keyboard input Attach server name, port to message; send into socket print out received string and close socket read reply characters from socket into string
10
Application Layer2- 10 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 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress) Python UDPServer create UDP socket bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client
11
Application Layer2- 11 Socket programming with TCP client must contact server r server process must first be running r server must have created socket (door) that welcomes client’s contact client contacts server by: r Creating TCP socket, specifying IP address, port number of server process r when client creates socket: client TCP establishes connection to server TCP r 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 source port numbers used to distinguish clients (more in Chap 3) TCP provides reliable, in-order byte-stream transfer (“pipe”) between client and server application viewpoint:
12
2: Application Layer12 Socket-programming using TCP TCP service: reliable transfer of bytes from one process to another process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet
13
2: Application Layer13 Client/server socket interaction: TCP wait for incoming connection request connectionSocket = welcomeSocket.accept() create socket, port= x, for incoming request: welcomeSocket = ServerSocket() create socket, connect to hostid, port= x clientSocket = Socket() close connectionSocket read reply from clientSocket close clientSocket Server (running on hostid ) Client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup non-persistent
14
Application Layer2- 14 Client/server socket interaction: TCP wait for incoming connection request connectionSocket = serverSocket.accept() create socket, port= x, for incoming request: serverSocket = socket() create socket, connect to hostid, port= x clientSocket = socket() server (running on hostid ) client send request using clientSocket read request from connectionSocket write reply to connectionSocket TCP connection setup close connectionSocket read reply from clientSocket close clientSocket persistent
15
Application Layer2- 15 Example app: TCP client from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() Python TCPClient create TCP socket for server, remote port 12000 No need to attach server name, port
16
Application Layer2- 16 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 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() Python TCPServer create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket)
17
Application Layer2- 17 Chapter 2: summary r application architectures client-server P2P r application service requirements: reliability, bandwidth, delay r Internet transport service model connection-oriented, reliable: TCP unreliable, datagrams: UDP our study of network apps now complete! specific protocols: HTTP FTP SMTP, POP, IMAP DNS P2P: BitTorrent, DHT socket programming: TCP, UDP sockets
18
Application Layer2- 18 r typical request/reply message exchange: client requests info or service server responds with data, status code r message formats: headers: fields giving info about data data: info being communicated important themes: control vs. data msgs in-band, out-of-band centralized vs. decentralized stateless vs. stateful reliable vs. unreliable msg transfer “complexity at network edge” Chapter 2: summary most importantly: learned about protocols!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.