advanced java programming cse 7345/5345/ ntu 531

45
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/ Server Welcome Welcome Back!!! Back!!!

Upload: amandla

Post on 21-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Welcome Back!!!. Advanced Java Programming CSE 7345/5345/ NTU 531. Multithreaded/Sockets/Server. Office Hours: by appt 12:50pm-1:50pm SIC 353. Chantale Laurent-Rice. Welcome Back!!!. [email protected]. [email protected]. News. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Advanced Java Programming

CSE 7345/5345/ NTU 531Multithreaded/Sockets/Server

Welcome Welcome Back!!!Back!!!

Page 2: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

[email protected]@engr.smu.edu

Chantale Laurent-

Rice

Welcome Welcome Back!!!Back!!!

[email protected]@aol.com

Office Hours:by appt12:50pm-1:50pmSIC 353

Page 3: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

News

• Dr. El-Rewini is looking for someone with Java Swing background.

• See Dr. El-Rewini or call Beth at SIC for more info

Page 4: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

ServerObjectives:

Use server-side socket communication

Understanding multithreading design

Use methods of the thread class

Page 5: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Threads

• There are two ways to create new Threads. – One is to declare a class to be a subclass

of Thread, This subclass should override the run method of class Thread, and you can then allocate and start an instance of that class.

Page 6: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

class CreateThread extends Thread{

CreateThread(){

/* Do standard constructor initialization */start();

}public void run(){

/*Do the work this thread was created for */

}}

Page 7: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

The other way to create a thread is to declare a class that implements Runnable interface.

That class then implements the run method.

An instance of the class can then be allocated (passed as an argument when you’ve creating a thread

object).

Page 8: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

class CreateThread implements Runnable{

Thread thread;CreateThread(){

/* Do standard constructor initialization */

thread = new Thread(this. “second”); thread.start();

}public void run(){

/*Do the work this thread was created for */

}}

Page 9: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

See Java in a nutshell chapter 10

I/Opg. 323

Page 10: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

In class Exercise

Page 11: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Using Threads to communicate with

Socket/Server

Page 12: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

This exercise defines a thread for Applet1 which:

listens and processes requests from Server

Page 13: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Requirements:Client workstations will communicate to the Server by sending a message(s).

Page 14: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

1. declare the import statements for I/O

Page 15: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

import java.net.*;import java.io.*;

Page 16: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

2. Define a new Thread class that starts when you create an instance of it.

Page 17: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Creating a thread

public class Applet1Thread extends Thread

{

}

Page 18: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

3. Declare the constructor

Page 19: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

public Applet1Thread () { }

Page 20: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

4. Take a shot at coding the run() method. Since we are using I/O, the compiler will insist that a try/catch structure.

Page 21: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Do you want the while(true) read loop inside or outside the run?

Why?

Page 22: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

public void run () { try {

}

catch( ) }

Page 23: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

“answer the phone”

6. The The “answer the phone” function in Java Socket servers is provided by the ServerSocket class, so instantiate a ServerSocket Object called ss. Designate port 8200 on the ServerSocket constructor, the port number to be called by the Clients.

Page 24: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

instantiate a ServerSocket Object called ss.

open a serverSocket on applet 1

Page 25: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

ServerSocket ss = new ServerSocket (8200);

Page 26: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Since we are calling I/O methods, we will have to catch exceptions. Put the try/catch structure inside the while(true loop), and in the catch block print the Exception object but do not

terminate.

Page 27: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

If a client Socket fails during the join processing, we will simply print an error message and then ignore that Client looping back to the top of the while(true) to accept() the next Client that calls.

Page 28: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

// Fill in the blanks.while(true )

{

}

Page 29: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

7. Now the Server can go into a while(true) loop where it

answers the phone when a Client calls

Parses the Clients’ first message (the joinrequest)

Makes arrangements for continuing communication with the Client

Returns to the top of the loop to process the next Client that calls.

Page 30: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

8. At the top of the while(true) loop, enter the accept() method of the ServerSocket object. The Server thread will wait here until a Client calls. When a connection is made, the accept() method returns the reference to the Socket it has instantiated for this Client.

Page 31: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

/* accept connection from server 2 */ Socket serverSocket2 = ss.accept

();

Page 32: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

/* Print something if works */

Page 33: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

System.out.println ("connection from server 2

accepted");

Page 34: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

9. Once the Server has been created, instantiate to read and

write from the Socket.

Page 35: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

BufferedReader inServer2 = new BufferedReader (new InputStreamReader

(serverSocket2.getInputStream () ) );

PrintWriter outServer2 = new PrintWriter(serverSocket2.getOutputStream (), true );

Page 36: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

/* get keywords from server 2 */

Page 37: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

String keywords = inServer2.readLine ();

Page 38: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

perform the search for server 2 by calling the RString.getSearchData used by applet1

Page 39: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

String results =

RString.getSearchData(keywords);

Page 40: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

/* send search results to server 2 */

Page 41: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

outServer2.println (results);

Page 42: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

Now it’s time for the catch write the catch structure

Page 43: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

catch (Exception e) { // Print a message }

Page 44: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

catch (Exception e) { System.out.println (e.getMessage());

}

Page 45: Advanced Java Programming CSE 7345/5345/ NTU 531

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp

End