Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 22 Internet Networking. CHAPTER GOALS To understand the concept of sockets To learn how to send and receive data through sockets To implement.

Similar presentations

Presentation on theme: "Chapter 22 Internet Networking. CHAPTER GOALS To understand the concept of sockets To learn how to send and receive data through sockets To implement."— Presentation transcript:

1 Chapter 22 Internet Networking

2 CHAPTER GOALS To understand the concept of sockets To learn how to send and receive data through sockets To implement network clients and servers To communicate with web servers and server-side applications through Hypertext Transfer Protocol (HTTP)

3 Internet A worldwide collection of networks, routing equipment, and computers Uses a common set of protocols to define how the parties will interact with each other Many different services are offered over the Internet oThe World Wide Web oEmail

4 Physical Media Network cabling oElectrical impulses represent the information flow Telephone wires using a modem oData travels encoded as tones Air in a wireless network oSignals are sent by modulating radio frequencies

5 Data Transmission Data transmission consists of sending and receiving streams of zeros and ones along the network connection

6 Two Types of Information Application data o consists of the information one computer wants to send to another Network protocol data o describes how to reach the intended computer o describes how to check for errors in the transmission

7 Network Protocol Local area network protocols o Microsoft Networking o Novell NetWare o AppleTalk Protocol for connecting local area networks o Internet Protocol (IP) previousprevious | start | nextstartnext File previousprevious | start | nextstartnext

8 Sending Data from A to B across the Internet A is you home computer It is connected by phone lines to an Internet Service Provider (ISP) The ISP is connected to an Internet Access Point B is on an local area network at XYZ Computers. XYZ has its own Internet Access Point The Internet Access Points are connected by a complex collection of pathways (the Internet) Over these pathways a message sent from one access point can eventually reach any access point

9 Two Computers Communicating across the Internet

10 Destination Address Data must be marked with a destination address In IP, addresses are denoted by a sequence of four numbers o Each is one byte (a number between 0 and 255) o For example To send data to B, A needs to know B's Internet address o A includes that address in the protocol portion when sending the data

11 Domain Naming Service In addition to an IP address, computers can have an easy-to-remenber domain name oFor example, A service called a Domain Naming Service (DNS) translates from domain name to Internet address When A wants to request data from a domain name: oIt asks the DNS for the numeric Internet Address oIt includes the numeric address with the request for data

12 Packets IP breaks large chunks of data up into more manageable packets Each packet is delivered separately Each packet in a larger transmission may be sent by a different route. Packets are numbered The recipient reassembles the data

13 Transmission Control Protocol Internet Protocol (IP) does not notify the sender if data is lost or garbled This is the job of a higher level protocol Transmission Control Protocol (TCP) The most commonly used Internet services use TCP with IP (TCP/IP)

14 TCP's Job Attempt to deliver the data Try again if there are failures Notify the sender whether or not the attempt was successful

15 Port Numbers One computer can offer multiple services over the Internet o For example, both a web server program and an email server program When data are sent to that computer, they need to indicate which program is to receive the data IP uses port numbers for this previousprevious | start | nextstartnext previousprevious | start | nextstartnext

16 Port Numbers A port number is an integer between 0 and 65,535 The sending program must know the port number of the receiving program This number is included in the transmitted data

17 Contents of TCP Packet The Internet address of the recipient The port number of the recipient Internet address of the sender The port number of the sender

18 The OSI Reference Model

19 Application Level Protocol TCP/IP mechanism establishes an Internet connection between two ports on two computers. Each Internet application has its own application protocol This application protocol describes how data for that application are transmitted

20 Hypertext Transfer Protocol The application protocol used by the World Wide Web is Hypertext Transfer Protocol (HTTP) A web address is called a Uniform Resource Locator (URL) You type a URL into the address window of your browser o For example,

21 Browser Steps You type into the browser's address The browser examines the part of the URL between the double slash and the first single slash o In this case: o This identifies the computer to which you want to connect o Because it contains letters, this part of the URL is a domain name The browser sends a quest to a DNS server to obtain the Internet address of the computer with the domain name

