Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.

Slides:



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

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
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.
Voice Instant Messenger Andrew Miller CS 491B Fall 2006 Professor Sun.
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 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Java Networking -- Socket Server socket class: ServerSocket wait for requests from clients. after a request is received, a client socket is generated.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
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 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.
Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Web Proxy Server. Proxy Server Introduction Returns status and error messages. Handles http CGI requests. –For more information about CGI please refer.
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.
Socket Programming in Java -First Step of Network Programming-
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
CSE 219 Computer Science III Network Programming (Application Layer) Reference:
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
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 Streams Files are a computer’s long term memory Need ability for programs to –get information from files –copy information from program variables to.
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.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
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.
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.
Java Sockets Tutorial Rahul Malik Nov 12, 2005.
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.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Field Trip #25 Creating a Client/Server By Keith Lynn.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
Socket Programming in Java -First Step of Network Programming-
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Messaging and Networking. Objectives Send SMS messages using the Messaging app Send SMS messages programmatically from within your app Receive and consume.
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.
Java 13. Networking public class SumTest {
The Chat Problem.
Echo Networking COMP
Lecture 21 Sockets 1 (Not in D&D) Date.
Socket Programming Cal Poly Pomona Young CS380.
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Java Tutorial Zhe Li.
EE 422C Socket Programming
Software Engineering for Internet Applications
Flight Simulation Client/Server
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Li Tak Sing COMPS311F

Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the server side to handle a client ChatClient //the client The source can be obtained at:

ChatServer The ChatServer class is the main program for the chat server. It will create a ServerSocket at port For each of the incoming requests, a ChatThread will be created to entertain the request. It is a subclass of JFrame. The window is used to display information regarding the connected clients and messages passed.

ChatServer The upper textfields will show the connected clients. The central textarea will show the messages from the clients.

Attributes of ChatServer Vector threads; //connected clients JTextField clientText; //for displaying the connected clients JTextArea text; //for displaying the messages from the clients

Methods of ChatServer synchronized public void addThread(ChatThread th); //this method adds the ChatThread to threads. synchronized public void removeThread(ChatThread th) ; //this method removes the ChatThread from threads. synchronized public void broadcast(String st); //this method is used to send a message to all connected clients.

ChatThread It is created to entertain an individual request. It is a subclass of Thread. When a ChatThread is created, it will be added to threads of ChatServer by using the addThread method of ChatServer. The run method of ChatThread will continuously read from the corresponding client. When a message is read from the client, it will be broadcast to all connected clients through the broadcast method of ChatServer. It also provide a sendMessage method for sending a message to the corresponding client.

Attributes of ChatThread String name; //name of the client Socket socket; //socket of the connection between the server and the client. ChatServer server; //the ChatServre DataInputStream input; //the input stream for the connection with the client. DataOutputStream output; //the output stream

Constructor of ChatThread public ChatThread(java.net.Socket s, ChatServer server) This construct allows ChatThread to record the socket and the server. The socket is used to create the data streams. The server is used to broadcast a message.

Methods of ChatThread public synchronized void sendMessage(String st) throws IOException ; //this method is used to send a message to the client public void run(); //this will continuously read from the client. After reading a message, it will invoke the server's broadcast method to send the message to all connected clients.

ChatClient It is the chat client. It is a subclass of JFrame. The top panel contains a textfield for the user to type in a name. This will be the id of the client in the chat system. The central textarea is used to display all messages from clients. The bottom panel contains a textfield for the user to type in a message.

Attributes of ChatClient JTextField name; //This allows the user to type in name JTextArea messages; //This is used to display messages from all connected clients. JTextField text; //This allows the user to type in message to be sent to all clients. DataInputStream input; //This is the input stream for the connection. DataOutputStream output; //The output stream.

Constructor of ChatClient public ChatClient() ; //the constructor does all things. It setups the window. All listeners are created using anonymous classes. A thread is created to continuously read from the server. When a message is read, it will be appended to the central textarea, messages.

Implementation of constructor of ChatServer... try { java.net.ServerSocket ss = new java.net.ServerSocket(12345); while (true) { java.net.Socket s = ss.accept(); ChatThread thread = new ChatThread(s, this); } } catch (Exception e) { e.printStackTrace(); }...

Implementation of methods of ChatServer synchronized public void addThread(ChatThread th) { threads.add(th); String st = ""; for (ChatThread thread:threads) { st+=thread.name()+" "; } clientText.setText(st); }

Implementation of methods of ChatServer synchronized public void removeThread(ChatThread th) { threads.remove(th); String st=""; for (ChatThread thread:threads) { st+=thread.name()+" "; } clientText.setText(st); }

Implementation of methods of ChatServer synchronized public void broadcast(String st) { text.append(st); java.util.Vector removedThreads = new java.util.Vector (); for (ChatThread th : threads) { try { th.sendMessage(st); } catch (Exception e) { removedThreads.add(th); } for (ChatThread th : removedThreads) { removeThread(th); }

Implementation of constructor of ChatThread public ChatThread(java.net.Socket s, ChatServer server) { socket=s; this.server=server; try { output=new java.io.DataOutputStream(socket.getOutputStream()); input=new java.io.DataInputStream(socket.getInputStream()); name=input.readUTF(); server.addThread(this); start(); } catch (Exception e) { e.printStackTrace(); }

Implementation of methods of ChatThread public synchronized void sendMessage(String st) throws IOException { output.writeUTF(st); }

Implementation of methods of ChatThread public void run() { try { while (true) { String st=input.readUTF(); server.broadcast(name+":"+st); } catch (Exception e) { server.removeThread(this); }

The ActionLister of the connect button public void actionPerformed(ActionEvent e) { if (button.getText().equals("connect")) { try { s = new java.net.Socket(" ", 12345); input = new java.io.DataInputStream(s.getInputStream()); output = new java.io.DataOutputStream(s.getOutputStream()); output.writeUTF(name.getText()); messages.append("connected\n"); button.setText("disconnect");

The ActionLister of the connect button new Thread() { public void run() { try { while (true) { String st = input.readUTF(); messages.append(st); } } catch (Exception e) { messages.append("Disconnected\n"); button.setText("connect"); } }.start();

The ActionLister of the connect button } catch (Exception ee) { button.setText("connect"); messages.append("disconnected"); } } else { try { input.close(); output.close(); s.close(); button.setText("connect"); messages.append("disconnected"); } catch (Exception ee) { } });