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.

Slides:



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

Prepared By: Eng. Ola M. Abd El-Latif May/2010.  In this Lab we will answer the most frequently asked questions about programming sockets in Java. 
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
CS 491, 591 Networking in Java Summer Establishing a Simple Server (Using Stream Sockets) Creating a Java server –Create ServerSocket object ServerSocket.
1 L55 Networking (4). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
1 Programming Section 9 James King 12 August 2003.
System Programming Practical session 10 Java sockets.
A Chat Server DBI – Representation and Management of Data on the Internet.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
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.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 11.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
1 Web Based Programming Section 8 James King 12 August 2003.
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.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Networks and Client/Server Applications Handling Multiple Clients Concurrently.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
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.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
Networking OSI (Open Systems Interconnection) model of computer networking, seven layers (the Application, Presentation, Session, Transport, Network, Data.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
The Echo Server Problem. Contents  Basic Networking Concepts  The Echo Server Problem.
Java Thread Programming
Object-Orientated Analysis, Design and Programming
Echo Networking COMP
Threads in Java Two ways to start a thread
Networks and Client/Server Applications
Networking COMP
Client-server Programming
Socket Programming Cal Poly Pomona Young CS380.
CH5 TCP Client - Server Example:
Networking with Java 2.
Client-Server Interaction
Client server programming
Threads II IS
EE 422C Socket Programming
Clients and Servers 19-Nov-18.
Networks and Client/Server Applications
Clients and Servers 1-Dec-18.
Object Serialization Explanation + Example of file + network
Software Engineering for Internet Applications
CSCD 330 Network Programming
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:

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 client /server connection next 2 slides ServerSocket serverSocket = new ServerSocket(4000); System. out.println("This server now awaits one client"); Socket client = serverSocket.accept(); ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream()); ObjectInputStream input = new ObjectInputStream(client.getInputStream()); String clientInput = (String) input.readObject(); System. out. println("Client wrote: " + clientInput); output.writeObject("This server is shutting down."); client.close();

3 Then run the client Socket server = new Socket("localhost", 4000); ObjectOutputStream output = new ObjectOutputStream(server.getOutputStream()); ObjectInputStream input = new ObjectInputStream(server.getInputStream()); output.writeObject("I am a client"); String responseToMyOutput = (String) input.readObject(); System. out.println("Server wrote: " + responseToMyOutput); server.close();

4 Practice test question What is the output on the computer running the Server? What is the output on the computer running the Client?

5 One Client at a time Server could listen for many clients with an infinite loop while(true) { /* do IO with each client */ // Server code ServerSocket serverSocket = new ServerSocket(4000); while ( true ) { Socket client = serverSocket.accept(); ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream()); output.writeObject("Please send money"); } But they all disappear after connecting as this program terminates The server is still running

6 Build a Chat Server, Client First Need the client to check for server output without interrupting the GUI interaction No "frozen" GUI please Can type new messages and append incoming messages At the same time server writes this 6

7 Review Threads The JVM starts your main (and other threads) thread We can start new stacks by calling run on a new Thread object Using threads can make it appear we are doing things simultaneously type into JTextField at the same time Lines , ActionPerformed read input from server Lines , the loop in IncomingReader run However, when your Java program has the processor, the threads take turns using the processor 7

8 Job: loop to read from server The client needs something behind scenes to read read input from server The code that reads input from the server has to run in a new Thread Have to give the Thread--the worker--a runnable, which is the job the worker is supposed to do Lines

9 Chat Server Main thread has a loop to accept new clients Each time a new client is accepted, add its outputStream to an ArrayList Also construct a ClientHandler that is all set up in a new thread This thread will wait for subsequent input in a loop The main thread is waiting for other connections When any client writes to the server, the loop gets the message Line 51 and tells everyone sdf 9

10 Keeping track of many concurrent clients for some time The server uses a separate thread for each client Each thread can wait to read from that client Code demo