22 Browser Steps From the http: prefix to the URL, the browser decides that the protocol you want to use is HTTP o HTTP uses port 80 by default The browser establishes a TCP/IP connection to port 80 at the Internet address determined above The browser deduces from the /index.html that you want to see the file /index.html It sends this request formatted as an HTTP command through the established connection GET /index.html HTTP/1.0 a blank line

23 Browser Steps The web server running on the computer whose Internet Address was obtained above receives the request It decodes the request It fetches the file /index.html It sends the file back to the browser

24 Browser Steps The browser displays the contents of the file for you to see Since this file is an HTML file, the browser decodes the HTML codes into fonts, paragraphs etc. If the file contains images, the browser makes more GET requests through the same connection

25 Telnet Telnet program allows you to o Type characters to send to a remote computer and o View the characters that the remote computer sends back It is a useful tool to establish test connections with servers You can imitate the browser connection by using a dialog box or typing at the command line telnet 80

26 Telnet After Telnet starts, type the following without using backspace GET /index.html HTTP/1.0 then hit enter twice The server responds to the request with the file Telnet is not a browser It does not understand HTML tags so it just displays everything it was sent

27 Web Server Response in Telnet

28 HTTP Commands

29 Client Program - Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection Syntax to create a socket in a Java program Socket s = new Socket(hostname, portnumber); Code to connect to the HTTP port of server, final int HTTP_PORT = 80; Socket s = new Socket("", HTTP_PORT); If it can't find the host, the Socket constructor throws an UnknownHostException

30 Client Program - Input and Output Streams Use the input and output streams attached to the socket s to communicate with the other endpoint of the connection Code to obtain the input and output streams InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); When you send data to the output stream out, the socket forwards them to the server The socket catches the server's response and you can read it through the input stream in When you are done communicating with the server, close the socket s.close();

31 Client and Server Sockets

32 Client Program - Readers and Writers InputStream and OutputStream send and receive bytes To send and receive text, get a reader and a writer BufferedReader reader = new BufferedReader(new InputStreamReader(in)); PrintWriter writer = new PrintWriter(out); A PrintWriter buffers the characters and only sends when the buffer is full When sending a command, you want the whole command to be sent now so flush the buffer manually writer.print(command); writer.flush();

33 Client Program - WebGet This program lets you retrieve any item from a web server You specify the host and item from the command line For example: java WebGet index.html

