Download lecture-1(network).

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture-1: Networking
Topics













Threads
Downloading the Contents of a URL
Using a URLConnection
Sending Email Through a URLConnection
A Simple Network Client
A Generic Client
A HTTP Client
A POP Client
A Simple Web Server
A Proxy Server
A Generic Multi-Threaded Server
Sending Datagrams
Receiving Datagrams
1.1 Threads

Language-level-supported multithread:




Java.lang.Runnable
Java.lang.Thread
Synchronized
Java.lang.Object (notify() and wait())
1.1 Threads (Cont.)

Basic concept of threads:



Extends from class Thread
Implements Runnable inteface
ThreadLocal class (Java 1.2)
Example 4.1 ThreadDemo.java
1.1 Threads (Cont.)
main: 1
Thread-1:
Thread-1:
Thread-1:
Thread-1:
Thread-1:
main: 2
main: 3
main: 4
main: 5
Thread-0:
Thread-0:
Thread-0:
Thread-0:
Thread-0:
1
2
3
4
5
1
2
3
4
5
Without yield
main: 1
Thread-1:
main: 2
Thread-1:
main: 3
Thread-1:
main: 4
Thread-1:
main: 5
Thread-1:
Thread-0:
Thread-0:
Thread-0:
Thread-0:
Thread-0:
1
2
3
4
5
1
2
3
4
5
1 5 With yield
1.1 Threads (Cont.)

Thread-safe
An instance of a class can be accessed by many
threads concurrently. It may cause inconsistence
sometimes. To prevent this, you can use keyword
synchronized:


synchronized method
When you call any synchronized method, that object is
locked and no other synchronized method of that object
can be called until the first one finishes and releases the lock.
synchronized code
The synchronized keyword can be used to specify the
object whose lock is being used to synchronize the enclosed
code.
1.1 Threads (Cont.)

Deadlock


Thread synchronization involves acquiring an exclusive
"lock."
Deadlock occurs when two or more threads are all waiting to
acquire a lock that is currently held by one of the other
waiting threads.

One good technique for preventing it, however, is for all
threads to always acquire all the locks they need in the
same order.
1.1 Threads (Cont.)

Timer
Java.util.Timer and java.util.TimerTask




ReminderBeep.java
Reminder.java
AnnoyingBeep.java
http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
1.2 Downloading the Contents of a URL


You can download the network
resource referred to by a URL using
the URL class.
GetURL.java



Methods of URL
openStream()
Fetch.java

url.getContent();
1.3 Using a URLConnection


The URLConnection class is used to
establish a connection to a URL.
By using a URLConnection object
directly, instead of relying on
openStream(), you have much more
control over the process of downloading
the contents of a URL.
GetURLInfo.java
1.4 Sending Email Through a URLConnection


Java includes support for different URL protocols
through''protocol handlers" that are implemented internally to
the JDK. In the Java 1.1 version of the JDK, these handlers
include support for the mailto: protocol.
Example 5-3 shows a program that uses a mailto: URL to send
email.


The program prompts the user to enter the sender, recipient or
recipients, subject, and body of the message, and then creates an
appropriate mailto: URL and obtains a URLConnection object for it.
The program uses the setDoInput() and setDoOutput() methods to
specify that it is writing data to the URLConnection.
http://jakarta.apache.org/commons/email/
1.5 A Simple Network Client

A simple network client program.



It sends a plain text to server and receives
a response from the server and print it.
It uses socket instead of URL to connect to
a server.
The socket uses InputStream and
OutputStream to communicate with server.
Connect.java (start the HttpMirror first)
1.6 A Generic Client

Connect.java only works for few situation, the
GenericClient.java can handle the service
based on plain text.


HTTP
Download file from a web server
POP
Interact with a POP server
GenericClient.java
1.7 A Http Client


HttpClient, that downloads the contents of a
URL from a Web server and writes it to a file
or to the console.
While GetURL relies on the URL class (and
the content handlers it uses) to handle
protocol details, HttpClient connects directly
to a Web server, and communicates with it
using the HTTP (only HTTP) protocol.
HttpClient.java
1.8 A POP Client

A POP client program can connect to a
mail server, list the messages based on
their subject or size, then delete the
messages that match the specified
condition.
PopClean.java
1.8 A POP Client (Cont.)
Subject 155: Registration Confirmation
Subject 156: 8.57% UP!
Subject 157: =?gb2312?B?x+u9zMT60ru49kpBVkHOyszi?=
Subject 158:
=?GB2312?B?tLq92szYvNu5qdOmIMrWu/ogMb+otuC6xaOhMDI6NDE6
MDI=?=
Subject 159: =?GB2312?B?LTHUwjIwLTIyyNUoye4g29otv6ogv84pLQ==?=
Subject 160: 研华HMI优质上门服务,马上轻松拥有!
1.9 A Simple Web Server


Instead of returning a requested
file, however, this server simply
"mirrors" the request back to the
client as its reply.
This can be useful when debugging
Web clients.
HttpMirror.java
1.10 A Proxy Server

A simple, single-threaded proxy server.



A proxy server is one that acts as a proxy for
some other real server.
When a client connects to a proxy server, the
proxy forwards the client's requests to the real
server, and then forwards the server's responses
to the client.
To the client, the proxy looks like the server. To
the real server, the proxy looks like a client.
SimpleProxyServer.java
1.11 A Generic Multi-Threaded Server

The Server class it defines is a multi-threaded
server that provides services defined by
implementations of a nested Server.Service
interface.


It can provide multiple services (defined by
multiple Service objects) on multiple ports, and it
has the ability to dynamically load and instantiate
Service classes and add (and remove) new
services at runtime.
It logs its actions to a specified stream and limits
the number of concurrent connections to a
specified maximum.
1.12 Sending Datagrams

Datagram communication is sometimes called
''UDP," for Unreliable Datagram Protocol.


Sending datagrams is fast, but the tradeoff is that
that they are not guaranteed to reach their
destination.
Multiple datagrams are not guaranteed to travel to
their destination by the same route or to arrive at
their destination in the order in which they were
sent.
UDPSend.java
1.13 Receiving Datagrams

To receive a datagram:



you must first create a DatagramSocket that
listens on a particular port of the local host. This
socket can only be used to receive packets sent to
that particular port.
Then, you must create a DatagramPacket with a
byte buffer into which datagram data is stored.
Finally, you call the DatagramSocket.receive()
method to wait for a datagram to arrive on the
specified port.
UDPReceive.java