University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Networking Concepts, Socket Programming Spring 2012Lecture Overview ! Application layer ! Client-server ! Application requirements ! Background ! TCP vs. UDP ! Byte ordering ! Socket I/O ! TCP/UDP server and client ! I/O multiplexingClient-Server Paradigm Typical network app has two pieces: client and server application transport network data link physical application transport network data link physical Client: • Initiates contact with server (“speaks first”) • Typically requests service from server, • For Web, client is implemented in browser; for e-mail, in mail reader Server: • Provides requested service to client • e.g., Web server sends requested Web page, mail server delivers e-mail request replyFtp: The File Transfer Protocol ! Transfer file to/from remote host ! Client/server model ! Client: side that initiates transfer (either to/from remote) ! Server: remote host ! ftp: RFC 959 ! ftp server: port 21 file transfer FTP server FTP user interface FTP client local file system remote file system user at hostSeparate Control, Data Connections ! Ftp client contacts ftp server at port 21, specifying TCP as transport protocol ! Two parallel TCP connections opened: ! Control: exchange commands, responses between client, server. “out of band control” ! Data: file data to/from server ! Ftp server maintains “state”: current directory, earlier authentication FTP client FTP server TCP control connection port 21 TCP data connection port 20Ftp Commands, Responses Sample Commands: ! sent as ASCII text over control channel ! USER username ! PASS password ! LIST return list of files in current directory ! RETR filename retrieves (gets) file ! STOR filename stores (puts) file onto remote host Sample Return Codes ! status code and phrase ! 331 Username OK, password required ! 125 data connection already open; transfer starting ! 425 Can’t open data connection ! 452 Error writing fileTransport Service Requirements Data loss ! Some apps (e.g., audio) can tolerate some loss ! Other apps (e.g., file transfer, telnet) require 100% reliable data transfer Timing ! Some apps (e.g., Internet telephony, interactive games) require low delay to be “effective” Bandwidth • Some apps (e.g., multimedia) require minimum amount of bandwidth to be “effective” • Other apps (“elastic apps”) make use of whatever bandwidth they getTransport Service Requirements no loss no loss no loss loss-tolerant loss-tolerant loss-tolerant no loss! elastic elastic elastic audio: 5Kb-1Mb video:10Kb-5Mb same as above few Kbps elastic no no no yes, 100’s msec yes, few secs yes, 100’s msec yes and no file transfer e-mail web documents real-time audio/ video stored audio/video interactive games financial apps!Application Data loss!Bandwidth Time SensitiveServer and Client TCP/UDP IP Ethernet Adapter Server TCP/UDP IP Ethernet Adapter Clients Server and Client exchange messages over the network through a common Socket API Socket API hardware kernel space user space portsConcept of Port Numbers ! Port numbers are used to identify “entities” on a host ! Port numbers can be ! Well-known (port 0-1023) ! Assigned (port 1024-49151) ! Dynamic or private (port 49152-65535) ! Servers/daemons usually use well-known ports ! Any client can identify the server/service ! HTTP = 80, FTP = 21, Telnet = 23, … ! Other common services use assigned ports ! Clients should use dynamic ports ! Assigned by kernel at runtime TCP/UDP!IP!Ethernet Adapter!NTP!daemon!Web #server!port 123!port 80!Packet FormatPacket Format! A socket is a file descriptor that lets an application read/write data from/to the network ! socket returns an integer (socket descriptor) ! fd < 0 indicates that an error occurred ! socket descriptors are similar to file descriptors ! AF_INET: associates a socket with the Internet protocol family ! SOCK_STREAM: selects the TCP protocol ! SOCK_DGRAM: selects the UDP protocol What is a Socket? int fd; /* socket descriptor */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) } perror(“socket”); exit(1); }Names and Addresses ! Each attachment point on Internet is given unique address ! Based on location within network – like phone numbers ! Humans prefer to deal with names not addresses ! Domain Name Service (DNS) provides mapping of name to address ! Name based on administrative ownership of hostTCP!IP!Ethernet Adapter!Web Server!Port 80!! For example: web server ! What does a web server need to do so that a web client can connect to it?!TCP Server! Since web traffic uses TCP, the web server must create a socket of type SOCK_STREAM int fd; /* socket descriptor */ if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror(“socket”); exit(1); } • socket returns an integer (socket descriptor) • fd < 0 indicates that an error occurred • AF_INET associates a socket with the Internet protocol family • SOCK_STREAM selects the TCP protocol Socket I/O: socket()! A socket can be bound to a port int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ /* create the socket */ srv.sin_family = AF_INET; /* use the Internet addr family */ srv.sin_port = htons(80); /* bind socket ‘fd’ to port 80*/ /* bind: a client may connect to any of my addresses */ srv.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror("bind"); exit(1); } • Still not quite ready to communicate with a client... Socket I/O: bind()Socket I/O: listen() ! listen indicates that the server will accept a connection int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ /* 1) create the socket */ /* 2) bind the socket to a port */ if(listen(fd, 5) < 0) { perror(“listen”); exit(1); } • Still not quite ready to communicate with a client...Socket I/O: accept() ! accept blocks waiting for a connection int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ struct sockaddr_in cli; /* used by accept() */ int newfd; /* returned by accept() */ int cli_len =
View Full Document