* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Document 4206551
Survey
Document related concepts
Wake-on-LAN wikipedia , lookup
Distributed firewall wikipedia , lookup
Internet protocol suite wikipedia , lookup
Piggybacking (Internet access) wikipedia , lookup
Computer network wikipedia , lookup
Recursive InterNetwork Architecture (RINA) wikipedia , lookup
Network tap wikipedia , lookup
Zero-configuration networking wikipedia , lookup
Cracking of wireless networks wikipedia , lookup
Transcript
Advanced Programming Rabie A. Ramadan Lecture 3 Network Programming An Overview 2 Computer Network A computer network is an interconnected collection of autonomous computers. Network topology A topology is a way of “laying out” the network. Topologies can be either physical or logical. Physical topologies describe how the cables are run. Logical topologies describe how the network messages travel Network topology (cont.) Bus (can be both logical and physical) Star (physical only) Ring (can be both logical and physical) Mesh (can be both logical and physical) Network topology (cont.) Bus A bus is the simplest physical topology. It consists of a single cable that runs to every workstation This topology uses the least amount of cabling, but also covers the shortest amount of distance. Network topology (cont.) It is difficult to add a workstation Have to completely reroute the cable and possibly run two additional lengths of it. If any one of the cables breaks, the entire network is disrupted. Therefore, it is very expensive to maintain. Network topology (cont.) Star Topology A physical star topology branches each network device off a central device called a hub, making it very easy to add a new workstation. Also, if any workstation goes down it does not affect the entire network. Network topology (cont.) Star topologies are more expensive to install than bus networks, • There are several more cables that need to be installed, plus the cost of the hubs that are needed. Network topology (cont.) Ring Each computer connects to two other computers, joining them in a circle creating a unidirectional path where messages move workstation to workstation. Network topology (cont.) The ring makes it difficult to add new computers. Unlike a star topology network, the ring topology network will go down if one entity is removed from the ring. Physical ring topology systems don’t exist much anymore, mainly because the hardware involved was fairly expensive and the fault tolerance was very low. Network topology (cont.) Mesh The mesh topology is the simplest logical topology in terms of data flow, but it is the most complex in terms of physical design. In this physical topology, each device is connected to every other device Network topology (cont.) The physical mesh topology is very expensive to install and maintain. Cables must be run from each device to every other device. The advantage you gain from it is its high fault tolerance. There will always be a way of getting the data from source to destination. Network topology (cont.) Advantages and Disadvantages of Network Topologies Topology Advantages Disadvantages Bus Cheap. Easy to install. Difficult to reconfigure. Break in bus disables entire network. Star Cheap. Easy to install. Easy to reconfigure. Fault tolerant. More expensive than bus. Ring Efficient. Easy to install. Reconfiguration difficult. Very expensive. Mesh Simplest. Most fault tolerant. Reconfiguration extremely difficult. Extremely expensive. Very complex. Computer Network A network includes: • Special purpose hardware devices that: • Interconnect transmission media • Control transmission of data • Run protocol software • Protocol software that: • Encodes and formats data • Detects and corrects problems encountered during transmission Network Hardware Repeaters Repeaters are very simple devices. Allow a cabling system to extend beyond its maximum allowed length by amplifying the network voltages so they travel farther. Repeaters are nothing more than amplifiers and, as such, are very inexpensive. Network Hardware Hubs Hubs are devices used to link several computers together. They repeat any signal that comes on one port and copy it to the other ports (a process that is also called broadcasting). There are two types of hubs: active and passive. • • Passive hubs simply connect all ports together electrically and are usually not powered. Active hubs use electronics to amplify and clean up the signal before it is broadcast to the other ports. In the category of active hubs, there is also a class called “intelligent” hubs, which are hubs that can be remotely managed on the network. Network Hardware Hubs Network Hardware Bridges They join similar topologies and are used to divide network segments. For example, with 200 people on one Ethernet segment, the performance will be bad, because of the design of Ethernet and the number of workstations that are fighting to transmit. If you divide the segment into two segments of 100 workstations each, the traffic will be much lower on either side and performance will increase. They are more intelligent than repeaters but are unable to move data across multiple networks simultaneously. Unlike repeaters, bridges can filter out noise. The main disadvantage to bridges is that they can’t connect dissimilar network types or perform intelligent path selection. For that function, you would need a router. Network Hardware Bridges Network Hardware Routers Routers are highly intelligent devices that connect multiple network types and determine the best path for sending data. The advantage of using a router over a bridge is that routers can determine the best path that data can take to get to its destination. Like bridges, they can segment large networks and can filter out noise. However, they are slower than bridges because they are more intelligent devices; Network Hardware Switch Just like a hub A network switch conserves network bandwidth and offers generally better performance than a hub. A vital difference between a hub and a switch is that all the nodes connected to a hub share the bandwidth among themselves, while a device connected to a switch port has the full bandwidth all to itself. For example, if 10 nodes are communicating using a hub on a 10-Mbps network, then each node may only get a portion of the 10 Mbps if other nodes on the hub want to communicate as well. . But with a switch, each node could possibly communicate at the full 10 Mbps. Network Hardware Switch Addressing and Routing Address: byte-string that identifies a node Routing: process of forwarding messages to the destination node based on its address Types of addresses • usually unique • unicast: node-specific • broadcast: all nodes on the network • multicast: some subset of nodes on the network IP Addresses and Classes IPv6 addresses have a size of 128 bits IP Addresses and Classes IP Addresses and Classes IP Addresses and Classes 28 Subnet Mask 29 Network Architecture A network architecture is a set of layers and protocols used to reduce network design complexity. The TCP/IP Protocol Suite (also called the Internet Architecture) is an important example of a network architecture. The OSI (Open Systems Interconnection) 7-Layer Reference Model [ISO,1984] is a guide that specifies what each layer should do, but not how each layer is implemented. ISO/OSI Reference Model ISO 7-Layer Reference Model End host End host Application Application Presentation Presentation Session Session Transport Transport Network Network Network Network Data link Data link Data link Data link Physical Physical Physical Physical One or more nodes within the network TCP/IP Model 33 Internet Model 34 Protocols A protocol is a set of rules of communication. Protocols are the building blocks of a network architecture. Term “protocol” is overloaded • specification of peer-to-peer interface • module that implements this interface Network Programming A network allows arbitrary applications to communicate. However, a network programmer doesn’t need to know the details of all lower-level network technologies. Network facilities are accessed through an Application Programming Interface (API); e.g., a Service Interface. Basic Paradigm for Communication Most network applications can be divided into two pieces: a client and a server. A Web browser (a client) communicate with a Web server. A Telnet client that we use to log in to a remote host. A user who needs access to data located at remote server. Basic Paradigm for Communication Establish contact (connection). Exchange information (bi-directional). Terminate contact. Client-Server Paradigm Server waits for client to request a connection. Client contacts server to establish a connection. Client sends request. Server sends reply. Client and/or server terminate connection. Two types of Communication Connection-oriented • Setup the link before communication. • Similar to the phone call. We need the phone number and receiver. Connectionless • No link needed to be set up before communication. • Similar to send a letter. We need the address and receiver. TCP and UDP TCP (Transmission Control Protocol) is a connection-oriented protocol. UDP (User Datagram Protocol) is connectionless (UDP) protocol. Ports Identifying the ultimate destination IP addresses identify hosts Host has many applications Ports (16-bit identifier) Application Port WWW 80 E-mail Telnet 25 23 192.18.22.13 Sockets A socket is defined as an endpoint for communication. Concatenation of IP address and port A socket pair (local IP address, local port, foreign IP address, foreign port) uniquely identifies a communication. The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Sockets 44 Sockets and Ports socket any port agreed port socket message client server other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 TCP Socket Bind() binds the socket to the specified address. The address parameter specifies the local component of the address, e.g. IP address and UDP/TCP port UDP Socket Types of Sockets A stream socket uses TCP for connection-oriented communication. It is also called a TCP socket. A datagram socket uses UDP for connectionless communication. It is also called a UDP socket. A raw socket uses IP directly. It is used in such applications as in ICMP protocol ICMP: Internet Control Message Protocol ICMP: Internet Control Message Protocol • ICMP handles error and control information between gateways and hosts. • The messages are transmitted using IP datagrams. • They are normally generated and processed by the TCP/IP software itself, not user processes. UNIX TCP Communication Normally, a server would first listen and accept a connection and then fork a new process to communicate with the client. It uses the listen operation to listen for connections on a socket. int listen (int sockfd, int backlog) : - Sockfd a socket descriptor created by the socket call. - The backlog parameter defines the maximum length the queue of pending connections may grow to. UNIX TCP Communication The server uses the accept system call to accept connection requested by a client. After a connection has been established, both processes may then use the write (send) and read (recv) operations to send and receive messages. Establishing a TCP Connection Passive Open Server process does a ‘passive’ open on a port It waits for a client to connect At this stage there is no Internet network traffic Establishing a TCP Connection Active Open Client process usually on a different machine Performs an ‘active’ open on the port Windows Sockets Similar to UNIX, only the interface is different We call it Windows Socket or WinSock Fits above the transport layer It is not a Protocol -- Located in a dynamic linked library (DLL). 54 Steps in creating Clients in Java What is the difference between String and BufferedReader? 55 Steps in creating Clients in Java 56 57 58 59 60 61 62 63 64 65 66 67 References http://www.sockets.com/winsock.htm http://tangentsoft.net/wskfaq/ Core Web Programming book Chapter 17. Network Programming Dynamic Programming 5/7/2017 69 Dynamic Programming JavaScript …. Server-Side Java: Servlets XML Remote Method Invocation (RMI) Java Server Pages (JSP) Others… Reference Marty Hall, Larry Brown, Core Web Programming, Second Edition. 5/7/2017 70 What are Servelts? Servlets are programs that run on a web server, Act as a middle layer between a request coming from a web browser or other http client and databases or applications on the http server. 5/7/2017 71 What is their Job? Read any data sent by the user. • • • Look up any other information about the request that is embedded in the HTTP request. • A form on a Web page An applet or A custom HTTP client program. This information includes details about browser capabilities, cookies, the host name of the requesting client, and so forth. Generate the results. • This process may require talking to a database, invoking a legacy application, or computing the response directly. 5/7/2017 72 What is their Job? Format the results inside a document. • In most cases, this involves embedding the information inside an HTML document. Set the appropriate HTTP response parameters. • This means telling the browser what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks. Send the document back to the client. • This document can be sent in text format (HTML), binary format (GIF images), or even in a compressed format like gzip that is layered on top of some other underlying format. 5/7/2017 73 Common Gateway Interface (CGI) Is Simply a program interprets the Client request to the server and forms the response back to the client . A new process is created for each request. 5/7/2017 74 Servlet vs. CGI Request Request CGI1 CGI1 Request RequestCGI2 CGI2 Request Request CGI1 CGI1 Request Request Servlet1 Servlet1 Request Request Servlet2 Servlet2 Request Servlet1 Child Childfor forCGI1 CGI1 CGI CGI Based Based Webserver Webserver Child Childfor forCGI2 CGI2 Child Childfor forCGI1 CGI1 Servlet Servlet Based BasedWebserver Webserver JVM JVM Servlet1 Servlet1 Servlet2 Servlet2 Advantages of Servlets Over "Traditional" CGI Efficient • Uses threads instead of O.S. processes for each request Convenient • Ready made components to automatically parse and decode HTML form data, read and set HTTP headers, handle cookies, track sessions, and many other such high-level utilities Powerful • Can talk directly to HTTP server , • Multiple servlets can also share data, making it easy to implement database connection pooling and similar resource-sharing optimizations 5/7/2017 76 Advantages of Servlets Over "Traditional" CGI Portable • By the end of the day it is java Secure • Filter out characters such as backquotes and semicolons that are treated specially by the shell • No Buffer overflow Inexpensive • Free of charge 5/7/2017 77 Basic Servlet Structure 5/7/2017 78 Basic Servlet Structure Analysis To be a servlet, a class should extend HttpServlet and override doGet or doPost, depending on whether the data is being sent by GET or by POST. HttpServletRequest has methods by which you can find out about incoming information such as form (query) data, HTTP request headers, and the client's hostname. The HttpServletResponse lets you specify outgoing information such as HTTP status codes (200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.). It lets you obtain a PrintWriter with which you send the document content back to the client. 5/7/2017 79 Handler Functions Each HTTP Request type has a separate handler function. • • • • • GET -> doGet(HttpServletRequest, HttpServletResponse) POST -> doPost(HttpServletRequest, HttpServletResponse) DELETE -> doDelete (HttpServletRequest, HttpServletResponse) TRACE -> doTrace (HttpServletRequest, HttpServletResponse) OPTIONS -> doOptions (HttpServletRequest, HttpServletResponse) 80 Generating HTML 5/7/2017 81 Generating Simple HTML Page 5/7/2017 82 Analysis response.setContentType("text/html"); You can use it to generate other document types : GIF images (content type image/gif) Excel spreadsheets (content type application/vnd.ms-excel). 5/7/2017 83 Servlet Life Cycle 84 A Servlet Template import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletTemplate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Use "request" to read incoming HTTP headers // (e.g. cookies) and HTML form data (e.g. data the user // entered and submitted). // Use "response" to specify the HTTP response status // code and headers (e.g. the content type, cookies). PrintWriter out = response.getWriter(); // Use "out" to send content to browser } } 85 Hello World Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>\n" + "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" + "<BODY>\n" + "<H1>Hello WWW</H1>\n" + "</BODY></HTML>"); } } 86 Your Assignment Read chapter 19 from the “Core Web Programming” book and implement the last example of the chapter (19.12 Session tracking) E-mail: [email protected] Subject “3S-Assignment 2” http://rabieramadan.org/classes/Advprog/ Hello.rar 5/7/2017 87 Web Programming (continue) HTML Basics 89 Basic HTML Document Format <HTML> <HEAD> <TITLE>WENT'99</TITLE> </HEAD> <BODY> Went'99 </BODY> </HTML> See what it looks like: 90 HTML 5 Parsing Reverse engineering current browsers Compatible with existing browsers and web pages Thoroughly defines error handling 91 New Structures <div<header> id="header"> <div class="article"> <article> <div <nav> id="nav"> <div <section> id="content"> <div<footer> id="footer"> <div <aside> id="right"> New Semantics <canvas> <datagrid> <meter> <progress> And many more… <m> <command> <details> <dialog> <figure> <time> <menu> <embed> Rating: New Controls <input type="datetime"> <input type="text" list="list"> <datalist id="list"> <option value="Mr"> ...<input <input type="number"> type="range"> <input type="url"> <input type="email"> </datalist> And many more… Repetition Model <tr id="member" repeat-template="member" repeat="template" repeat="0"> repeat-start="2"> Team Members <td><input type="text" name="member[member].rank"></td> name="member0.rank"></td> <td><input name="member0.name"></td> Ranktype="text" name="member[member].name"></td> Name <td><button type="remove">Remove</button></td> Colonel Jack O'Neill </tr> <tr repeat-template="member" repeat="1"> <td><input type="text" name="member1.rank"></td> Major Sam Carter <td><input type="text" name="member1.name"></td> <td><button type="remove">Remove</button></td> Civilian Daniel Jackson </tr> <tr id="member" repeat="template" repeat-start="2"> <td><input type="text" Alien Add Team Membername="member[member].rank"></td> Teal’c <td><input type="text" name="member[member].name"></td> <td><button type="remove">Remove</button></td> </tr> Remove Remove Remove Continue Remove <button type="add" template="member">Add Team Member</button> Client-Side Form Validation Regular Expressions Minimum and Maximum Values MaxLength for textarea Required Fields And many more… <input type="text" pattern="[A-Za-z0-9_\-]+"> <input type="email" required="required"> <input type="range" min="20" max="80"> <textarea maxlength="2000"></textarea> XML Technologies and Applications 97 Sample XML Data <orders> <order> <onum>1020</onum> <takenBy>1000</takenBy> <customer>1111</customer> <recDate>10-DEC 94</recDate> <items> <item> <pnum>10506</pnum> <quantity>1</quantity> </item> <item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items> </order> ... </orders> 98 Sample Data startDocument <orders> <order> <onum>1020</onum> <takenBy>1000</takenBy> <customer>1111</customer> <recDate>10-DEC 94</recDate> <items> <item> <pnum>10506</pnum> <quantity>1</quantity> </item> Parsing Event <item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items> </order> ... endDocument </orders> Sample Data <orders> <order> <onum>1020</onum> <takenBy>1000</takenBy> <customer>1111</customer> <recDate>10-DEC-94</recDate> <items> <item> <pnum>10506</pnum> <quantity>1</quantity> </item> startElement <item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> endElement <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items> </order> ... </orders> Sample Data <orders> <order> <onum>1020</onum> <takenBy>1000</takenBy> <customer>1111</customer> <recDate>10-DEC-94</recDate> <items> characters <item> <pnum>10506</pnum> <quantity>1</quantity> </item> <item> <pnum>10507</pnum> <quantity>1</quantity> </item> <item> <pnum>10508</pnum> <quantity>2</quantity> </item> <item> <pnum>10509</pnum> <quantity>3</quantity> </item> </items> </order> ... </orders> XML-Parsing Standards Two parsing methods that implement W3C standards for accessing XML SAX (Simple API for XML) • event-driven parsing • “serial access” protocol • Read only API DOM (Document Object Model) • convert XML into a tree of objects • “random access” protocol • Can update XML document (insert/delete nodes) 102 SAX Parsers <?xml version="1.0"?> . . . SAX Parser When you see the start of the document do … When you see the start of an element do … When you see the end of an element do … The DOM Tree 104 Using a DOM Tree XML File DOM Parser DOM Tree A P I Application 105 Node Navigation getPreviousSibling() getFirstChild() getChildNodes() getParentNode() getLastChild() getNextSibling() 106 SAX vs DOM Parsing: Efficiency The DOM object built by DOM parsers is usually complicated and requires more memory storage than the XML file itself A lot of time is spent on construction before use For some very large documents, this may be impractical SAX parsers store only local information that is encountered during the serial traversal Hence, programming with SAX parsers is, in general, more efficient 107