COMP201 Java Programming

advertisement
COMP201 Java Programming
Part III: Advanced Features
Topic 14: Networking
Volume II,Chapter 3
COMP201 Topic 14 / Slide 2
Outline

Networking basics


Socket-level programming



IP addresses, ports, protocols, client-server interaction
Writing a client
Writing a server
URL connections and web servers


Retrieving information
Sending information
COMP201 Topic 14 / Slide 3
Networking Basics

Internet protocol (IP) address

Every host on Internet has a unique IP address
143.89.40.46, 203.184.197.198
203.184.197.196, 203.184.197.197, 127.0.0.1

More convenient to refer to using hostname string
cs.ust.hk, tom.com, localhost

Domain Naming Service (DNS) maps names to numbers

One hostname can correspond to multiple internet addresses:
tom.com: 203.184.197.196, 203.184.197.196,
203.184.197.197 (down to one now)
COMP201 Topic 14 / Slide 4
Networking Basics

java.net.InetAddress class converts between
hostnames and internet addresses
InetAddress tm = InetAddress.getByName(“tom.com");
InetAddress tm= InetAddress.getByName(“localhost");
//127.0.0.1
InetAddress tm = InetAddress.getLocalHost();

Can get array of addresses (if more than one)
InetAddress[] addrs;
addrs=InetAddress.getAllByName(“tom.com");
for (int i = 0; i < addr.length; i++)
System.out.println(addrs[i].getHostAddress());
InetAddressTest.java
COMP201 Topic 14 / Slide 5
Networking Basics

Ports

A port identifies a service within a host
Many different services can be running on the host

Many standard port numbers are pre-assigned
time of day 13, ftp 21, telnet 23, smtp 25, finger 79, http 80
see /etc/services on workstation for list of all assigned ports

IP address + port number = "phone number“ for service
COMP201 Topic 14 / Slide 6
Networking Basics

protocols : rules that facilitate communications
between machines
Time of day just reports time, ftp has put/get commands, etc.

Protocols must be standardized and documented
So machines can reliably work with one another

TCP (Transmission Control Protocol) vs UDP (User
Datagram Protocol, good for, e.g., video delivery)
COMP201 Topic 14 / Slide 7
Networking Basics

Client-Server interaction

Communication between hosts is two-way, but usually
the two hosts take different roles

Server waits for client to make request
Server registered on a known port with the host ("public phone
number")
Usually running in endless loop
Listens for incoming client connections
COMP201 Topic 14 / Slide 8
Networking Basics

Client "calls" server to start a conversation
Client making calls uses hostname/IP address and port number
Sends request and waits for response

Standard services always running
ftp, http, smtp, etc. server running on host using expected port

Server offers shared resource (information,database, files,
printer, compute power) to clients
COMP201 Topic 14 / Slide 9
Networking Basics

Using telnet to try out some services of servers:

Telnet assumes you want to connect to port 23 on the
receiving host (port 23 is where the telnet server is listening)

However there is an optional argument after the hostname
that allows you to connect to a different port

Try the following
Get time: telnet time-A.timefreq.bldrdoc.gov 13
Finger some one: telnet cssu100.cs.ust.hk 79 & type a username
Get HTML page: telnet tom.com
80 and enter a GET command
COMP201 Topic 14 / Slide 10
Socket-Level Programming

A socket is a bi-directional communication channel
between hosts

Send and receive data using streams
OutputStream
InputStream
Client
Server
InputStream

Next:


How to write a client
How to write a server
OutputStream
COMP201 Topic 14 / Slide 11
Socket-Level Programming/Client

To write a client

Create a new Socket with hostname and port number of the connection
Socket s = New Socket(String hostName,int portNumber);

Call s.getOutputStream() and s.getInputStream() to get
streams for sending and receiving infomation

Need to learn protocol used to communicate
– Know how to properly form requests to send to server
– Know how to interpret the server’s responses
COMP201 Topic 14 / Slide 12
Socket-Level Programming/Client

Example: A client using http to request page
Socket s = new Socket(“tom.com", 80);
PrintWriter send = new
PrintWriter(s.getOutputStream());
send.print("GET \n");
send.flush();
BufferedReader recv = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String line;
while ((line = recv.readLine()) != null)
System.out.println(line);
s.close()//HttpTest.java. This is basically what web browsers do.
COMP201 Topic 14 / Slide 13
Socket-Level Programming/Client

Use setSoTimeout method to limit the amount of time
waiting for data
Socket s = new Socket(“tom.com", 80);
s.setSoTimeout( 10000 ); // time out after 10 seconds

Can still wait indefinitely for initial connection
(construction of socket object).
COMP201 Topic 14 / Slide 14
Socket-Level Programming/Client

Solution: Construct socket in a separate thread and use the join
method to time out.
class SocketOpener implements Runnable
{ public SocketOpener(String aHost, int aPort)
{ socket = null;
host = aHost; port = aPort;
}
public void run()
{ try
{ socket = new Socket(host, port);}
catch (IOException exception) {}
}
public Socket getSocket()
{ return socket; }
…
private String host; private int port;
private Socket socket;
COMP201 Topic 14 / Slide 15
Socket-Level Programming/Client
class SocketOpener implements Runnable
{ // method to be called by main thread
public static Socket openSocket(String aHost, int aPort,
int timeout)
{ SocketOpener opener = new SocketOpener(aHost, aPort);
Thread t = new Thread(opener);
t.start();
try
{
t.join(timeout); // wait at most timeout milliseconds
}
// for the thread to dead
catch (InterruptedException exception){}
return opener.getSocket();
}
SocketOpenerTest.java
COMP201 Topic 14 / Slide 16
Socket-Level Programming/Server

To write a server

Create a new ServerSocket with a port number to listen on the port
ServerSocket s = New ServerSocket( portNumber);

Use accept() to listen on the port.

accept() returns a socket incoming when a client calls
Socket incoming = s.accept();

Call incoming.getOutputStream() and
incoming.getInputStream() to get streams for sending and
receiving information
COMP201 Topic 14 / Slide 17
Socket-Level Programming/Server
Example: Echo server

ServerSocket s = new ServerSocket(8189);
while (true)
{ Socket incoming = s.accept( );
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */ );
out.println( "Hello! Enter BYE to exit." );
…
}
EchoServer.java
COMP201 Topic 14 / Slide 18
Socket-Level Programming/Server

Multithread server: starts a separate thread for each connection.
public class ThreadedEchoServer
{
public static void main(String[] args )
{
int i = 1;
try{ServerSocket s = new ServerSocket(8190);
while (true)
{ Socket incoming = s.accept( );
System.out.println("Spawning " + i);
new ThreadedEchoHandler(incoming, i).start();
i++;
}
} catch (Exception e) …. //ThreadedEchoServer.java
COMP201 Topic 14 / Slide 19
Socket-Level Programming/Server
class ThreadedEchoHandler extends Thread
{
public ThreadedEchoHandler(Socket i, int c)
{ incoming = i; counter = c; }
public void run()
{
try
{
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
…
private Socket incoming;
private int counter; }
COMP201 Topic 14 / Slide 20
Socket-Level Programming/Server

ICQServer.java

A simple server that listens on port 7777.

Connect two clients so that they can talk to each other.

Can handle more than one pairs.

See code for details.
COMP201 Topic 14 / Slide 21
URL Connections

URL stands for Uniform Resource Locator
Neat scheme for uniquely identifying all kinds of network resources

Basic form <protocol>:<sitename><pathname>
http://www.cs.ust.hk/~liao/comp201/index.html
ftp://ftp.cs.ust.hk/pub/lzhang/teach/201/codes/HttpTest/HttpTest.java
file:/MyDisk/Letters/ToMom2-11-98
Protocols include files, http, ftp, gopher, news, mailto, etc.

Connecting to a URL, the hard way
Manually parse out the host, protocol, path from URL
Create a socket to host
Use protocol to send the right request & interpret response
But Java makes this much easier than that...
COMP201 Topic 14 / Slide 22
URL Connections/Retrieve Info

Open connection with java.net.URL
URL url1 = new
URL(“http://www.cs.ust.hk/~liao/comp201/index.html”);
URL url2 = new URL
(“ftp://ftp.cs.ust.hk/pub/lzhang/teach/201/codes/net/HttpT
est/HttpTest.java”);

To fetch contents of resource, use openStream method of URL, which
returns a InputStream
InputStream in1 = url1.openStream();
InputStream in2 = url2.openStream();
Now we can nest the InputStreams with other Java streams to retrieve contents

Advantages
1.
No need to worry about protocol details & port.
2.
Connection to particular file, not easy to do at Socket level
COMP201 Topic 14 / Slide 23
URL Connections/Retrieve Info

Use java.net.URLConnection for additional info about resource
1.
Create URLConnection object from URL,
URLConnection cnn = url.openConnection();
2.
Set properties of connections:
E.g. setDoOutPut(true) for sending information to the server
3.
Make connection: cnn.connect();
4.
Query header information:
getContentType, getContentLength, getContentEncoding,
getDate, getExpiration, getLastModified
5.
getInputStream for reading and getOutputStream for
writing
URLConnectionTest.java
COMP201 Topic 14 / Slide 24
URL Connections/Sending Info

Web servers receive information from clients using either GET or
POST

http://maps.yahoo.com/py/maps.py:
<form action="/py/maps.py?Pyt=Tmap&YY=28457" method=GET> … </form>

http://www.census.gov/ipc/www/idbprint.html:
<form method=post action="/cgi-bin/ipc/idbsprd">

Appropriate CGI (common gateway interface) script is called to
process info received and produce an HTML page to send back to
client

CGI scripts usually written in C, Perl, shell script. (Out of the scope
of this course.)

Will discuss servlets, Java alternative to CGI scripts
COMP201 Topic 14 / Slide 25
URL Connections/Sending Info

Send information to server using GET

Attach parameters to the end of URL
http://host/script?parameters

Separate parameters using “&” and encode parameters as
follows to avoid misinterpretation (URL encoding)

Static String encode (String s)

Replace space with “+”

Replace each non-alphanumeric character with “%” followed by the
hexadecimal code of the character
“Mastering C++”  “Mastering+C%2b%2b”

Disadvantage: long parameter string, might exceed limits of browsers.
GetTest.java
COMP201 Topic 14 / Slide 26
URL Connections/Sending Info

Sending information to server using POST:
Open URLConnection and send parameter using a stream
1.
Open a URLConnection:
URL url = new URL(“http:/host/script”);
URLConnection cnn = url.openConnection();
2.
Set up connection for output:
cnn.setDoOutput(true);
3.
Get a stream for sending data:
PrinterWriter out = new
PrintWriter(cnn.getOutputStream());
4.
Send parameters
Out.print(name1 + “=“ + URLEncoder.encode(value1) + “&” );
PostTest.java
Out.print(name2 + “=“ + URLEncoder.encode(value2) + “\n”);
Download