School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

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.
System Programming Practical session 10 Java sockets.
Java sockets. From waiting.
System Programming Practical session 11 Multiple clients server Non-Blocking I/O.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 First app – simplechat1 Reminder: what’s the point?  To build on ocsf framework.  Simple requirements: echo all messages to all clients.  Use only.
19-Aug-15 About the Chat program. 2 Constraints You can't have two programs (or two copies of the same program) listen to the same port on the same machine.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 21.
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
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
SWE 622- Distributed Systems Project Phase III Eric Barnes, David Chang, David Nelson Fisayo Oluwadiya, Xiang Shen.
1 Rick Mercer Multi Threaded Chat Server. 2 Client – Server with Socket Connections We've seen how to establish a connection with 1 client Review a simple.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Java Server Programming Web Interface for Java Programs.
Part 4: Network Applications Client-server interaction, example applications.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
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.
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, VUW IRC, parsing, protocols COMP # 25.
Sockets A popular API for client-server interaction.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Echo Networking COMP
Threads in Java Two ways to start a thread
Lecture 21 Sockets 1 (Not in D&D) Date.
Block 15 Developing the Calculator application
Networking COMP
Multithreaded Programming in Java
Android Topics UI Thread and Limited processing resources
Why Threads Are A Bad Idea (for most purposes)
Distributed Computing
Multiplexing/Demux.
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
CS18000: Problem Solving and Object-Oriented Programming
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP

COMP112 23: 2 Menu The execution of a program is called a process Modern computers are multi-core but each core can time- slice and hence fake concurrent execution Concurrent processes Some times processes do not want to wait it would be easier if a process could do two things at once Multi threaded programs allow more than one thread of execution each thread running the same program and shareing the same memory. Admin

COMP112 23: 3 Concurrency Computers appear to execute two programs at once A process has state and a thread of execution Two processes have disjoint state One processes can have multiple threads of execution State Each thread shares the same state as all other threads of the same process This can introduce very subtle errors! Process Thread

COMP112 23: 4 Deadlock Two processes need to communicate via a protocol Process A receives and only then sends Process B sends and only then waits to receive If both wait to receive a message at the same time nothing will happen, ever. Deadlock A B Send Receive A B Send

COMP112 23: 5 Echo Server Echo has one process for the Server and one for each Client The Server may have lots of Clients Needs to process each Client with no apparent delay Server canot wait for Client A to respond else Client B may have to wait utill after A reponds A simple design solution is to have many servers or many threads – one per Client. This allows the time slicing built into computers to ensure that no Client waits Basic Server design: Listen to the port. Start a new thread for each client

COMP112 23: 6 Concurret Echo server Client A Server Server and Client are distinct processes Distinct clients are distinct processes How do you prevent one client having to wait because the sever is dealing with another client One answer is different server threads for each client Client B

COMP112 23: 7 Concurrent Echo Server Design 1.Main Program Listen to the port. Loop Wait for a new client to try to connect create a new socket for the client connection start a thread to process the client 2.Thread processing the client Loop listen for input from client echo it back quit if the message was QUIT

COMP112 23: 8 Echo Server: Listening for clients public class EchoServer{ public static final int PORT = 6667; // The port the server will listen on public static void main(String[] args) { try{ ServerSocket serverSocket = new ServerSocket(PORT); System.out.println("Waiting for clients to connect..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("Found client"); EchoService echoService= new EchoService(socket); new Thread(echoService).start(); } }catch(IOException e){System.out.println("Failed to connect" + e);} } new thread wait for a client Executed when calling EchoServer Executes constructor Executes the run method

COMP112 23: 9 Echo Server: per client class EchoService implements Runnable { private Socket socket; private Scanner clientIn; private PrintStream clientOut; public EchoService(Socket socket) { this.socket = socket; try { clientIn = new Scanner(socket.getInputStream()); clientOut = new PrintStream(socket.getOutputStream()); } catch (IOException e) {System.out.println(“Connection failed." + e);} }

COMP112 23: 10 Echo Server: per client public void run() { while (clientIn.hasNext()) { String message = clientIn.nextLine(); System.out.println("Received: "+ message); if (message.equals("QUIT")) { break;} clientOut.println("ECHO: "+ message); clientOut.flush(); } try{ socket.close();} catch (IOException e) {System.out.println("Error socket close" + e);} System.out.println("Client disconnected."); } Executed in separate thread

COMP112 23: 11 Synchronous or Asynchronous? Two different echo clients are asynchronous Each echo client: While(true) get message from user send message to server get reply from server display reply IRC client: messages may come from the server and displayed to the user messages may come from the user and sent to the server  client must be concurrently listening to user and server  each client must have two threads! Each client is Synchronous: one thread sending and receiving. Fixed cycle of interaction Each client is Asynchronous: one thread sends user input to server another receives data from server and displays. Interaction not fixed

COMP112 23: 12 Asynchronous client public class AsynchClient { private static final String SERVER = "localhost"; private static final int PORT = 6667; private Socket socket; private Scanner input; private PrintStream output; public static void main(String[] args) { new AsynchClient(); } public AsynchClient(){ try { socket = new Socket(SERVER, PORT); new Thread(new Runnable(){ public void run(){ listenToServer(); }}).start(); sendToServer(); }catch(IOException e){UI.println("IO failure "+ e);} }

COMP112 23: 13 Asynchronous client public void sendToServer(){ PrintStream output = new PrintStream(socket.getOutputStream()); while (true) { String toSend = UI.askString(">"); output.println(toSend); output.flush(); } public void listenToServer(){ Scanner input = new Scanner(socket.getInputStream()); while (input.hasNext()){ String line = input.nextLine(); UI.println("SERVER: "+ line); }

COMP112 23: 14 Threads What is a thread? Like a separate CPU running a program (or part of a program). independent of all the other threads (could be faster or slower). Java threads all have access to the same memory Two threads accessing the same location can cause conflict and error Safe Programming with threads is HARD. Harder to debug. No problems if the different threads don’t share any resources You should expect some odd things to happen if the threads do share resources (eg, the same window!)

COMP112 23: 15 How do you get a Thread? The main method is called with one thread anything it calls is executed in that thread: main  constructor  sendToServer The GUI events are executed in a separate thread called by the Java language not you, the application programmer. repainting, responding to mouse, buttons etc. You can create a new Thread object and call run() on it.

COMP112 23: 16 IRC login Login sequence:  NICK pondy  USER pondy 0 unused :Peter Andreae  …lots of messages… including grumbling about looking up hostname  :irc.ecs.vuw.ac.nz.net 001 pondy …….  :irc.ecs.vuw.ac.nz.net 002 pondy …….  :irc.ecs.vuw.ac.nz.net 003 pondy …….  :irc.ecs.vuw.ac.nz.net 004 pondy irc.ecs.vuw.ac.nz hybrid …. or  :irc.ecs.vuw.ac.nz.net 433 * pondy :Nickname is already in use