Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2 outline 2.1 Principles of app layer protocols clients and servers app requirements 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching Content distribution networks P2P file sharing Lecture7 1 Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte streamoriented socket a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process Lecture7 2 Socket-programming using TCP Socket: a door between application process and endend-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process process socket TCP with buffers, variables host or server internet socket TCP with buffers, variables controlled by application developer controlled by operating system host or server Lecture7 3 Socket programming with TCP Client must contact server server process must first be running server must have created socket (door) that welcomes client’s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process 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 client allows server to talk with multiple clients source port numbers used to distinguish clients (more in Chap 3) application viewpoint TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server Lecture7 4 Stream jargon A stream is a sequence of characters that flow into or out of a process. An input stream is attached to some input source for the process, eg, keyboard or socket. An output stream is attached to an output source, eg, monitor or socket. Lecture7 5 Socket programming with TCP Client Process process input stream output stream inFromServer 1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream) 2) server reads line from socket 3) server converts line to uppercase, sends back to client 4) client reads, prints modified line from socket (inFromServer stream) outToServer Example client-server app: monitor inFromUser keyboard input stream client TCP clientSocket socket to network TCP socket from network Lecture7 6 Client/server socket interaction: TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = ServerSocket() TCP wait for incoming connection request connection connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket close connectionSocket setup create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket read reply from clientSocket close clientSocket Lecture7 7 Example: Java client (TCP) import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence; Create input stream Create client socket, connect to server Create output stream attached to socket BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Lecture7 8 Example: Java client (TCP), cont. Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); Send line to server outToServer.writeBytes(sentence + '\n'); Read line from server modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } } Lecture7 9 Example: Java server (TCP) import java.io.*; import java.net.*; class TCPServer { Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); Lecture7 10 Example: Java server (TCP), cont Create output stream, attached to socket DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); Read in line from socket clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; Write out line to socket outToClient.writeBytes(capitalizedSentence); } } } End of while loop, loop back and wait for another client connection Lecture7 11 Chapter 2 outline 2.1 Principles of app layer protocols clients and servers app requirements 2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail SMTP, POP3, IMAP 2.5 DNS 2.6 Socket programming with TCP 2.7 Socket programming with UDP 2.8 Building a Web server 2.9 Content distribution Network Web caching Content distribution networks P2P file sharing Lecture7 12 Socket programming with UDP UDP: no “connection” between client and server no handshaking sender explicitly attaches IP address and port of destination to each packet server must extract IP address, port of sender from received packet application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server UDP: transmitted data may be received out of order, or lost Lecture7 13 Client/server socket interaction: UDP Server (running on hostid) create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number Client create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read reply from clientSocket close clientSocket Lecture7 14 Example: Java client (UDP) input stream Client process monitor inFromUser keyboard Process Input: receives packet (TCP received “byte stream”) UDP packet receivePacket packet (TCP sent “byte stream”) sendPacket Output: sends client UDP clientSocket socket to network UDP packet UDP socket from network Lecture7 15 Example: Java client (UDP) import java.io.*; import java.net.*; Create input stream Create client socket Translate hostname to IP address using DNS class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine(); sendData = sentence.getBytes(); Lecture7 16 Example: Java client (UDP), cont. Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); Send datagram to server clientSocket.send(sendPacket); Read datagram from server clientSocket.receive(receivePacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } } Lecture7 17 Example: Java server (UDP) import java.io.*; import java.net.*; Create datagram socket at port 9876 class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { Create space for received datagram Receive datagram DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); Lecture7 18 Example: Java server (UDP), cont String sentence = new String(receivePacket.getData()); Get IP addr port #, of sender InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); Create datagram to send to client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Write out datagram to socket serverSocket.send(sendPacket); } } } End of while loop, loop back and wait for another datagram Lecture7 19 Building a simple Web server handles one HTTP request accepts the request parses header obtains requested file from server’s file system creates HTTP response message: after creating server, you can request file using a browser (eg IE explorer) see text for details If you are familiar with Java, practice this by yourself header lines + file sends response to client Lecture7 20 Socket programming: references Java-tutorials: “All About Sockets” (Sun tutorial), http://java.sun.com/docs/books/tutorial/networking/so ckets/ “Socket Programming in Java: a tutorial,” http://www.javaworld.com/javaworld/jw-12-1996/jw12-sockets.html Lecture7 21 Socket programming API Unix systems System calls Proj1: simple HTTP client/server Using UNIX BSD sockets C-language tutorial: “Unix Network Programming” (J. Kurose), http://manic.cs.umass.edu/~amldemo/courseware/intro Beej's Guide to Network Programming http://www.ecst.csuchico.edu/~beej/guide/net/ See useful links fore more infor on class web page Lecture7 22 Unix Programming: Mechanism UNIX system calls and library routines (functions called from C/C++ programs) man <function name> A word on style: check all return codes if ((code = syscall()) < 0) { perror("syscall"); } Lecture7 23 Creating Sockets #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); - creates an endpoint for communication - return value: -1 if an error occurs; otherwise the return value is a descriptor referencing the socket Lecture7 24 Creating Sockets: Parameters domain : address family (protocol family) determine address structure e.g. AF_UNIX, AF_INET, AF_INET6 we will use AF_INET only type : service of a socket e.g. SOCK_DGRAM provides unreliable, connectionless service e.g. SOCK_STREAM provides connectionoriented reliable byte-stream service Lecture7 25 Creating Sockets: Parameters (cont.) protocol : specifies particular protocol Usually already defined by domain and type (e.g. TCP for AF_INET and SOCK_STREAM; UDP for AF_INET and SOCK_DGRAM) we will use 0 (default protocol) Example if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) { perror(“socket”); exit(1); } Lecture7 26 Binding the Address for a Socket #include <sys/types.h> #include <sys/socket.h> int bind(int sd, struct sockaddr *my_addr, socklen_t addrlen); - assigns the local address of a socket - return value: -1 if an error occurs; otherwise 0 Lecture7 27 Socket Address Several types of addresses We will use sockaddr_in (<netinet/in.h>) struct sockaddr_in { sa_family_t sin_family; /*AF_INET*/ uint16_t sin_port; /* network order*/ struct in_addr sin_addr; }; struct in_addr { uint32_t s_addr; /* network order*/ }; Two types of byte ordering: little endian, and big endian (network order) Lecture7 28 Internet Addresses and Ports sin_port 16 bits 0-1024 reserved for system well-known ports are important If you specify 0, the OS picks a port s_addr 32 bits INADDR_ANY for any local interface address Lecture7 29 Internet Addresses and Ports: Example struct sockaddr_in myaddr; bzero( (char*)myaddr, sizeof(myaddr) );/*initialize*/ myaddr.sin_family = AF_INET; myaddr.sin_port = htons(80); /* bind to HTTP port*/ myaddr.sin_addr.s_addr = htos(INADDR_ANY); /* any address*/ if ( (bind(sockfd, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0 ) { perror(“bind”); exit(1); } Lecture7 30 Set a Socket in the Listening State (Server) #include <sys/types.h> #include <sys/socket.h> int listen(int sd, int backlog); - Specify the willingness to accept new connection - backlog : specify the number of pending connections - return value: -1 if an error occurs; otherwise 0 Lecture7 31 Initialize Connection Setup (Client) #include <sys/types.h> #include <sys/socket.h> int connect(int sd, const struct sockaddr *serv_addr, socklen_t addrlen); - For SOCK_STREAM, initialize connection to the server; for SOCK_DGRAM, just set the destination address and set the socket in connected state - return value: -1 if an error occurs; otherwise 0 Lecture7 32 Accept a Connection (Server) #include <sys/types.h> #include <sys/socket.h> int accept(int sd, struct sockaddr *peer_addr, socklen_t addrlen); - remove the first connection from the pending connection queue, create a new socket in connected state, the original sd is not changed and still in listening state - return value: -1 if an error occurs; otherwise the descriptor of the newly connected socket Lecture7 33 Read/Write to a Socket read()/write() of the file interface for connected-oriented Socket specific system call send()/sendto()/sendmsg() recv()/recvfrom()/recvmsg() Lecture7 34 Read from a socket by using read() #include <unistd.h> ssize_t read(int sockfd, void *buf, size_t count); - read up to count from the socket - return value: -1 if an error occurs; 0 if end of file; otherwise number of bytes read Lecture7 35 Write to a socket by using write() #include <unistd.h> ssize_t write(int sockfd, const void *buf, size_t count); - write up to count to the socket - return value: -1 if an error occurs; otherwise number of bytes write Lecture7 36 Send to a Socket #include <sys/types.h> #include <sys/socket.h> int send(int sd, const void *msg, size_t len, int flags); int sendto(int sd, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen); int sendmsg(int sd, const struct msghdr *msg, int flags) - return value: -1 if an error occurs; otherwise the number of bytes sent Lecture7 37 Receive from a Socket #include <sys/types.h> #include <sys/socket.h> int recv(int sd, void *buf, size_t len, int flags); int recvfrom(int sd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t fromlen); int recvmsg(int sd, struct msghdr *msg, int flags); - return value: -1 if an error occurs; otherwise the number of bytes received Lecture7 38 Close a Socket #include <unistd.h> int close(int sd); - return value: -1 if an error occurs; otherwise 0 #include <sys/socket.h> int shutdown(int sd, int how); - how : if 0, no further receives; if 1, no further sends; if 2, no further sends or receives - return value: -1 if an error occurs; otherwise 0 Lecture7 39 Support Routines: Network/Host Order #include <netinet/in.h> unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigned long int networklong); unsigned short int ntohs(unsigned short int networkshort); Lecture7 40 DNS Service #include <netdb.h> extern int h_errno; struct hostent *gethostbyname(const char *name); Struct hostent { char *h_name; // official name char **h_aliases; // a list of aliases int h_addrtype; int h_length; char **h_addr_list; } #define h_addr h_addr_list[0] - return value: NULL if fails Lecture7 41