收藏 分享(赏)

02Sockets高级计算机网络.ppt

上传人:dzzj200808 文档编号:3319504 上传时间:2018-10-12 格式:PPT 页数:38 大小:638.50KB
下载 相关 举报
02Sockets高级计算机网络.ppt_第1页
第1页 / 共38页
02Sockets高级计算机网络.ppt_第2页
第2页 / 共38页
02Sockets高级计算机网络.ppt_第3页
第3页 / 共38页
02Sockets高级计算机网络.ppt_第4页
第4页 / 共38页
02Sockets高级计算机网络.ppt_第5页
第5页 / 共38页
点击查看更多>>
资源描述

1、1,Networked Applications: Sockets,COS 461: Computer NetworksJennifer Rexford,2,Goals of Todays Lecture,Client-server paradigm End systems Clients and servers Sockets Socket abstraction Socket programming in UNIX File-Transfer Protocol (FTP) Uploading and downloading files Separate control and data c

2、onnections,3,End System: Computer on the Net,Internet,Also known as a “host”,4,Clients and Servers,Client program Running on end host Requests service E.g., Web browser,Server program Running on end host Provides service E.g., Web server,GET /index.html,“Site under construction”,5,Clients Are Not Ne

3、cessarily Human,Example: Web crawler (or spider) Automated client program Tries to discover & download many Web pages Forms the basis of search engines like Google Spider client Start with a base list of popular Web sites Download the Web pages Parse the HTML files to extract hypertext links Downloa

4、d these Web pages, too And repeat, and repeat, and repeat,6,Client-Server Communication,Client “sometimes on” Initiates a request to the server when interested E.g., Web browser on your laptop or cell phone Doesnt communicate directly with other clients Needs to know the servers address,Server is “a

5、lways on” Services requests from many client hosts E.g., Web server for the Web site Doesnt initiate contact with the clients Needs a fixed, well-known address,7,Peer-to-Peer Communication,No always-on server at the center of it all Hosts can come and go, and change addresses Hosts may have a diffe

6、rent address each time Example: peer-to-peer file sharing Any host can request files, send files, query to find where a file is located, respond to queries, and forward queries Scalability by harnessing millions of peers Each peer acting as both a client and server,8,Client and Server Processes,Prog

7、ram vs. process Program: collection of code Process: a running program on a host Communication between processes Same end host: inter-process communication Governed by the operating system on the end host Different end hosts: exchanging messages Governed by the network protocols Client and server pr

8、ocesses Client process: process that initiates communication Server process: process that waits to be contacted,9,Socket: End Point of Communication,Sending message from one process to another Message must traverse the underlying network Process sends and receives through a “socket” In essence, the

9、doorway leading in/out of the house Socket as an Application Programming Interface Supports the creation of network applications,socket,socket,User process,User process,Operating System,Operating System,10,Identifying the Receiving Process,Sending process must identify the receiver Name or address o

10、f the receiving end host Identifier that specifies the receiving process Receiving host Destination address that uniquely identifies the host An IP address is a 32-bit quantity Receiving process Host may be running many different processes Destination port that uniquely identifies the socket A port

11、number is a 16-bit quantity,11,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 se

12、rver),OS,OS,Client,Client,12,Knowing What Port Number To Use,Popular applications have well-known ports E.g., port 80 for Web and port 25 for e-mail Well-known ports listed at http:/www.iana.org Well-known vs. ephemeral ports Server has a well-known port (e.g., port 80) Between 0 and 1023 Client pic

13、ks an unused ephemeral (i.e., temporary) port Between 1024 and 65535 Uniquely identifying the traffic between the hosts Two IP addresses and two port numbers Underlying transport protocol (e.g., TCP or UDP),13,Delivering the Data: Division of Labor,Network Deliver data packet to the destination host

14、 Based on the destination IP address Operating system Deliver data to the destination socket Based on the protocol and destination port # Application Read data from the socket Interpret the data (e.g., render a Web page),14,UNIX Socket API,Socket interface Originally provided in Berkeley UNIX Later

15、adopted by all popular operating systems Simplifies porting applications to different OSes In UNIX, everything is like a file All input is like reading a file All output is like writing a file File is represented by an integer file descriptor System calls for sockets Client: create, connect, write,

