Java How to Program, 9/e © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved.

Slides:



Advertisements
Similar presentations
CCNA – Network Fundamentals
Advertisements

Network Programming Chapter 11 Lecture 6. Networks.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
© 2007 Cisco Systems, Inc. All rights reserved.Cisco Public 1 Version 4.0 OSI Transport Layer Network Fundamentals – Chapter 4.
1 L54 Networking (3). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
Socket Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
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 L52 Networking (1). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 L55 Networking (4). 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.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
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.
© 2007 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.1 Computer Networks and Internets with Internet Applications, 4e By Douglas.
© 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 java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Process-to-Process Delivery:
INTRODUCTION TO WEB DATABASE PROGRAMMING
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Computer Networks  Network - A system of computers interconnected in order to share information.  Data transmission - consists of sending and receiving.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
1 CSC111H Client-Server: An Introduction Dennis Burford
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
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.
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.
Internet Applications (Cont’d) Basic Internet Applications – World Wide Web (WWW) Browser Architecture Static Documents Dynamic Documents Active Documents.
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.
Java Programming II Java Network (I) Java Programming II.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
McGraw-Hill Chapter 23 Process-to-Process Delivery: UDP, TCP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Java’s networking capabilities are declared by the classes and interfaces of package java.net, through which Java offers stream-based communications that.
1 Network Communications A Brief Introduction. 2 Network Communications.
Advance Computer Programming Networking Basics – explores the java.net package which provides support for networking. – Also Called “programming for the.
Network Programming. These days almost all devices.
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
MCA – 405 Elective –I (A) Java Programming & Technology
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
24 Networking.
Network Programming Introduction
Process-to-Process Delivery:
Process-to-Process Delivery: UDP, TCP
Presentation transcript:

Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.

 Java provides a number of built-in networking capabilities that make it easy to develop Internet-based and web-based applications.  Java’s fundamental networking capabilities are declared by the classes and interfaces of package java.net  Stream-based communications that enable applications to view networking as streams of data.  Packet-based communications for transmitting individual packets of information—commonly used to transmit data images, audio and video over the Internet.  We focus on both sides of the client/server relationship.  The client requests that some action be performed, and the server performs the action and responds to the client. ◦ A common implementation of the request-response model is between web browsers and web servers. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Socket-based communications enable applications to view networking as if it were file I/O. ◦ The socket is simply a software construct that represents one endpoint of a connection.  With stream sockets, a process establishes a connection to another process.  While the connection is in place, data flows between the processes in continuous streams.  Stream sockets are said to provide a connection-oriented service.  The protocol used for transmission is the popular TCP (Transmission Control Protocol). © Copyright by Pearson Education, Inc. All Rights Reserved.

 With datagram sockets, individual packets of information are transmitted.  UDP—the User Datagram Protocol—is a connectionless service, and thus does not guarantee that packets arrive in any particular order. ◦ Packets can even be lost or duplicated.  UDP is most appropriate for network applications that do not require the error checking and reliability of TCP. © Copyright by Pearson Education, Inc. All Rights Reserved.

 HyperText Transfer Protocol (HTTP) forms the basis of the World Wide Web ◦ uses URIs (Uniform Resource Identifiers) to identify data on the Internet.  URIs that specify the locations of documents are called URLs (Uniform Resource Locators).  AppletContext method showDocument causes the browser to access and display a resource. © Copyright by Pearson Education, Inc. All Rights Reserved.

 This applet takes advantage of applet parameters specified in the HTML document that invokes the applet.  The HTML document contains eight applet parameters specified with the param element—these lines must appear between the starting and ending applet tags.  The applet can read these values and use them to customize itself.  Any number of param elements can appear between the starting and ending applet tags.  Each parameter has a unique name and a value.  Applet method getParameter returns the value associated with a specific parameter name as a String. ◦ Argument is a String containing the name of the parameter in the param element. © Copyright by Pearson Education, Inc. All Rights Reserved.

 The URL constructor determines whether its argument represents a properly constructed URL. ◦ otherwise, throws a Malformed-URLException.  Applet method getAppletContext gets a reference to an AppletContext object that represents the applet container. ◦ Used to invoke method showDocument to change web pages.  A second version of AppletContext method showDocument enables an applet to specify the so-called target frame in which to display the web resource.  Target frames: ◦ _blank results in a new web browser window. ◦ _self displays content in the same frame as the applet. ◦ _top removes the current frames in the browser window, then displays the content in the current window. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Swing GUI component JEditorPane (from package javax.swing ) can display the contents of a file on a web server.  Class JEditorPane can render both plain text and XHTML-formatted text ◦ this application acts as a simple web browser.  Can process HyperlinkEvent s when the user clicks a hyperlink in the JEditorPane. © Copyright by Pearson Education, Inc. All Rights Reserved.

 JEditorPane method setPage downloads a document and displays it. ◦ If there is an error, throws an IOException. ◦ if an invalid URL, throws a MalformedURLException.  If a JEditorPane contains an XHTML document and the user clicks a hyperlink, the JEditorPane generates a HyperlinkEvent (package javax.swing.event ) and notifies all registered HyperlinkListener s (package javax.swing.event ) of that event.  When a HyperlinkEvent occurs, the program calls method hyperlinkUpdate on the registered listener(s). © Copyright by Pearson Education, Inc. All Rights Reserved.

 HyperlinkEvent method getEventType returns the type of the HyperlinkEvent.  HyperlinkEvent nested class EventType declares three static EventType objects ◦ ACTIVATED indicates that the user clicked a hyperlink to change web pages ◦ ENTERED indicates that the user moved the mouse over a hyperlink ◦ EXITED indicates that the user moved the mouse away from a hyperlink  HyperlinkEvent method getURL obtains the URL represented by the hyperlink. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Establishing a simple server in Java requires five steps.  Step 1 is to create a ServerSocket object.  ServerSocket constructor  ServerSocket server = new ServerSocket( portNumber, queueLength );  The constructor establishes the port number where the server waits for connections from clients—a process known as binding the server to the port.  Each client will ask to connect to the server on this port. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Programs manage each client connection with a Socket object.  In Step 2, the server listens indefinitely (or blocks) for an attempt by a client to connect.  To listen for a client connection, the program calls ServerSocket method accept, as in  Socket connection = server.accept(); ◦ returns a Socket when a connection with a client is established.  The Socket allows the server to interact with the client. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Step 3 is to get the OutputStream and InputStream objects that enable the server to communicate with the client by sending and receiving bytes. ◦ The server invokes method getOutputStream on the Socket to get a reference to the Socket ’s OutputStream and invokes method getInputStream on the Socket to get a reference to the Socket ’s InputStream.  Step 4 is the processing phase, in which the server and the client communicate via the OutputStream and InputStream objects.  In Step 5, when the transmission is complete, the server closes the connection by invoking the close method on the streams and on the Socket. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Establishing a simple client in Java requires four steps.  In Step 1, the Socket constructor establishes a connection to the server.  Socket connection = new Socket( serverAddress, port ); ◦ If the connection attempt is successful, this statement returns a Socket. ◦ A connection attempt that fails throws an instance of a subclass of IOException. ◦ An UnknownHostException occurs when the system is unable to resolve the server name. © Copyright by Pearson Education, Inc. All Rights Reserved.

 In Step 2, the client uses Socket methods getInputStream and getOutputStream to obtain references to the Socket ’s InputStream and OutputStream.  Step 3 is the processing phase in which the client and the server communicate via the InputStream and OutputStream objects.  In Step 4, the client closes the connection when the transmission is complete by invoking the close method on the streams and on the Socket. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Figures 27.5 and 27.7 present a simple client-/server chat application. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Method runServer sets up the server to receive a connection and processes one connection at a time.  Methods displayMessage uses the event-dispatch thread to display messages in the application’s JTextArea.  SwingUtilities method invokeLater receives a Runnable object as its argument and places it into the event-dispatch thread for execution. ◦ Ensures that we don’t modify a GUI component from a thread other than the event-dispatch thread. ◦ We use a similar technique in method setTextFieldEditable. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Socket method getInet-Address returns an InetAddress (package java.net ) containing information about the client computer.  InetAddress method getHostName returns the host name of the client computer.  IP address ( ) and host name ( localhost ) are useful for testing networking applications on your local computer ◦ known as the loopback address © Copyright by Pearson Education, Inc. All Rights Reserved.

 Note the call to ObjectOutputStream method flush. ◦ Causes the ObjectOutputStream on the server to send a stream header to the corresponding client’s ObjectInputStream. ◦ The stream header contains such information as the version of object serialization being used to send objects. ◦ This information is required by the ObjectInputStream so that it can prepare to receive those objects correctly. © Copyright by Pearson Education, Inc. All Rights Reserved.

 In this example, you can execute the client from any computer on the Internet and specify the IP address or host name of the server computer as a command-line argument to the program.  The following command attempts to connect to the Server on the computer with IP address  java Client © Copyright by Pearson Education, Inc. All Rights Reserved.

 InetAddress static method getByName returns an InetAddress object containing the specified IP address ◦ Method getByName can receive a String containing either the actual IP address or the host name of the server.  The first argument also could have been written other ways.  For the localhost address , the first argument could be specified with either of the following expressions:  InetAddress.getByName( "localhost" ) InetAddress.getLocalHost() © Copyright by Pearson Education, Inc. All Rights Reserved.

 Connectionless transmission with datagrams.  Connection-oriented transmission is like the telephone system ◦ You dial and are given a connection to the telephone of the person with whom you wish to communicate. ◦ The connection is maintained for your phone call, even when you’re not talking.  Connectionless transmission with datagrams is more like the way mail is carried via the postal service. ◦ If a large message will not fit in one envelope, you break it into separate pieces that you place in sequentially numbered envelopes. ◦ All of the letters are then mailed at once. ◦ The letters could arrive in order, out of order or not at all (the last case is rare). © Copyright by Pearson Education, Inc. All Rights Reserved.

 Class Server declares two DatagramPacket s that the server uses to send and receive information and one DatagramSocket that sends and receives the packets.  The DatagramSocket constructor that takes an integer port-number argument binds the server to a port where it can receive packets from clients. ◦ Client s sending packets to this Server specify the same port number in the packets they send. ◦ A Socket-Exception is thrown if the DatagramSocket constructor fails to bind the DatagramSocket to the specified port. © Copyright by Pearson Education, Inc. All Rights Reserved.

 DatagramSocket method receive waits for a packet to arrive at the Server. ◦ Blocks until a packet arrives, then stores the packet in its DatagramPacket argument. ◦ The method throws an IOException if an error occurs while receiving a packet.  DatagramPacket method getAddress returns an InetAddress object containing the IP address of the computer from which the packet was sent.  Method getPort returns an integer specifying the port number through which the client computer sent the packet.  Method getLength returns an integer representing the number of bytes of data received.  Method getData returns a byte array containing the data.  DatagramSocket method send throws an IOException if an error occurs while sending a packet. © Copyright by Pearson Education, Inc. All Rights Reserved.

 Tic-Tac-Toe implemented by using client/server techniques with stream sockets.  Sample outputs are shown in Fig  As the TicTacToeServer receives each client connection, it creates an instance of inner-class Player to process the client in a separate thread.  These threads enable the clients to play the game independently.  The first client to connect to the server is player X and the second is player O.  Player X makes the first move.  The server maintains the information about the board so it can determine if a player’s move is valid. © Copyright by Pearson Education, Inc. All Rights Reserved.

 When a client connects, a new Player object is created to manage the connection as a separate thread, and the thread is executed in the runGame thread pool.  The Player constructor receives the Socket object representing the connection to the client and gets the associated input and output streams.  The Player ’s run method controls the information that is sent to and received from the client. © Copyright by Pearson Education, Inc. All Rights Reserved.