1 L55 Networking (4). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.

Slides:



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

1 Chapter 9 Network Programming. 2 Overview Java has rich class libraries to support network programming at various levels. There are standard classes.
COEN 445 Communication Networks and Protocols Lab 4
Network Programming Chapter 11 Lecture 6. Networks.
Voice Instant Messenger Andrew Miller CS 491B Fall 2006 Professor Sun.
1 L54 Networking (3). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Advanced Java Class Network Programming. Network Protocols Overview Levels of Abstraction –HTTP protocol: spoken by Web Servers and Web Clients –TCP/IP:
Socket Programming.
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.
1 L52 Networking (1). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
 Pearson Education, Inc. All rights reserved. 1 CH24 Networking : OBJECTIVES In this chapter you will learn:  To understand Java networking.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Programming Section 9 James King 12 August 2003.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
System Programming Practical session 10 Java sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
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.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
 Pearson Education, Inc. All rights reserved Networking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Datagram Programming A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
Networks and Client/Server Applications. Basics of Client/Server One host computer can have several servers Several clients can connect to a server Client.
Julia Ljunbjörk and Anita Mugenyi. What is a socket? Like a house Between the layers.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Assignment 3 A Client/Server Application: Chatroom.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
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.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 3: Basing Software Development on Reusable Technology.
© 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.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
 2005 Pearson Education, Inc. All rights reserved Networking.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.
Practical Session 12 Reactor Pattern. Disadvantages of Thread per Client It's wasteful – Creating a new Thread is relatively expensive. – Each thread.
Field Trip #25 Creating a Client/Server By Keith Lynn.
Part 4: Network Applications Client-server interaction, example applications.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Networked Graphics Building Networked Virtual Environments and Networked Games Chapter 2: One on one (101)
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
A Local Area Network Chat Client ITTC LAN CHAT John Vincent Cecogo Jerikho Daguno Ardee Santos Elaine Mendoza Anjomar Pat Del Mindo Philip John Sales Philip.
Li Tak Sing COMPS311F. Case study: a multithreaded chat server The source contains 3 files: ChatServer //the chat server ChatThread //the thread on the.
1 Network Communications A Brief Introduction. 2 Network Communications.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
Network Programming. These days almost all devices.
Distributed Systems1 Socket API  The socket API is an Interprocess Communication (IPC) programming interface originally provided as part of the Berkeley.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Object-Orientated Analysis, Design and Programming
Block 14 Group Communication (Multicast)
24 Networking.
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
NETWORK PROGRAMMING CNET 441
Socket Programming 2: Application Layer.
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Presentation transcript:

1 L55 Networking (4)

2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java networking applications by using sockets and datagrams.  To understand how to implement Java clients and servers that communicate with one another.  To understand how to implement network-based collaborative applications.  To construct a multithreaded server.

Case Study: DeitelMessenger Server and Client Chat rooms – Each user can post a message and read all other messages – Multicast Send packets to groups of clients

DeitelMessengerServer and Supporting Classes DeitelMessengerServer – Online chat system – Classes: DeitelMessengerServer – Clients connect to this server Interface SocketMessengerConstants – Defines constants for port numbers Interface MessageListener – Defines method for receiving new chat messages Class MessageReceiver – Separate thread listens for messages from clients Class MulticastSender – Separate thread delivers outgoing messages to clients

5 Outline DeiterMessenger Server.java (1 of 3) Line 15 Lines Implement the MessageListener interface Create a ServerSocket to accept incoming network connections

6 Outline DeiterMessenger Server.java (2 of 3) Line 38 Lines Invoke method accept to wait for and accept a new client connection Create and start a new MessageReceiver for the client

7 Outline DeiterMessenger Server.java (3 of 3) Lines Create and start new MulticastSender to deliver completeMessage to all clients

8 Outline DeitelMessenger ServerTest.java Program output

9 Outline SocketMessenger Constants.java Line 9 Line 12 Line 15 Line 18 Line 21 Line 24 Line 27 Address to send multicast datagrams Port listening for multicast datagrams Port for sending multicast datagrams Port for socket connections to server String that indicates disconnect String that separates user name and message Maximum message size in bytes

10 Outline MessageListener.java Line 9 Method messageReceived allows an implementing class to receive chat messages

11 Outline MessageReceiver.java (1 of 5)

12 Outline MessageReceiver.java (2 of 5) Line 31 Attempt to read for five seconds

13 Outline MessageReceiver.java (3 of 5) Line 53 Line 55 Lines Read line of data from client A SocketTimeOutException is thrown if the read times out Separate message into two tokens delimited by Message_SEPARATOR

14 Outline MessageReceiver.java (4 of 5) Lines Lines Invoke method messageReceived of interface MessageListener to deliver the new message to the registered MessageListener Determine whether message indicates that user wishes to leave chat room

15 Outline MessageReceiver.java (5 of 5)

16 Outline MulticastSender.java (1 of 2) Lines Create DatagramSocket for delivering DatagramPacket s via multicast

