Chapter 22 Internet Networking
Feb 25, 2016
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 clients and servers • To communicate with web servers and
server-side applications through Hypertext Transfer Protocol (HTTP)
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
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
Data Transmission
• Data transmission consists of sending and receiving streams of zeros and ones along the network connection
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
Network Protocol• Local area network protocols
o Microsoft Networking o Novell NetWare o AppleTalk
• Protocol for connecting local area networks o Internet Protocol (IP)
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
Two Computers Communicating across the Internet
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
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
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
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)
TCP's Job • Attempt to deliver the data
• Try again if there are failures
• Notify the sender whether or not the attempt was successful
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
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
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
The OSI Reference Model
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
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
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
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
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
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
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
Web Server Response in Telnet
HTTP Commands
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
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();
Client and Server Sockets
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();
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
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 communicate11: with a web server. Supply the name of the host and the12: resource on the command-line, for example13: java WebGet java.sun.com index.html14: */15: public class WebGet16: {17: public static void main(String[] args) throws IOException
18: {19: // get command-line arguments20: 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 socket30: 31: final int HTTP_PORT = 80;32: Socket s = new Socket(host, HTTP_PORT);33: 34: // get streams35: 36: InputStream in = s.getInputStream();37: OutputStream out = s.getOutputStream();
38: 39: // turn streams into readers and writers40: 41: BufferedReader reader = new BufferedReader(42: new InputStreamReader(in));43: PrintWriter writer = new PrintWriter(out); 44: 45: // send command46: 47: String command = "GET /" + resource + " HTTP/1.0\n\n";48: writer.print(command);49: writer.flush();50: 51: // read server response52: 53: boolean done = false;54: while (!done)55: {56: String input = reader.readLine();57: if (input == null) done = true;
58: else System.out.println(input);59: }60: 61: // always close the socket at the end62: 63: s.close(); 64: }65: }
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
Simple Bank Access Protocol
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();
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();
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
Server Program • Use a StringTokenizer to analyze
the client command string
• The first token is the command
• The second token is the account number
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));
Server Program
• The server program never stops • When you are done running the
sever, kill it.
To Try Out Server Program • Run the server
• Use Telnet to connect to localhost , port number 8888
• Start typing commands
Using the Telnet Program to Connect to the BankServer
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:
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:
File BankService.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: import java.util.StringTokenizer;09: 10: /**11: Executes Simple Bank Access Protocol commands12: from a socket.13: */14: public class BankService15: {
16: /**17: Constructs a service object that processes commands18: from a socket for a bank.19: @param aSocket the socket20: @param aBank the bank21: */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 the30: end of input.31: */32: public void doService() throws IOException33: {34: BufferedReader in = new BufferedReader(35: new InputStreamReader(s.getInputStream()));
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.
56: @param line the command57: @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);
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: }
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 BankClient13: {14: public static void main(String[] args) throws IOException15: {16: final int SBAP_PORT = 8888;17: Socket s = new Socket("localhost", SBAP_PORT);
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);
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:
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
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));
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 }
HTTP Commandscommand 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
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
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
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
Retrieve Other Response Information from URLConnection
• Content-Type int length = connection.getContentLength();
• Content-Content String type = connection.getContentType();
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 the14: command-line, for example15: java UrlGet http://java.sun.com/index.html16: */17: public class URLGet
18: {19: public static void main(String[] args) throws IOException20: {21: // get command-line arguments22: 23: if (args.length != 1)24: {25: System.out.println("usage: java UrlGet URL");26: System.exit(0);27: }28: 29: // open connection30: 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)
39: {40: String message = httpConnection.getResponseMessage(); 41: System.out.println(code + " " + message);42: return;43: }44: 45: // read server response46: 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: }
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
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
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
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
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
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
POST
• Don't format the POST request yourself
• Use the URLConnection class
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
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
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 Service13: server that can look up the ZIP code for a city name. Supply14: the city name and an optional state on the command line, 15: such as 16: java PostZipQuery Los Angeles, CA17: */
18: public class PostZipQuery19: { 20: public static void main(String[] args) throws IOException21: { 22: // concatenate all command line arguments23: 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: else32: input = "90210";33: 34: // establish URL connection35: 36: URL u = new URL(37: "http://www.usps.gov/cgi-bin/zip4/ctystzip");
38: URLConnection connection = u.openConnection();39: 40: // send POST data41: 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 response50: 51: InputStream in = connection.getInputStream();52: BufferedReader reader = new BufferedReader(new53: InputStreamReader(in));54: 55: boolean done = false;56: while (!done)57: {
58: String inputLine = reader.readLine();59: if (inputLine == null)60: done = true;61: else62: System.out.println(inputLine);63: }64: reader.close();65: }66: }