Title: Networking in Java
1Networking in Java
- Representation and Management of Data on the
Internet
2Client-Server Model
Client Application
Port 80
132.65.32.29
Server Application
64.208.34.100
Client Application
www.google.com
132.68.7.11
3Clients
- initiates connection
- retrieves data
- displays data
- responds to user input
- requests more data
- Examples
- Web Browser
- Chat Program
- PC accessing files
4Servers
- responds to connection
- receives request for data
- looks it up
- delivers it
- Examples
- Web Server
- Database Server
- Domain Name Server
5Internet Architecture Model
6What is a Protocol?
Hi
TCP connection req.
Hi
7TCP and UDP
- TCP (Transmission Control Protocol)
- connection-based protocol
- provides a reliable flow of data between two
computers - UDP (User Datagram Protocol)
- a protocol that sends independent packets of
data, called datagrams, from one computer to
another - arrival of datagrams is not guaranteed
- UDP is not connection-based like TCP
8How to Choose TCP or UDP
- Use TCP when reliability is critical
- HTTP
- FTP
- Telnet
- Use UDP when reliability is not critical
- Ping
- Clock
9Host and Port
- Destination in the Internet is identified by
host port - a 32 bits IP-address
- a 16 bits port
- Q Why dont we specify the port in a Web
browser? - Ports 0-1023 are restricted
- Do not use them
10Destination of a Connection
- Q How does an HTTP request know where the
server is to which it is intended? - Q How does an HTTP response know where the
server is to which it is intended? - Q There can be more then one application running
on the same host, how do we know to which
application a message is intended?
11A Connection to Many Applications
- Q There can be many applications that are on the
same time connected to the same host, - (for example, many browser and one search engine)
- How do we send the right transmission to each
- client?
- A By adding the IP address of the client and the
port of the client to the IP packets
12Known Ports
- Some known ports are
- 20, 21 FTP
- 23 telnet
- 25 SMTP
- 43 whois
- 80 HTTP
- 119 NNTP
Client Application
21 23 25 43 80 119
13Internet Addresses
- InetAddress a class that represents Internet
Protocol (IP) addresses and the names of hosts - Getting the InetAdddress
- getLocalHost Returns the local host
- getByName(String host) For the given host name
one of its IP addresses is returned - getAllByName(String host) For a given host name
all its IP addresses are returned
14Methods of InetAddress
- getHostAddress Returns the IP address of the
host, in the form d.d.d.d - getHostName Returns the name of the host
15Working with URLs
- URL (Uniform Resource Locator) - a reference (an
address) to a resource on the Internet - http//www.cs.huji.ac.il80/dbi/main.htmlnotes
16Creating URLs
- The class URL is defined in the packagejava.net
- Basic constructor
- URL w3c new URL("http//www.w3.org/")
- Relative links
- Are created from baseURL relativeURL
- URL amaya new URL(w3c, Amaya/Amaya.html)
- URL jigsaw new URL(w3c, JigsawGetting)
17Creating URLs (cont.)
- The following two are equivalent
- URL dbiNotes
- new URL(http//www.cs.huji.ac.il80/
dbi/main.htmlnotes) - URL dbiNotes new URL(http,
www.cs.huji.ac.il, 80,
dbi/main.htmlnotes) - URL Construction can throw MalformedURLException
18Why Do We Need the URL Class?
- The main usage of URL is for parsing URLs
- getting the protocol
- getting the host
- getting the port
- getting the file name
- getting the reference
19Printing URL Details
import java.net. public class UrlUtilities
public static void printUrlDetails(URL url)
System.out.println("Protocol "
url.getProtocol()) System.out.println("Host
" url.getHost()) System.out.println("File
name " url.getFile()) System.out.println(
"Port " url.getPort()) System.out.println(
"Reference " url.getRef())
20Running the Example (1)
- If we try
- printUrlDetails(http//www.cs.huji.ac.il)
- We get
- Protocol http
- Host www.cs.huji.ac.il
- File name
- Port -1
- Reference null
21Running the Example (2)
- If we try
- printUrlDetails(http//www.cs.huji.ac.il80/dbi/
main.htmlnotes) - We get
- Protocol http
- Host www.cs.huji.ac.il
- File name /dbi/main.html
- Port 80
- Reference notes
22Reading From A URL
url
23import java.net. I import java.io. public
class UrlUtilities public static void
printUrlDetails (URL url) ...... public static
void printUrlContents(URL url) throws
IOException BufferedReader in new
BufferedReader( new InputStreamReader(url.ope
nStream())) String inputLine while
((inputLine in.readLine()) !
null) System.out.println(inputLine)
in.close()
24Reading From a URL
- For reading a URL using a proxy, we run java with
the environment variables for http.proxyHost and
http.proxyPort set properly - java -Dhttp.proxyHostwwwproxy.cs.huji.ac.il
- Dhttp.proxyPort8080 ...
- Another option is to set the environment
variables in the program itself
25import java.net. I import java.io. public
class UrlUtilities public static void
printUrlDetails (URL url) ...... public static
void printUrlContents(URL url) ...... public
static void setHujiProxy System.setProperty("h
ttp.proxyHost", "wwwproxy.cs.huji.ac
.il") System.setProperty("http.proxyPort","8080
")
26Running the example
- If we try
- printUrlContents(http//www.cs.huji.ac.il/dbi)
- We get
- ltHTMLgtltHEADgtltMETA NAME"Description" CONTENT"The
dbi course homepage"gt ltTITLEgtRepresentation and
Management of Data on the Internetlt/TITLEgt ...
27Connecting to A URL
url
28Interacting with a CGI script
- Create a URL
- Open a connection to the URL
- Set output capability on the URLConnection
- Get an output stream from the connection
- This output stream is connected to the standard
input stream of the cgi-bin script on the server - Write to the output stream
- Close the output stream
29HTTP connections
- You can create a connection to an HTTP server
with an object - HttpURLConnection
- This object extends the URLConnection object
- getResponseCode
- getResponseMessage
- setRequestMethod
- Look in the Java API
30URLEncoder
- Contains a utility method encode for converting a
string into an encoded format - To convert a string, each character is examined
in turn - The ASCII characters 'a' 'z', 'A' 'Z', '0'
'9', ".", "-", "", "_" remain the same - Space is converted into a plus sign ''
- All other characters are converted into the
3-character string "xy", where xy is the
two-digit hexadecimal representation of the lower
8-bits of the character
31URL Connection Example
- The next example connects to a CGI script on
www.walla.co.il a search tool is given a word
to search
32import java.net. import java.io. public
class SearchWalla public static main(String
args) try UrlUtilities.setHujiProxy()
URL url new URL(http//search.walla.co
.il/guide/h_srch.cgi) URLConnection
connection url.openConnection()
connection.setDoInput(true)
connection.setDoOutput(true) connection.se
tAllowUserInteraction(false)
33 PrintWriter out new
PrintWriter(connection.getOutputStream())
out.println("q" URLEncoder.encode(args0))
out.close() BufferedReader in new
BufferedReader ( new InputStreamReader (
connection.getInputStream() )) String
inputLine while ((inputLine in.readLine())
! null) System.out.println(inputLine)
in.close() catch (Exception e)
e.printStackTrace()
34Sockets
- Communication is between sockets
- A socket is one end-point of a two-way
communication link between two programs running
on the network - A socket has a binding to a port to which it
listens - A socket implementation gives us the ability to
read from it and write to it as if it is a file
35In Sockets
- A socket knows the following things
- The port number of the remote host
- The host name (InetAddress) of the remote host
- The local port to which it is bound
- The local address of its local host
36Sockets
- The server has a socket that listens to a known
port, e.g., Web server and port 80 - The server waits (listening to the socket) until
a client requests a connection - A client requests a connection on the known host
and port of the server - Q What should the server do so that it can serve
other clients as well?
37Client Requests for a Connection
Port 8090
Port 8090 Local Port 80 Local host
www.google.com Remote host 132.65.32.29
Port 80
Client application
132.65.32.29
Port 8888 Local Port 80 Local host
www.google.com Remote host 132.68.7.11
Port 8888
Client application
Server application
132.68.7.11
64.208.34.100
www.google.com
38Socket
- Class Socket implements the client side of the
connection - Class ServerSocket implements the server side
of the connection
39Using a Socket
40Using a Socket
- // Constructors (partial list)
- public Socket(String host, int port)
- // Methods (partial list)
- public void close()
- public InetAddress getInetAddress()
- public int getLocalPort()
- public InputStream getInputStream()
- public OutputStream getOutputStream()
- public int getPort()
41Using a Socket (client)
- A client
- Opens a socket (with a binding to which port?)
- Opens an input stream and output stream to the
socket - Reads from and write to the stream according to
the clients protocol - Closes the streams
- Closes the socket
42Using a Socket (cont.)
- A server
- Opens a socket
- Opens an input stream and output stream to the
socket - Reads from and writes to the stream according to
the server's protocol - Closes the streams
- Closes the socket
43A Client Example
- The following is a client that connects to a Time
Server (port 13) and returns the current time
44import java.net. import java.io. public class
TimeClient public static void main(String
args) int TimePort 13 String host
"www.cs.huji.ac.il" try Socket socket
new Socket(host, TimePort) BufferedReader in
new BufferedReader(new InputStreamReader
(socket.getInputStream())) String lineInput
while ((lineInput in.readLine()) ! null)
System.out.println(lineInput) catch
(UnknownHostException e) catch
(IOException ioe)
45Running the TimeClient
46ServerSocket
- // Constructors (partial list)
- public ServerSocket(int port)
- public ServerSocket(int port, int count)
- // Methods (partial list)
- public Socket accept()
- public void close()
- public InetAddress getInetAddress()
- public int getLocalPort()
47A Server Example
- The following is a time server that returns the
time - Note When the ServerSocket constructor is given
port number 0, a random free port is chosen
48import java.net. import java.io. public class
TimeServer public static void main(String
args) throws Exception InetAddress
localHost InetAddress.getLocalHost() ServerS
ocket listen new ServerSocket(0) System.out.
println("Listening on port " listen.getLo
calPort() " of host " localHost) while
(true) Socket client listen.accept()
System.out.println(client.toString())
PrintWriter out new PrintWriter(clie
nt.getOutputStream(), true) out.println(new
Date()) client.close()
49More on Server Socket
- A ServerSocket waits for requests to come in over
the network - It performs some operation based on that request,
and then possibly returns a result to the
requester - The actual work of the ServerSocket is performed
by an instance of the SocketImpl class - The abstract class SocketImpl is a common
superclass of all classes that actually implement
sockets - It is used to create both client and server
sockets
50(No Transcript)
51HelloServer
- import java.net.
- import java.io.
- // A server that says 'hello'
- class HelloServer
- public static void main(String args)
- int port Integer.parseInt(args0)
- ServerSocket server null
- try
- server new ServerSocket(port)
- catch (IOException ioe)
- System.err.println("Couldn't run"
- "server on port "port)
- return
-
52HelloServer (cont.)
- while(true)
- try
- Socket connection server.accept()
- BufferedReader reader
- new BufferedReader(
- new InputStreamReader(
- connection.getInputStream()
)) - PrintWriter writer
- new PrintWriter(
- new OutputStreamWriter(
- connection.getOutputStream(
))) -
- String clientName reader.readLine()
- writer.println("Hello "clientName)
- writer.flush()
- catch (IOException ioe1)
-
53HelloClient
- import java.net.
- import java.io.
- // A client of an HelloServer
- class HelloClient
- public static void main(String args)
- String hostname args0
- int port Integer.parseInt(args1)
-
- Socket connection null
- try
- connection new Socket(hostname,
port) - catch (IOException ioe)
- System.err.println("Connection
failed") - return
-
54HelloClient (cont.)
- try
- BufferedReader reader
- new BufferedReader(
- new InputStreamReader(
- connection.getInputStream()))
- PrintWriter writer
- new PrintWriter(
- new OutputStreamWriter(
- connection.getOutputStream()))
-
- writer.println(args2) // client name
- writer.flush()
- String reply reader.readLine()
- System.out.println("Server reply
"reply) - catch (IOException ioe1)
-
55Datagrams
- Datagram packets are used to implement a
connectionless, packet based, delivery service - Each message is routed from one machine to
another based solely on information contained
within that packet - Multiple packets sent from one machine to another
might be routed differently, and might arrive in
any order - Packets may be lost or duplicated during transit
- The class DatagramPacket represents a datagram in
Java
56DatagramPacket Class
- //Constructors (partial list)
- public DatagramPacket(byte ibuf, int ilength)
- public DatagramPacket(byte ibuf, int ilength,
- InetAddress iaddr, int iport)
- // Methods (partial list)
- public synchronized InetAddress getAddress()
- public synchronized int getPort()
- public synchornized byte getData()
- int getLength()
- void setAddress(InetAddress iaddr)
- void setPort(int iport)
- void setData(byte ibuf)
- void setLength(int ilength)
57DatagramSocket
- This class represents a socket for sending and
receiving datagram packets - Addressing information for outgoing packets is
contained in the packet header - A socket that is used to read incoming packets
must be bound to an address (sockets that are
used for sending must be bound as well, but in
most cases it is done automatically) - There is no special datagram server socket class
- Since packets can be lost, the ability to set
timeouts is important
58Class DatagramSocket
- // Constructors (partial list)
- DatagramSocket()
- DatagramSocket(int port)
-
- // Methods (partial list)
- void close()
- int getLocalPort()
- int getSoTimeout()
- void receive(DatagramPacket p)
- void send(DatagramPacket p)
- setSoTimeout(int timeout)
59Echo Servers
- A common network service is an echo server
- An echo server simply sends packets back to the
sender - A client creates a packet, sends it to the
server, and waits for a response - Echo services can be used to test network
connectivity and performance
60import java.net. import java.io. import
java.util. public class EchoClient int
echoPort 7000, msgLen 16, timeOut1000
public static void main(String argv) try
DatagramSocket socket new
DatagramSocket() DatagramPacket packet
byte msg new bytemsgLen
InetAddress echoHost InetAddress.getByName(argv
0) packet new DatagramPacket(msg,msgLen,
echoHost,echoPort) socket.send(packet)
socket.setSoTimeout(timeOut)
socket.receive(packet) catch
(InterruptedIOException e) System.out.println("Ti
meout") catch (Exception e)
61import java.net.import java.io.import
java.util. public class EchoServer
static int echoPort 7000 public static
void main(String args) try
DatagramSocket socket new DatagramSocket(echoPor
t) DatagramPacket p,reply
for () sock.receive(p)
System.out.println(p.getAddress()) reply
new DatagramPacket(p.getData(),p.getLength(),
p.getAddress(),p.getPort())
socket.send(reply) catch
(Exception e)