NETWORK PROGRAMMING-Horizon Campus - Lecture5.pdf

Full Transcript

IT31072/3 NETWORK PROGRAMMING G. S. NADEERA MEEDIN SUMMARY – LECTURE ▪ IPv4 & IPv6 ▪ How are IP addresses used to locate devices on the Internet? ▪ DNS Resolution Horizon Campus - Lecture 3 2 COURSE CONTENTS 1. Introduction to network programming...

IT31072/3 NETWORK PROGRAMMING G. S. NADEERA MEEDIN SUMMARY – LECTURE ▪ IPv4 & IPv6 ▪ How are IP addresses used to locate devices on the Internet? ▪ DNS Resolution Horizon Campus - Lecture 3 2 COURSE CONTENTS 1. Introduction to network programming [ Lecture 1] 2. Basic web concepts, streams and threads [ Lecture 2,3] 3. Looking up Internet addresses, URLs and URIs [ Lecture 4,5,6] 4. Sockets for clients and servers, secure sockets [ Lecture 7-9] 5. Non-blocking I/O [ Lecture 10-12] 6. UDP datagrams and sockets, multicast sockets [ Lecture 13-15] Horizon Campus - Lecture 3 3 OUTLINE ▪ Looking up Internet addresses, URLs and URIs Horizon Campus - Lecture 3 4 LOOKING UP INTERNET ADDRESSES, URLS AND URIS CONTD. Horizon Campus - Lecture 3 5 UNIFORM RESOURCE IDENTIFIERS (URIS) ▪ Uniform Resource Identifier (URI) is a sequence of characters identifying a hypertext resource. ▪ The generic form of any URI scheme is [//[user:password@]host[:port]][/]path[?query][#fragment] ▪ A URI may consist of the following elements: ▪ Scheme ▪ Authority ▪ Query (optional) ▪ Fragment (optional) Horizon Campus - Lecture 3 6 UNIFORM RESOURCE IDENTIFIERS (URIS) Scheme ▪ Within the URI, the first element is the scheme name. ▪ Schemes are case-insensitive and separated from the rest of the object by a colon. ▪ The scheme establishes the concrete syntax and associated protocols for the URI. ▪ Ideally, URI schemes should be registered with the Internet Assigned Numbers Authority (IANA) although nonregistered schemes can also be used. Example If the URI is telnet://192.0.2.16:80, the scheme name is "telnet." Horizon Campus - Lecture 3 7 URI SCHEMES DESIGNED FOR DIFFERENT PURPOSES FTP (File Transfer Protocol): ▪ Scheme: ftp ▪ Example: ftp://ftp.example.com/pub/file.txt ▪ Description: Used to access and transfer files over a network using the FTP protocol. mailto: ▪ Scheme: mailto ▪ Example: mailto:[email protected] ▪ Description: Used to create email links that open the user's default email client with the specified recipient's address pre-filled. Horizon Campus - Lecture 3 8 URI SCHEMES DESIGNED FOR DIFFERENT PURPOSES file: ▪ Scheme: file ▪ Example: file:///C:/Users/username/Documents/document.txt ▪ Description: Used to reference files on the local file system. Note that access to local files through file URIs may be restricted in some contexts for security reasons. telnet: ▪ Scheme: telnet ▪ Example: telnet://example.com ▪ Description: Used to establish a telnet connection to a remote host. Telnet is a network protocol used to interact with remote computers. Horizon Campus - Lecture 3 9 URI SCHEMES DESIGNED FOR DIFFERENT PURPOSES ssh: ▪ Scheme: ssh ▪ Example: ssh://[email protected] ▪ Description: Used to initiate secure shell (SSH) connections to remote hosts, often for secure command-line access or file transfer. news: ▪ Scheme: news ▪ Example: news:rec.games.chess ▪ Description: Used for accessing Usenet newsgroups and reading articles within those groups. Horizon Campus - Lecture 3 10 URI SCHEMES DESIGNED FOR DIFFERENT PURPOSES irc: ▪ Scheme: irc ▪ Example: irc://irc.example.com/channel ▪ Description: Used to connect to an Internet Relay Chat (IRC) server and join a specified chat channel. data: ▪ Scheme: data ▪ Example: data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D ▪ Description: Used to include data directly in the URI, commonly used for embedding small data, such as inline images or CSS styles, in web pages. Horizon Campus - Lecture 3 11 URI SCHEMES DESIGNED FOR DIFFERENT PURPOSES spotify: Scheme: spotify Example: spotify:track:4uLU6hMCjMI75M1A2tKUQC Description: Used to open content in the Spotify application, such as specific songs, albums, or playlists. Horizon Campus - Lecture 3 12 URL ENCODING ▪ URL encoding is the process of converting special characters within a URL to a per cent-encoded format. ▪ URL encoding is required to handle reserved characters, non-ASCII characters, or characters with special meanings in URLs. ▪ URLs can only be sent over the Internet using the ASCII character set. ▪ Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. ▪ URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. ▪ URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20 Horizon Campus - Lecture 3 13 URL ENCODING ▪ Read https://www.w3schools.com/tags/ref_urlencode.ASP for ASCII Encoding Reference. Horizon Campus - Lecture 3 14 HTTP REDIRECTION ▪ URL redirection, also known as URL forwarding, is a technique to give more than one URL address to a page, a form, a whole website, or a web application. ▪ HTTP has a special kind of response, called an HTTP redirect, for this operation. ▪ Redirects accomplish numerous goals: ❖ Temporary redirects during site maintenance or downtime ❖ Permanent redirects to preserve existing links/bookmarks after changing the site's URLs, progress pages when uploading a file, etc. Horizon Campus - Lecture 3 15 HTTP REDIRECTION ▪ Redirection is triggered by a server sending a special redirect response to a request. ▪ Redirect responses have status codes that start with 3, and a Location header holding the URL to redirect to. ▪ The Location response header indicates the URL to redirect a page to. ▪ It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response. Horizon Campus - Lecture 3 16 HTTP RESPONSE STATUS CODES ▪ HTTP response status codes indicate whether a specific HTTP request has been successfully completed. ▪ Responses are grouped into five classes: ❖ Informational responses (100 – 199) ❖ Successful responses (200 – 299) ❖ Redirection messages (300 – 399) ❖ Client error responses (400 – 499) ❖ Server error responses (500 – 599) Further Read : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status Horizon Campus - Lecture 3 17 READING FILES FROM THE INTERNET (URLS) ▪ In JAVA, there is a URL class defined in the java.net package. ▪ We can create our own URL objects as follows: ▪ JAVA will "dissect" the given String in order to obtain information about protocol, hostName, file etc.... ▪ JAVA may throw a MalformedURLException Horizon Campus - Lecture 3 18 READING FILES FROM THE INTERNET (URLS) ▪ Another way to create a URL is to break it into its various components ▪ The URL class also supplies methods for extracting the parts (protocol, host, file, port and reference) of a URL object. Horizon Campus - Lecture 3 19 READING FILES FROM THE INTERNET (URLS) Modifier Method and Description and Type String getProtocol() Gets the protocol name of this URL. String getHost() Gets the host name of this URL, if applicable. int getPort() Gets the port number of this URL. String getRef() Gets the anchor (also known as the "reference") of this URL. String getFile() Gets the file name of this URL. InputStream openStream() Opens a connection to this URL and returns an InputStream for reading from that connection. Refer https://docs.oracle.com/javase/8/docs/api/java/net/URL.html Horizon Campus - Lecture 3 20 READING FILES FROM THE INTERNET (URLS) Horizon Campus - Lecture 3 21 READING FILES FROM THE INTERNET (URLS) Horizon Campus - Lecture 3 22 READING FILES FROM THE INTERNET (URLS) Horizon Campus - Lecture 3 23 PARSING A URI ▪ In Java, you can parse and manipulate URLs/URIs programmatically using the built-in `java.net.URI` and `java.net.URL` classes. Horizon Campus - Lecture 3 24 When working with `java.net.URI`, you should handle `URISyntaxException` that may be thrown during parsing or URI creation. SUMMARY ▪ URI ▪ URL Encoding ▪ HTTP Redirection Horizon Campus - Lecture 3 26 Q&A Horizon Campus - Lecture 3 27

Use Quizgecko on...
Browser
Browser