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

Slides:



Advertisements
Similar presentations
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Advertisements

1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Network Read/Write. Review of Streams and Files java.io package InputStream and OutputStream classes for binary bytes Reader and Writer classes for characters.
Java Threads Part II. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications.
1 Creating a network app Write programs that  run on different end systems and  communicate over a network.  e.g., Web: Web server software communicates.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
1 Multithreading. 2 Threads Program units that execute independently; multiple threads run “simultaneously” Virtual machine executes each thread for short.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L23 (Chapter 25) Networking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
TCP Socket Programming CPSC 441 Department of Computer Science University of Calgary.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Networking with Java. Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There.
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
Io package as Java’s basic I/O system continue’d.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Ryerson University CPS Distributing Computing with Java.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another.
Lab 6: Introduction to Sockets (Web Programming – Part 1) Reference: Head First Java (2 nd Edition) by Kathy Sierra & Bert Bates.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
Lab 2C Primer I/O in Java Sockets Threads More Java Stuffs.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Multithreading in JAVA
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Java Server Programming Web Interface for Java Programs.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Multi-Threading in Java
Exceptions and Assertions Chapter 15 – CSCI 1302.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Liang, Oreilly, Herbert Schildt, Joseph O’Neil Advanced Java Programming CSE 7345/5345/ NTU 531 Session 5 Welcome Back!!!
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Threads CS 21b. Threads in Java zDefinition Õunit of program control that represents an execution sequence zJava supports multi-threaded execution * Programs.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Java 13. Networking public class SumTest {
Object-Orientated Analysis, Design and Programming
Threads in Java Two ways to start a thread
CSE 341, S. Tanimoto Java networking-
Client-server Programming
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
CSCD 330 Network Programming
Multiplexing/Demux.
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Thread per client and Java NIO
Presentation transcript:

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Chantale Laurent-Rice Welcome Back!!! Office Hours: by appt 12:50pm-1:50pm SIC 353

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Server Objectives: Use server-side socket communication Understanding multithreading design Use methods of the thread class

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.

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

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

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp See Java in a nutshell chapter 10 I/O pg. 323

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Using Threads to communicate with Socket/Server

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Requirements: Client workstations will communicate to the Server by sending a message(s).

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp 1.declare the import statements for I/O

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp import java.net.*; import java.io.*;

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.

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Creating a thread public class Applet1Thread extends Thread { }

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp 3. Declare the constructor

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp public Applet1Thread () { }

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.

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?

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp public void run () { try { } catch( ) }

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.

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp instantiate a ServerSocket Object called ss. open a serverSocket on applet 1

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp ServerSocket ss = new ServerSocket (8200);

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.

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.

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp // Fill in the blanks. while(true ) { }

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.

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.

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp /* accept connection from server 2 */ Socket serverSocket2 = ss.accept ();

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp /* Print something if works */

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp System.out.println ("connection from server 2 accepted");

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.

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp /* get keywords from server 2 */

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp String keywords = inServer2.readLine ();

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

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp String results = RString.getSearchData(keywords);

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp /* send search results to server 2 */

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp outServer2.println (results);

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Now it’s time for the catch write the catch structure

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp catch (Exception e) { // Print a message }

Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp catch (Exception e) { System.out.println (e.getMessage()); }

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