1 / 21

Lecture-1: Networking

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

mikaia
Download Presentation

Lecture-1: Networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture-1: Networking

  2. 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

  3. 1.1 Threads • Language-level-supported multithread: • Java.lang.Runnable • Java.lang.Thread • Synchronized • Java.lang.Object (notify() and wait())

  4. 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

  5. 1.1 Threads (Cont.) main: 1 Thread-1: 1 main: 2 Thread-1: 2 main: 3 Thread-1: 3 main: 4 Thread-1: 4 main: 5 Thread-1: 5 Thread-0: 1 Thread-0: 2 Thread-0: 3 Thread-0: 4 Thread-0: 5 1 5 With yield main: 1 Thread-1: 1 Thread-1: 2 Thread-1: 3 Thread-1: 4 Thread-1: 5 main: 2 main: 3 main: 4 main: 5 Thread-0: 1 Thread-0: 2 Thread-0: 3 Thread-0: 4 Thread-0: 5 Without yield

  6. 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 keywordcan beused to specify the object whose lock is being used to synchronize the enclosed code.

  7. 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.

  8. 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

  9. 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();

  10. 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

  11. 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/

  12. 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)

  13. 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

  14. 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

  15. 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

  16. 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优质上门服务,马上轻松拥有!

  17. 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

  18. 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

  19. 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.

  20. 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

  21. 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

More Related