17 Outline MulticastSender.java (2 of 2) Line 31 Lines Line 37 Line 38 Create an InetAddress object for the multicast address Create the Datagrampacket containing the message Send the packet with DatagramSocket method send Close the DatagramSocket, and the run method returns, terminating the MulticastSender

DeitelMessengerServer and Supporting Classes Execute DeitelMessengerServerTest – Change directories to the proper location – Type command java com.deitel.mesenger.sockets.server.DeitelMessengerServerTest

DeitelMessenger Client and Supporting Classes DeitelMessengerServer client – Consists several components Interface MessageManager Class that implements interface MessageManager – Manages communication with server Runnable subclass – Listens for messages at server’s multicast address Another Runnable subclass – Sends messages from client to server JFrame subclass – Provides client GUI

20 Outline MessageManager.java Line 10 Line 14 Line 17 Connects MessageManager to DeitelMessengerServer and routes incoming messages to appropriate MessageListener Disconnects MessageManager from DeitelMessengerServer and stops delivering messages to MessageListener Sends new message to DeitelMessengerServer

21 Outline SocketMessage Manager.java (1 of 4) Line 20 Line 22 Socket for connecting and sending messages to DeitelMessengerServer Runnable listens for incoming messages

22 Outline SocketMessage Manager.java (2 of 4) Lines Line 44 Line 45 Create Socket to communicate with DeitelMessengerServer Create a new packetReceiver, which listens for incoming multicast messages from the server Execute the Runnable

23 Outline SocketMessage Manager.java (3 of 4) Lines Line 65 Line 66 Create a new MessageSender to send DISCONNECT_STRING to the server Start the MessageSender to deliver the message using method submit of ExecutorService Invoke Future method get to wait for the disconnect message to be delivered and the Runnable to terminate

24 Outline SocketMessage Manager.java (4 of 4) Lines Create and start a new MessageSender to deliver the new message in a separate thread of execution

25 Outline MessageSender.java (1 of 2)

26 Outline MessageSender.java (2 of 2) Lines Line 31 Line 32 Create a new Formatter for the clientSocket ’s OutputStream Invoke Formatter method format to format the message Invoke Formatter method flush to ensure that the message is sent immediately

27 Outline PacketReceiver.java (1 of 4)

28 Outline PacketReceiver.java (2 of 4) Lines Line 34 Line 37 Line 40 Line 55 MulticastSocket listens for incoming chat messages on port MULTICAST_LISTENING_PORT InetAddress object to which DeitelMessengerServer multicasts chat messages Register MulticastSocket to receive messages sent to MULTICAST_ADDRESS Invoke MulticastSocket method setSoTimeout to specify that if no data is received in 5000 milliseconds, the MulticastSocket should issue an InterruptedIOException Create byte array for storing DatagramPacket

29 Outline PacketReceiver.java (3 of 4) Lines Line 63 Line 78 Lines Create DatagramPacket for storing message Receive incoming packet from multicast address Invoke method trim of class String to remove extra white spaces from the end of the message Create a StringTokenizer to separate the message body from the name of the user who sent the message

30 Outline PacketReceiver.java (4 of 4) Lines Line 97 Line 98 After parsing message, deliver message to PacketReceiver ’s MessageListener Invoke MulicastSocket method leaveGroup to stop receiving messages from the multicast address Invoke MulticastSocket method close to close the MulticastSocket

31 Outline ClientGUI.java (1 of 10)

32 Outline ClientGUI.java (2 of 10) Line 40 Line 41 MessageManager handles communication with chat server MessageListener receives incoming messages from MessageManager

33 Outline ClientGUI.java (3 of 10) Line 51 Create an instance of MyMessageListener, which implements interface MessageListener

34 Outline ClientGUI.java (4 of 10)

35 Outline ClientGUI.java (5 of 10) Lines Send user’s name and inputArea ’s text to DeitelMessengerServer as a chat message

36 Outline ClientGUI.java (6 of 10) Line 155 Disconnect from chat server when user exits client application

37 Outline ClientGUI.java (7 of 10) Line 169 Lines When user accesses Connect menu, connect to chat server Prompt the user for a user name

38 Outline ClientGUI.java (8 of 10) Line 195 Invoke MessageManager method disconnect to disconnect from chat server

39 Outline ClientGUI.java (9 of 10) Lines Display message when MessageListener detects that message was received

40 Outline ClientGUI.java (10 of 10) Line 237 Append the user name, “> ” and messageBody to messageArea

41 Outline DeitelMessenger.java (1 of 3) Line 17 Line 20 Line 23 Create a client to connect to the localhost Connect to a host supplied by the user Create a ClientGUI for the MessageManager

42 Outline DeitelMessenger.java (2 of 3)

43 Outline DeitelMessenger.java (3 of 3)

DeitelMessenger Client and Supporting Classes Execute DeitelMessenger client application – Change directories to the proper location – Type command java com.deitel.messenger.sockets.client.DeitelMessenger java com.deitel.messenger.sockets.client.DeitelMessenger localhost java com.deitel.messenger.sockets.client.DeitelMessenger