Top Banner
Chapter 22 Internet Networking
80

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

Apr 02, 2015

Download

Documents

Jaylene Graley
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 22 Internet Networking. CHAPTER GOALS To understand the concept of sockets To learn how to send and receive data through sockets To implement.

Chapter 22

Internet Networking

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

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)

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

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

o The World Wide Web

o Email

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

Physical Media• Network cabling

o Electrical impulses represent the information flow

• Telephone wires using a modem o Data travels encoded as tones

• Air in a wireless network o Signals are sent by modulating radio frequencies

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

Data Transmission

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

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

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

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

Network Protocol

• Local area network protocols

o Microsoft Networking

o Novell NetWare

o AppleTalk

• Protocol for connecting local area networks

o Internet Protocol (IP)

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

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

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

Two Computers Communicating across the Internet

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

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 130.65.86.66 • 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

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

Domain Naming Service• In addition to an IP address, computers can have

an easy-to-remenber domain name

o For example, java.sun.com

• 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:

o It asks the DNS for the numeric Internet Address

o It includes the numeric address with the request for data

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

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

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

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)

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

TCP's Job

•  Attempt to deliver the data

• Try again if there are failures

• Notify the sender whether or not the attempt was successful

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

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

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

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

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

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

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

The OSI Reference Model

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

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

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

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, http://java.sun.com/index.html

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

Browser Steps• You type http://java.sun.com/index.html 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: java.sun.com 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 java.sun.com

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

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

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

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

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

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 java.sun.com 80

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

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

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

Web Server Response in Telnet

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

HTTP Commands

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

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, java.sun.com final int HTTP_PORT = 80;

Socket s = new Socket("java.sun.com", HTTP_PORT); •If it can't find the host, the Socket constructor throws an UnknownHostException

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

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();

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

Client and Server Sockets

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

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();

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

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 java.sun.com index.html

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

File WebGet.java 01: import java.io.BufferedReader;

02: import java.io.InputStream;

03: import java.io.InputStreamReader;

04: import java.io.IOException;

05: import java.io.OutputStream;

06: import java.io.PrintWriter;

07: import java.net.Socket;

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 java.sun.com index.html

14: */

15: public class WebGet

16: {

17: public static void main(String[] args) throws IOException

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

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();

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

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;

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

58: else System.out.println(input);

59: }

60:

61: // always close the socket at the end

62:

63: s.close();

64: }

65: }

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

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

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

Simple Bank Access Protocol

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

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();

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

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();

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

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

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

Server Program

• Use a StringTokenizer to analyze the client command string

• The first token is the command

• The second token is the account number

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

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));

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

Server Program

• The server program never stops

• When you are done running the sever, kill it.

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

To Try Out Server Program • Run the server

• Use Telnet to connect to localhost , port number 8888

• Start typing commands

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

Using the Telnet Program to Connect to the BankServer

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

File BankServer.java01: import java.io.IOException;02: import java.net.ServerSocket;03: import java.net.Socket;04: 05: /**06: A server that executes the Simple Bank Access Protocol.07: */08: public class BankServer09: { 10: public static void main(String[] args ) throws IOException11: { 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:

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

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:

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

File BankService.java

01: import java.io.BufferedReader;

02: import java.io.InputStream;

03: import java.io.InputStreamReader;

04: import java.io.IOException;

05: import java.io.OutputStream;

06: import java.io.PrintWriter;

07: import java.net.Socket;

08: import java.util.StringTokenizer;

09:

10: /**

11: Executes Simple Bank Access Protocol commands

12: from a socket.

13: */

14: public class BankService

15: {

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

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()));

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

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.

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

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);

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

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: }

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

File BankClient.java01: import java.io.BufferedReader;

02: import java.io.InputStream;

03: import java.io.InputStreamReader;

04: import java.io.IOException;

05: import java.io.OutputStream;

06: import java.io.PrintWriter;

07: import java.net.Socket;

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);

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

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);

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

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:

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

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

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

URL Connections• Construct a URL object from a URL starting with the http or

ftp prefix

URL u = new URL("http://java.sun.com/index.html");

• 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));

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

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 }

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

HTTP Commands

command request properties blank line

• HTTP command

o Such as GET item HTTP/1.0

• request properties

o Such as If-Modified-Since

• blank line

o separates the command and its request properties from the input data

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

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

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

Server Responsestatus line containing response code response parameters

blank line

• status line containing response code

o HTTP/1.1 200 OK

o HTTP/1.1 400 NOT FOUND

• response parameters

o There may be several lines of parameters

• blank line o separates the status and response parameters

from the requested data

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

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

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

Retrieve Other Response Information from URLConnection

• Content-Type

int length = connection.getContentLength();

• Content-Content String type = connection.getContentType();

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

File URLGet.java01: import java.io.BufferedReader;

02: import java.io.InputStream;

03: import java.io.InputStreamReader;

04: import java.io.IOException;

05: import java.io.OutputStream;

06: import java.io.PrintWriter;

07: import java.net.HttpURLConnection;

08: import java.net.URL;

09: import java.net.URLConnection;

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 http://java.sun.com/index.html

16: */

17: public class URLGet

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

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)

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

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: }

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

Dynamic Content• Content of many pages changes by the minute

o Stock prices

o Weather

• These pages are not stored in files

• When you request such a page

o The web server starts the appropriate program

o Supplies it with input from the request o Sends the output back

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

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

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

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

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

GET• Input is appended after the program URL following a ?

• Input is usually name/value pairs

• Example: GET Mach.usno.navy.mil/cgi-bin/aa_moonphases?year=2000 HTTP/1.0blank line

• Multiple name/value pairs are separated by an ampersand(&)city=Chicago&zip=60614

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

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 o Spaces are encoded as + character o Other bytes are encoded as %xy where xy is

the hexadecimal value of the byte

• Use static methods URLEncoder.encode and URLDecoder.decode

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

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

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

POST

• Don't format the POST request yourself

• Use the URLConnection class

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

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

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

PostZipQuery.java• 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

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

File PostZipQuery.java01: import java.io.BufferedReader;

02: import java.io.InputStream;

03: import java.io.InputStreamReader;

04: import java.io.IOException;

05: import java.io.OutputStream;

06: import java.io.PrintWriter;

07: import java.net.URL;

08: import java.net.URLConnection;

09: import java.net.URLEncoder;

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: */

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

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: "http://www.usps.gov/cgi-bin/zip4/ctystzip");

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

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: {

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

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: }