Lecture-1: Networking

Post on 12-Jan-2016

41 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

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: 1Thread-1: 1Thread-1: 2Thread-1: 3Thread-1: 4Thread-1: 5main: 2main: 3main: 4main: 5Thread-0: 1Thread-0: 2Thread-0: 3Thread-0: 4Thread-0: 5

Without yield

main: 1Thread-1: 1main: 2Thread-1: 2main: 3Thread-1: 3main: 4Thread-1: 4main: 5Thread-1: 5Thread-0: 1Thread-0: 2Thread-0: 3Thread-0: 4Thread-0: 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 methodWhen 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 codeThe 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 es

tablish a connection to a URL. By using a URLConnection object dire

ctly, 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''pr

otocol 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, t

he 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+otuC6xaOhMDI6NDE6MDI=?=

Subject 159: =?GB2312?B?LTHUwjIwLTIyyNUoye4g29otv6ogv84pLQ==?=

Subject 160: 研华 HMI 优质上门服务 , 马上轻松拥有!

1.9 A Simple Web Server Instead of returning a requested fi

le, 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-thread

ed server that provides services defined by implementations of a nested Server.Service interface. It can provide multiple services (defined by mult

iple 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 call

ed ''UDP," for Unreliable Datagram Protocol. Sending datagrams is fast, but the tradeoff is tha

t 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

top related