16、read, close Server: create, bind, listen, accept, read, write, close,15,Typical Client Program,Prepare to communicate Create a socket Determine server address and port number Initiate the connection to the server Exchange data with the server Write data to the socket Read data from the socket Do stu

17、ff with the data (e.g., render a Web page) Close the socket,16,Creating a Socket: socket(),Operation to create a socket int socket(int domain, int type, int protocol) Returns a descriptor (or handle) for the socket Originally designed to support any protocol suite Domain: protocol family PF_INET for

18、 the Internet Type: semantics of the communication SOCK_STREAM: reliable byte stream SOCK_DGRAM: message-oriented service Protocol: specific protocol UNSPEC: unspecified (PF_INET and SOCK_STREAM already implies TCP),17,Connecting the Socket to the Server,Translating the servers name to an address st

19、ruct hostent *gethostbyname(char *name) Argument: the name of the host (e.g., “”) Returns a structure that includes the host address Identifying the services port number struct servent *getservbyname(char *name, char *proto) Arguments: service (e.g., “ftp”) and protocol (e.g., “tcp”) Establishing th

20、e connection int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen) Arguments: socket descriptor, server address, and address size Returns 0 on success, and -1 if an error occurs,18,Sending and Receiving Data,Sending data ssize_t write(int sockfd, void *buf, size_t len) Argume

21、nts: socket descriptor, pointer to buffer of data to send, and length of the buffer Returns the number of characters written, and -1 on error Receiving data ssize_t read(int sockfd, void *buf, size_t len) Arguments: socket descriptor, pointer to buffer to place the data, size of the buffer Returns t

22、he number of characters read (where 0 implies “end of file”), and -1 on error Closing the socket int close(int sockfd),19,Byte Ordering: Little and Big Endian,Hosts differ in how they store data E.g., four-byte number (byte3, byte2, byte1, byte0) Little endian (“little end comes first”) Intel PCs! L

23、ow-order byte stored at the lowest memory location Byte0, byte1, byte2, byte3 Big endian (“big end comes first”) High-order byte stored at lowest memory location Byte3, byte2, byte1, byte 0 IP is big endian (aka “network byte order”) Use htons() and htonl() to convert to network byte order Use ntohs

24、() and ntohl() to convert to host order,20,Why Cant Sockets Hide These Details?,Dealing with endian differences is tedious Couldnt the socket implementation deal with this by swapping the bytes as needed? No, swapping depends on the data type Two-byte short int: (byte 1, byte 0) vs. (byte 0, byte 1)

25、 Four-byte long int: (byte 3, byte 2, byte 1, byte 0) vs. (byte 0, byte 1, byte 2, byte 3) String of one-byte charters: (char 0, char 1, char 2, ) in both cases Socket layer doesnt know the data types Sees the data as simply a buffer pointer and a length Doesnt have enough information to do the swap

26、ping,21,Servers Differ From Clients,Passive open Prepare to accept connections but dont actually establish one until hearing from a client Hearing from multiple clients Allow a backlog of waiting clients . in case several try to start a connection at once Create a socket for each client Upon accepti

27、ng a new client create a new socket for the communication,22,Typical Server Program,Prepare to communicate Create a socket Associate local address and port with the socket Wait to hear from a client (passive open) Indicate how many clients-in-waiting to permit Accept an incoming connection from a cl

28、ient Exchange data with the client over new socket Receive data from the socket Do stuff to handle the request (e.g., get a file) Send data to the socket Close the socket Repeat with the next connection request,23,Server Preparing its Socket,Bind socket to the local address and port number int bind

29、(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) Arguments: socket descriptor, server address, address length Returns 0 on success, and -1 if an error occurs Define how many connections can be pending int listen(int sockfd, int backlog) Arguments: socket descriptor and acceptable backlog Re

30、turns 0 on success, and -1 on error,24,Accepting a New Client Connection,Accept a new connection from a client int accept(int sockfd, struct sockaddr *addr, socketlen_t *addrlen) Arguments: socket descriptor, structure that will provide client address and port, and length of the structure Returns de

31、scriptor for a new socket for this connection Questions What happens if no clients are around? The accept() call blocks waiting for a client What happens if too many clients are around? Some connection requests dont get through But, thats okay, because the Internet makes no promises,25,Putting it Al