34 File 01: import; 02: import; 03: import; 04: import; 05: import; 06: import; 07: import; 08: 09: /** 10: This program demonstrates how to use a socket to communicate 11: with a web server. Supply the name of the host and the 12: resource on the command-line, for example 13: java WebGet index.html 14: */ 15: public class WebGet 16: { 17: public static void main(String[] args) throws IOException

35 18: { 19: // get command-line arguments 20: 21: if (args.length != 2) 22: { 23: System.out.println("usage: java WebGet host resource"); 24: System.exit(0); 25: } 26: String host = args[0]; 27: String resource = args[1]; 28: 29: // open socket 30: 31: final int HTTP_PORT = 80; 32: Socket s = new Socket(host, HTTP_PORT); 33: 34: // get streams 35: 36: InputStream in = s.getInputStream(); 37: OutputStream out = s.getOutputStream();

36 38: 39: // turn streams into readers and writers 40: 41: BufferedReader reader = new BufferedReader( 42: new InputStreamReader(in)); 43: PrintWriter writer = new PrintWriter(out); 44: 45: // send command 46: 47: String command = "GET /" + resource + " HTTP/1.0\n\n"; 48: writer.print(command); 49: writer.flush(); 50: 51: // read server response 52: 53: boolean done = false; 54: while (!done) 55: { 56: String input = reader.readLine(); 57: if (input == null) done = true;

37 58: else System.out.println(input); 59: } 60: 61: // always close the socket at the end 62: 63: s.close(); 64: } 65: }

38 A Server Program This is a server program that enable clients to manage a set of bank accounts in a bank When you develop a server application, you need some application-level protocol The client can use this protocol to interact with the server A simple bank access protocol is described on the next slide

39 Simple Bank Access Protocol

40 A Server Program The server waits for the client to connect on a certain port. We choose 8888 To listen for incoming connections, use a server socket To construct a server socket, provide the port number ServerSocket server = new ServerSocket(8888); Use the accept method to wait for client connection and obtain a socket Socket s = server.accept();

41 Server Program - Readers and Writers Get the input and output streams connected to the socket s Create a reader and writer BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter( s.getOutputStream()); The server reads a command from the client String line = in.readLine();

42 Server Program The socket s is closed if: o The string received by the client equals QUIT command o Or it is null o A null string means the client has disconnected

43 Server Program Use a StringTokenizer to analyze the client command string The first token is the command The second token is the account number

44 Server Program If the command is DEPOSIT, deposit to the given account If the command is WITHDRAW, withdraw from the given account Then send the account number and new balance to the client out.println(n + " " + bank.getBalance(account));

45 Server Program The server program never stops When you are done running the sever, kill it.

46 To Try Out Server Program Run the server Use Telnet to connect to localhost, port number 8888 Start typing commands

47 Using the Telnet Program to Connect to the BankServer

48 File import; import; import; /** A server that executes the Simple Bank Access Protocol. */ public class BankServer { public static void main(String[] args ) throws IOException { final int ACCOUNTS_LENGTH = 10; Bank bank = new Bank(ACCOUNTS_LENGTH); final int SBAP_PORT = 8888; ServerSocket server = new ServerSocket(SBAP_PORT); System.out.println("Waiting for clients to connect..."); import; import; import; /** A server that executes the Simple Bank Access Protocol. */ public class BankServer { public static void main(String[] args ) throws IOException { final int ACCOUNTS_LENGTH = 10; Bank bank = new Bank(ACCOUNTS_LENGTH); final int SBAP_PORT = 8888; ServerSocket server = new ServerSocket(SBAP_PORT); System.out.println("Waiting for clients to connect..."); 01: import; 02: import; 03: import; 04: 05: /** 06: A server that executes the Simple Bank Access Protocol. 07: */ 08: public class BankServer 09: { 10: public static void main(String[] args ) throws IOException 11: { 12: final int ACCOUNTS_LENGTH = 10; 13: Bank bank = new Bank(ACCOUNTS_LENGTH); 14: final int SBAP_PORT = 8888; 15: ServerSocket server = new ServerSocket(SBAP_PORT); 16: System.out.println("Waiting for clients to connect..."); 17:

49 18: while (true) 19: { 20: Socket s = server.accept(); 21: 22: BankService service = new BankService(s, bank); 23: service.doService(); 24: s.close(); 25: } 26: } 27: } 28: 29: 30: 31: 32: 33: 34: 35:

50 File 01: import; 02: import; 03: import; 04: import; 05: import; 06: import; 07: import; 08: import java.util.StringTokenizer; 09: 10: /** 11: Executes Simple Bank Access Protocol commands 12: from a socket. 13: */ 14: public class BankService 15: {

51 16: /** 17: Constructs a service object that processes commands 18: from a socket for a bank. 19: @param aSocket the socket 20: @param aBank the bank 21: */ 22: public BankService(Socket aSocket, Bank aBank) 23: { 24: s = aSocket; 25: bank = aBank; 26: } 27: 28: /** 29: Executes all commands until the QUIT command or the 30: end of input. 31: */ 32: public void doService() throws IOException 33: { 34: BufferedReader in = new BufferedReader( 35: new InputStreamReader(s.getInputStream()));

52 36: PrintWriter out = new PrintWriter( 37: s.getOutputStream()); 38: 39: while (true) 40: { 41: String line = in.readLine(); 42: System.out.println("Received: " + line); 43: if (line == null || line.equals("QUIT")) 44: return; 45: 46: String response = executeCommand(line); 47: 48: System.out.println("Sending: " + response); 49: out.println(response); 50: out.flush(); 51: } 52: } 53: 54: /** 55: Executes a single command.

53 56: @param line the command 57: @return the reply to send to the client. 58: */ 59: public String executeCommand(String line) 60: { 61: StringTokenizer tokenizer 62: = new StringTokenizer(line); 63: String command = tokenizer.nextToken(); 64: int account = Integer.parseInt(tokenizer.nextToken()); 65: if (command.equals("DEPOSIT")) 66: { 67: double amount = Double.parseDouble( 68: tokenizer.nextToken()); 69: bank.deposit(account, amount); 70: } 71: else if (command.equals("WITHDRAW")) 72: { 73: double amount = Double.parseDouble( 74: tokenizer.nextToken()); 75: bank.withdraw(account, amount);

54 76: } 77: else if (!command.equals("BALANCE")) 78: return "Invalid command"; 79: 80: return account + " " + bank.getBalance(account); 81: } 82: 83: private Socket s; 84: private Bank bank; 85: }

55 File 01: import; 02: import; 03: import; 04: import; 05: import; 06: import; 07: import; 08: 09: /** 10: This program tests the bank server. 11: */ 12: public class BankClient 13: { 14: public static void main(String[] args) throws IOException 15: { 16: final int SBAP_PORT = 8888; 17: Socket s = new Socket("localhost", SBAP_PORT);

56 18: InputStream in = s.getInputStream(); 19: OutputStream out = s.getOutputStream(); 20: BufferedReader reader = new BufferedReader( 21: new InputStreamReader(in)); 22: PrintWriter writer = new PrintWriter(out); 23: 24: String command = "DEPOSIT 3 1000\n"; 25: System.out.print("Sending: " + command); 26: writer.print(command); 27: writer.flush(); 28: String response = reader.readLine(); 29: System.out.println("Receiving: " + response); 30: 31: command = "WITHDRAW 3 500\n"; 32: System.out.print("Sending: " + command); 33: writer.print(command); 34: writer.flush(); 35: response = reader.readLine(); 36: System.out.println("Receiving: " + response);

57 37: 38: command = "QUIT\n"; 39: System.out.print("Sending: " + command); 40: writer.print(command); 41: writer.flush(); 42: 43: s.close(); 44: } 45: } 46: 47: 48: 49: 50:

58 URLConnection Class Provides convenient support for HTTP Can also handle FTP (file transfer protocol) Takes care of socket connection for you Makes it easy to communicate with a web server without giving HTTP commands

59 URL Connections Construct a URL object from a URL starting with the http or ftp prefix URL u = new URL(""); Use the URL 's openConnection() method to get the URLConnection URLConnection connection = u.openConnection(); Call the getInputStream method to obtain an input stream InputStream in = connection.getInputStream(); Construct a reader from the stream BufferedReader in = new BufferedReader(new InputStreamReader(in));

60 URL Connections Use the reader to read a line at a time from the resource boolean done = false; while (!done) { String input = reader.readLine(); if (input == null) done = true; else do something with the input }

61 HTTP Commands command request properties blank line HTTP command oSuch as GET item HTTP/1.0 request properties oSuch as If-Modified-Since blank line oseparates the command and its request properties from the input data

62 URLConnection Class Has methods to set request properties connection.setIf ModifiedSince(date); Set the request properties before calling getInputStream The URLConnection class sends all the request properties that are set to the web server

63 Server Response status line containing response code response parameters blank line status line containing response code oHTTP/1.1 200 OK oHTTP/1.1 400 NOT FOUND response parameters oThere may be several lines of parameters blank line oseparates the status and response parameters from the requested data

64 Retrieving Response Code and Message Cast the URLConnection object to the HttpConnection subclass Get the response code with getResponseCode Get the response message with getResponseMessage

65 Retrieve Other Response Information from URLConnection Content-Type int length = connection.getContentLength(); Content-Content String type = connection.getContentType();

66 File 01: import; 02: import; 03: import; 04: import; 05: import; 06: import; 07: import; 08: import; 09: import; 10: 11: /** 12: This program demonstrates how to use an URL connection 13: to communicate with a web server. Supply the URL on the 14: command-line, for example 15: java UrlGet 16: */ 17: public class URLGet

67 18: { 19: public static void main(String[] args) throws IOException 20: { 21: // get command-line arguments 22: 23: if (args.length != 1) 24: { 25: System.out.println("usage: java UrlGet URL"); 26: System.exit(0); 27: } 28: 29: // open connection 30: 31: URL u = new URL(args[0]); 32: URLConnection connection = u.openConnection(); 33: 34: // check if response code is HTTP_OK (200) 35: 36: HttpURLConnection httpConnection = (HttpURLConnection)connection; 37: int code = httpConnection.getResponseCode(); 38: if (code != HttpURLConnection.HTTP_OK)

68 39: { 40: String message = httpConnection.getResponseMessage(); 41: System.out.println(code + " " + message); 42: return; 43: } 44: 45: // read server response 46: 47: InputStream in = connection.getInputStream(); 48: BufferedReader reader = new BufferedReader( 49: new InputStreamReader(in)); 50: 51: boolean done = false; 52: while (!done) 53: { 54: String input = reader.readLine(); 55: if (input == null) done = true; 56: else System.out.println(input); 57: } 58: } 59: }

69 Dynamic Content Content of many pages changes by the minute oStock prices oWeather These pages are not stored in files When you request such a page oThe web server starts the appropriate program oSupplies it with input from the request oSends the output back

70 Request for Web Page with Dynamic Content The web server knows this URL denotes a program o/server o/cgi-bin The web server starts the appropriate program Supplies it with input from the request Sends the program's output back to the client

71 Form Data There are two HTTP commands for supplying input to server-side programs o GET o POST GET is simpler to use but can only be used for short inputs

72 GET Input is appended after the program URL following a ? Input is usually name/value pairs Example: GET bin/aa_moonphases?year=2000 HTTP/1.0 blank line Multiple name/value pairs are separated by an ampersand(&) city=Chicago&zip=60614

73 Encoding GET and Post requests must both be encoded The scheme is called URL encoding Characters that are not ASCII letters or numbers are encoded oSpaces are encoded as + character oOther bytes are encoded as %xy where xy is the hexadecimal value of the byte Use static methods URLEncoder.encode and URLDecoder.decode

74 POST Use POST for longer inputs Format POST url Content-Type type Content-Length length other request headers blank line input data Most common source of POST request is the submission of HTML form data

75 POST Don't format the POST request yourself Use the URLConnection class

76 POST Call setDoOutput method of URLConnection Call getOutputStream Send the data to that stream You can send URL encoded name/value pairs Close the output stream when you are done

77 The Program makes a ZIP code lookup by calling a server-side program hosted by US Postal Service The program expect POST input of name/value format o With a single name cytstzip o And value of either a ZIP code or a city and state Run the program from the command line o java PostZipQuery Beverly Hills, CA

78 File 01: import; 02: import; 03: import; 04: import; 05: import; 06: import; 07: import; 08: import; 09: import; 10: 11: /** 12: This program posts a query to a United States Postal Service 13: server that can look up the ZIP code for a city name. Supply 14: the city name and an optional state on the command line, 15: such as 16: java PostZipQuery Los Angeles, CA 17: */

79 18: public class PostZipQuery 19: { 20: public static void main(String[] args) throws IOException 21: { 22: // concatenate all command line arguments 23: 24: String input; 25: if (args.length > 0) 26: { 27: input = args[0]; 28: for (int i = 1; i < args.length; i++) 29: input += " " + args[i]; 30: } 31: else 32: input = "90210"; 33: 34: // establish URL connection 35: 36: URL u = new URL( 37: "");

80 38: URLConnection connection = u.openConnection(); 39: 40: // send POST data 41: 42: connection.setDoOutput(true); 43: OutputStream out = connection.getOutputStream(); 44: PrintWriter writer = new PrintWriter(out); 45: writer.print("ctystzip=" 46: + URLEncoder.encode(input) + "\n"); 47: writer.close(); 48: 49: // print server response 50: 51: InputStream in = connection.getInputStream(); 52: BufferedReader reader = new BufferedReader(new 53: InputStreamReader(in)); 54: 55: boolean done = false; 56: while (!done) 57: {

81 58: String inputLine = reader.readLine(); 59: if (inputLine == null) 60: done = true; 61: else 62: System.out.println(inputLine); 63: } 64: reader.close(); 65: } 66: }

Download ppt "Chapter 22 Internet Networking. CHAPTER GOALS To understand the concept of sockets To learn how to send and receive data through sockets To implement."

Similar presentations

Ads by Google