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
Physical Media
- Network cabling
- Electrical impulses represent the information flow
- Telephone wires using a modem
- Data travels encoded as tones
- Air in a wireless network
- 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
- consists of the information one computer wants to send to another
- Network protocol data
- describes how to reach the intended computer
- describes how to check for errors in the transmission
Network Protocol
- Local area network protocols
- Microsoft Networking
- Novell NetWare
- AppleTalk
- Protocol for connecting local area networks
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
- Each is one byte (a number between 0 and 255)
- For example 130.65.86.66
- To send data to B, A needs to know B's Internet address
- 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
- 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:
- It asks the DNS for the numeric Internet Address
- 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
- 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
- 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
- In this case: java.sun.com
- This identifies the computer to which you want to connect
- 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
- 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
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
Telnet
- Telnet program allows you to
- Type characters to send to a remote computer and
- 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
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
Client and Server Sockets
Client Program - Readers and Writers
Client Program - WebGet
File WebGet.java
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
Server Program - Readers and Writers
Server Program
- The socket s is closed if:
- The string received by the client equals QUIT command
- Or it is null
- 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
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.java
File BankService.java
File BankClient.java
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
HTTP Commands
command
request properties
blank line
- HTTP command
- Such as GET item HTTP/1.0
- request properties
- Such as If-Modified-Since
- blank line
- separates the command and its request properties from the input data
URLConnection Class
Server Response
status line containing response code
response parameters
blank line
- status line containing response code
- HTTP/1.1 200 OK
- HTTP/1.1 400 NOT FOUND
- response parameters
- There may be several lines of parameters
- blank line
- 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
File URLGet.java
Dynamic Content
- Content of many pages changes by the minute
- These pages are not stored in files
- When you request such a page
- The web server starts the appropriate program
- Supplies it with input from the request
- Sends the output back
Request for Web Page with Dynamic Content
- The web server knows this URL denotes a program
- 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
- 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.0
blank 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
- Spaces are encoded as + character
- Other bytes are encoded as %xy where xy is the hexadecimal
value of the byte
- Use static methods URLEncoder.encode and URLDecoder.decode
POST
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
- With a single name cytstzip
- And value of either a ZIP code or a city and state
- Run the program from the command line
- java PostZipQuery Beverly Hills, CA
File PostZipQuery.java