32、l Together,socket(),bind(),listen(),accept(),read(),write(),Server,block,process request,Client,socket(),connect(),write(),establish connection,send request,read(),send response,26,Serving One Request at a Time?,Serializing requests is inefficient Server can process just one request at a time All ot

33、her clients must wait until previous one is done Need to time share the server machine Alternate between servicing different requests Do a little work on one request, then switch to another Small tasks, like reading HTTP request, locating the associated file, reading the disk, transmitting parts of

34、the response, etc. Or, start a new process to handle each request Allow the operating system to share the CPU across processes Or, some hybrid of these two approaches,27,Wanna See Real Clients and Servers?,Apache Web server Open source server first released in 1995 Name derives from “a patchy server

35、” ;-) Software available online at http:/www.apache.org Mozilla Web browser http:/www.mozilla.org/developer/ Sendmail http:/www.sendmail.org/ BIND Domain Name System Client resolver and DNS server http:/www.isc.org/index.pl?/sw/bind/ ,28,Advice for Assignment #1,Start early (even if March 1 seems fa

36、r away) Familiarize yourself with the socket API Read the online references Read the manual pages (e.g., “man socket”) Feeling self-referential? Do “man man”! Write a simple socket program first E.g., simple echo program E.g., simple FTP client that connects to server Learn about the File Transfer P

37、rotocol Assignment #1 Web page Request For Comments 959,29,File Transfer Protocol (FTP),Allows a user to copy files to/from remote hosts Client program connects to FTP server and provides a login id and password and allows the user to explore the directories and download and upload files with the se

38、rver A predecessor of the Web (RFC 959 in 1985) Requires user to know the name of the server machine and have an account on the machine and find the directory where the files are stored and know whether the file is text or binary and know what tool to run to render and edit the file That is, no URL,

39、 hypertext, and helper applications,30,FTP Protocol,Control connection (on server port 21) Client sends commands and receives responses Connection persists across multiple commands FTP commands Specification includes more than 30 commands Each command ends with a carriage return and a line feed (“rn

40、” in C) Server responds with a three-digit code and optional human-readable text (e.g., “226 transfer completed”) Try it at the UNIX prompt ftp ftp.cs.princeton.edu Id “anonymous” and password as your e-mail address,31,Example Commands,Authentication USER: specify the user name to log in as PASS: sp

41、ecify the users password Exploring the files LIST: list the files for the given file specification CWD: change to the given directory Downloading and uploading files TYPE: set type to ASCII (A) or binary image (I) RETR: retrieve the given file STOR: upload the given file Closing the connection QUIT:

42、 close the FTP connection,32,Server Response Codes,1xx: positive preliminary reply The action is being started but expect another reply before sending the next command. 2xx: positive completion reply The action succeeded and a new command can be sent. 3xx: positive intermediate reply The command was

43、 accepted but another command is now required. 4xx: transient negative completion reply The command failed and should be retried later. 5xx: permanent negative completion reply The command failed and should not be retried.,33,FTP Data Transfer,Separate data connection To send lists of files (LIST) T

44、o retrieve a file (RETR) To upload a file (STOR),control,data,34,Creating the Data Connection,Client acts like a server Creates a socket Client acquires an ephemeral port number Binds an address and port number Waits to hear from the FTP server,control,socket,35,Creating Data Connection (cont.),But,

45、 the server doesnt know the port number So, the client tells the server the port number Using the PORT command on the control connection,PORT ,36,Creating Data Connection (cont),Then, the server initiates the data connection Connects to the socket on the client machine and the client accepts to comp

46、lete the connection,socket,37,Why Out-of-Band Control?,Avoids need to mark the end of the data transfer Data transfer ends by closing of data connection Yet, the control connection stays up Aborting a data transfer Can abort a transfer without killing the control connection which avoids requiring th

47、e user to log in again Done with an ABOR on the control connection Third-party file transfer between two hosts Data connection could go to a different hosts by sending a different client IP address to the server E.g., user coordinates transfer between two servers,38,Conclusions,Client-server paradig

48、m Model of communication between end hosts Client asks, and server answers Sockets Simple byte-stream and messages abstractions Common application programmable interface File-Transfer Protocol (FTP) Protocol for downloading and uploading files Separate control and data connections Next Monday: IP packet switching!,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报