Block 15 Developing the Calculator application

Slides:



Advertisements
Similar presentations
CS Network Programming CS 3331 Fall CS 3331 Outline Socket programming Remote method invocation (RMI)
Advertisements

Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 TCP socket application Architecture of Client-Server Applications Java Socket Programming Client Application Server Application.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
System Programming Practical session 10 Java sockets.
Client/Server example. Server import java.io.*; import java.net.*; import java.util.*; public class PrimeServer { private ServerSocket sSoc; public static.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
1 Fall 2005 Socket Programming Qutaibah Malluhi Computer Science and Engineering Qatar University.
Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger.
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.
Practical Session 11 Multi Client-Server Java NIO.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Assignment 3 A Client/Server Application: Chatroom.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
Cli/Serv.: Chat/121 Client/Server Distributed Systems v Objectives –discuss a client/server based chat system –mention two other ways of chatting.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Lab 2C Primer I/O in Java Sockets Threads More Java Stuffs.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
VII. Sockets. 1. What is a Socket? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes.
CSC 480 Software Engineering Socket. What is Socket? A socket is one end-point of a two-way communication link between two programs running on the network.
Practical Session 11 Multi Client-Server Java NIO.
Field Trip #25 Creating a Client/Server By Keith Lynn.
CS 4244: Internet Programming Network Programming in Java 1.0.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Computer Networks & PROTOCOLS (CSI 4118) FALL 2005 Professor Robert L. Probert.
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 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
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.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Object-Orientated Analysis, Design and Programming
Stock Market Quotes – Socket Version
Assignment 3 A Client/Server Application: Chatroom
Echo Networking COMP
Threads in Java Two ways to start a thread
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
Lecture 14 Throwing Custom Exceptions
Lecture 21 Sockets 1 (Not in D&D) Date.
Block 14 Group Communication (Multicast)
Client-server Programming
Networking with Java 2.
„Networking”.
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
CSI 4118 – UNIVERSITY OF OTTAWA
CSCD 330 Network Programming
Distributed Computing
More on iterations using
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

Block 15 Developing the Calculator application Jin Sa 2018/6/15 Client-server programming

Client-server programming Outline Objectives Problem description Structural design Detailed design Implementation –version 1 Partial GUI implementation –version 2 2018/6/15 Client-server programming

Objectives Help you to do the application part of the assignment Calculator is a simplified version of last year’s assignment The Calculator application is similar to this year’s assignment. This year has an extra aspect – threads sharing the same data, i.e. the counter (not addressed in this unit, see the Thread lecture).

Problem description See handout “CalculatoSimplifed.doc”.

Structural Design – client side

Structural Design – server side Why do we want to have three classes? Rationale?

Detailed Design – Protocol class The state diagram needs revision because …

Implementation – version 1 (no GUI)

Implementation – version 1 (no GUI) Pseudo code Not addressing error detection. (you need to do error detection in your assignment. 2018/6/15 Client-server programming

Version 1 CalcServer- pseudo code public class CalcServer { main(String[] args) { ServerSocket serverSocket; boolean listening = true; serverSocket = new ServerSocket(5555); while (listening) {Socket s= serverSocket.accept(); new CalcThread(s).start();} serverSocket.close(); }

Version 1 CalcThread (1) - pseudo code public class CalcThread extends Thread { private Socket socket; public CalcThread(Socket socket) { super("CalcThread"); this.socket = socket; }

Version 1 CalcThread (2) - pseudo code public void run() { PrintWriter out = new PrintWriter(socket.getOutputStream()); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); CalcProtocol calculator = new CalcProtocol(); do { outputLine = calculator.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Bye")) { break;} inputLine = in.readLine(); } while (!input.equals(“Bye")); socket.close(); }

CalcProtocol (1) – pseudo code public class CalcProtocol { // Allowed states private int WAITING_2_START = 0; private int EXPECT_OPERATOR = 1; private int EXPECT_2_NUMBERS = 2; private char operation; private String menu = "+ or – or e for Exit"; private int state = WAITING_2_START;

CalcProtocol (2) – pseudo code public String processInput(String theInput) { String theOutput = null; switch (state) { case WAITING_2_START: theOutput = returnMenu(); break; case EXPECT_OPERATOR: theOutput = processOperator(theInput); case EXPECT_2_NUMBERS: theOutput = doOperation(theInput); } return theOutput;

CalcProtocol (3) – pseudo code private String processOperator(String s) { c = s.charAt(0); switch (c) { case '+‘ and ‘-’: operation = c; out = "Enter 2 numbers: "; state = EXPECT_2_NUMBERS; break; case 'e': out = "Bye"; default: out = "not validate operator, re-enter operator: "; } return out;

CalcProtocol (4) – pseudo code private String doOperation(String s) { n1 = sc.nextInt(); n2 = sc.nextInt(); switch (operation) { case '+': out = Integer.toString(n1 + n2); break; case '-': out = Integer.toString(n1 - n2); } state=WAITING_2_START; return "Answer = " + out;

CalcClient(1) – pseudo code public class CalcClient { public static void main(String[] args) { String hostName = "localhost"; int port = 5555; Socket socket = new Socket(hostName, port); PrintWriter out = new PrintWriter(socket.getOutputStream()); Scanner in=new Scanner(socket.getInputStream()); Scanner stdIn = new Scanner(System.in); About connect And set up the input out streams

CalcClient(2) – pseudo code About receive msg while (true) { fromServer = in.nextLine(); println("Server says: " + fromServer); if (fromServer.equals("Bye")) break; fromUser = stdIn.nextLine(); println("Client says: " + fromUser); out.println(fromUser); } // while out.close(); in.close(); stdIn.close(); socket.close(); } About send msg About disconnect

Version 2 : Adding GUI to the client No need to change the server side at all Version 1: CalcClient mixture of IO with user and communication with the server Version 2: separate IO with user from the communication with the server: CalcClient2: communicate with server CalcClientGUI: communicate with user

Version 2 : Adding GUI to the client CalcClient2: change the main into some specific methods: Connect Disconnect Send Receive CalcClientGUI: IO with user is done via a JFrame; it drives CalcClient2 2018/6/15 Client-server programming

Revised structural design on client side 2018/6/15 Client-server programming

Implementation – version 2 with GUI

CalcClient2(1) – pseudo code public class CalcClient2 { private String hostName = "localhost"; private int port = 5555; private Socket socket = null; private PrintWriter out = null; private Scanner in = null; // from server private Scanner stdIn = new Scanner(System.in); private String fromServer; private String fromUser;

CalcClient2(2) – pseudo code public void connect() { socket = new Socket(hostName, port); out = new PrintWriter(socket.getOutputStream()); in = new Scanner(socket.getInputStream()); } public void disconnect() { out.close(); in.close(); stdIn.close(); socket.close(); 2018/6/15 Client-server programming

CalcClient2(3) – pseudo code public void sendToServer(String msg) { out.println(msg); } public String receiveFromServer() { return in.nextLine(); 2018/6/15 Client-server programming

CalcClientGUI Use Netbeans Form editor to create the interface (demo) For each event, implement the appropriate event handler. In the event handler, call the corresponding methods in CalcClient2 to communicate with the serever (I.e. Linking the GUI to the client and therefore the server.)

Linking the GUI to CalcClient2 Change this: private void connectButtonActionPerformed( java.awt.event.ActionEvent evt) { // TODO add your handling code here: } To: client.connect(); String msg = client.receiveFromServer(); serverTextArea.setText(msg); 2018/6/15 Client-server programming