advanced java programming cse 7345 5345 ntu 531 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Advanced Java Programming CSE 7345/5345/ NTU 531 PowerPoint Presentation
Download Presentation
Advanced Java Programming CSE 7345/5345/ NTU 531

Loading in 2 Seconds...

play fullscreen
1 / 45

Advanced Java Programming CSE 7345/5345/ NTU 531 - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

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!!!. trice75447@aol.com. claurent@engr.smu.edu. News.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Advanced Java Programming CSE 7345/5345/ NTU 531' - amandla


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chantale laurent rice

Office Hours:

by appt

12:50pm-1:50pm

SIC 353

Chantale Laurent-Rice

Welcome Back!!!

trice75447@aol.com

claurent@engr.smu.edu

slide3
News
  • Dr. El-Rewini is looking for someone with Java Swing background.
  • See Dr. El-Rewini or call Beth at SIC for more info
server
Server

Objectives:

Use server-side socket communication

Understanding multithreading design

Use methods of the thread class

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

class CreateThread extends Thread{ CreateThread() { /* Do standard constructor initialization */ start(); } public void run() { /*Do the work this thread was created for */ }}

slide7

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

slide8

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 */ }}

creating a thread
Creating a thread

public class Applet1Thread extends Thread

{

}

slide20
4. Take a shot at coding the run() method. Since we are using I/O, the compiler will insist that a try/catch structure.
answer the phone
“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.

slide26

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.

slide27

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.

slide29

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.

slide30

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.

accept connection from server 2 socket serversocket2 ss accept
/* accept connection from server 2 */ Socket serverSocket2 = ss.accept ();
slide35

BufferedReader inServer2 = new BufferedReader (new InputStreamReader (serverSocket2.getInputStream () ) );PrintWriter outServer2 = new PrintWriter(serverSocket2.getOutputStream (), true );

slide38
perform the search for server 2 by calling the RString.getSearchData used by applet1
slide42
Now it’s time for the catch

write the catch structure

slide43
catch (Exception e) {

// Print a message

}

slide44
catch (Exception e) {

System.out.println (e.getMessage());

}