 1992-2007 Pearson Education, Inc. All rights reserved. 1 CH24 Networking : OBJECTIVES In this chapter you will learn:  To understand Java networking.

Slides:



Advertisements
Similar presentations
COEN 445 Communication Networks and Protocols Lab 4
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.
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.
CSCI 4550/8556 Computer Networks Comer, Chapter 3: Network Programming and Applications.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Programming Section 9 James King 12 August 2003.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
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.
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
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.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
© 2007 Cisco Systems, Inc. All rights reserved.Cisco Public 1 Version 4.0 OSI Transport Layer Network Fundamentals – Chapter 4.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
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.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
© 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.
ECEN “Internet Protocols and Modeling”, Spring 2012 Course Materials: Papers, Reference Texts: Bertsekas/Gallager, Stuber, Stallings, etc Class.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
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.
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.
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.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
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.
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.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
Object-Orientated Analysis, Design and Programming
WWW and HTTP King Fahd University of Petroleum & Minerals
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:

 Pearson Education, Inc. All rights reserved. 1 CH24 Networking : 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.

 Pearson Education, Inc. All rights reserved Introduction 24.2 Manipulating URLs (skip) 24.3 Reading a File on a Web Server (skip) 24.4 Establishing a Simple Server Using Stream Sockets 24.5 Establishing a Simple Client Using Stream Sockets 24.6 Client/Server Interaction with Stream Socket Connections 24.7 Connectionless Client/Server Interaction with Datagrams 24.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server (skip) 24.9 Security and the Network Wrap-Up

 Pearson Education, Inc. All rights reserved Introduction Networking package is java.net – Stream-based communications Applications view networking as streams of data Connection-based protocol Uses TCP (Transmission Control Protocol – Packet-based communications Individual packets transmitted Connectionless service Uses UDP (User Datagram Protocol)

 Pearson Education, Inc. All rights reserved Introduction (Cont.) Client-server relationship – Client requests some action be performed – Server performs the action and responds to client – Request-response model Common implementation: Web browsers and Web servers

 Pearson Education, Inc. All rights reserved. 5 Performance Tip 24.1 Connectionless services generally offer greater performance but less reliability than connection- oriented services.

 Pearson Education, Inc. All rights reserved. 6 TCP, UDP and related protocols enable a great variety of heterogeneous computer systems (i.e., computer systems with different processors and different operating systems) to intercommunicate. Portability Tip 24.1

 Pearson Education, Inc. All rights reserved Manipulating URLs HyperText Transfer Protocol (HTTP) – Uses URIs (Uniform Resource Identifiers) to identify data URLs (Uniform Resource Locators) – URIs that specify the locations of documents – Refer to files, directories and complex objects HTML document SiteSelector.html (Fig. 24.1) – applet element – param tag name attribute value attribute

 Pearson Education, Inc. All rights reserved Reading a File on a Web Server Swing GUI component JEditorPane – Render both plain text and HTML-formatted text – Act as a simple Web browser Retrieves files from a Web server at a given URI HyperlinkEvent s – Occur when the user clicks a hyperlink – Three event types HyperlinkEvent.EventType.ACTIVATED HyperlinkEvent.EventType.ENTERED HyperlinkEvent.EventType.EXITED

 Pearson Education, Inc. All rights reserved. 9 Import JEditPane from package javax.swing, import HyperlinkEvent and HyperlinkListener from package javax.swing.event Declare JEditorPane contentsArea, which will be used to display the contents of the file

 Pearson Education, Inc. All rights reserved. 10 Create JEditorPane using the no-argument constructor Set JEditorPane editable property to false Register a HyperlinkListener to handle HyperlinkEvents, which occur when the user clicks a hyperlink in the JEditorPane Method hyperlinkUpdate is called when a HyperlinkEvent occurs Use HyperlinkEvent method getEventType to determine the type of the HyperlinkEvent Use HyperlinkEvent method getURL to obtain the URL represented by the hyperlink

 Pearson Education, Inc. All rights reserved. 11 Invoke JEditorPane method setPage to download the document specified by location and display it in the JEditPane

 Pearson Education, Inc. All rights reserved. 12

 Pearson Education, Inc. All rights reserved. 13

 Pearson Education, Inc. All rights reserved. 14 A JEditorPane generates HyperlinkEvents only if it is uneditable. Look-and-Feel Observation 24.1

 Pearson Education, Inc. All rights reserved Establishing a Simple Server Using Stream Sockets Five steps to create a simple server in Java – Step 1: Create ServerSocket object ServerSocket server = new ServerSocket( portNumber, queueLength ); Register an available port Specify a maximum number of clients Handshake point Binding the server to the port – Only one client can be bound to a specific port

 Pearson Education, Inc. All rights reserved. 16 Software Engineering Observation 24.1 Port numbers can be between 0 and 65,535. Most operating systems reserve port numbers below 1024 for system services (e.g., and World Wide Web servers). Generally, these ports should not be specified as connection ports in user programs. In fact, some operating systems require special access privileges to bind to port numbers below 1024.

 Pearson Education, Inc. All rights reserved Establishing a Simple Server Using Stream Sockets (Cont.) Five steps to create a simple server in Java – Step 2: Server listens for client connection Server blocks until client connects Socket connection = server.accept(); – Step 3: Sending and receiving data OutputStream to send and InputStream to receive data – Method getOutputStream returns Socket ’ s OutputStream – Methods getInputstream returns Socket ’ s InputStream

 Pearson Education, Inc. All rights reserved Establishing a Simple Server Using Stream Sockets (Cont.) Five steps to create a simple server in Java – Step 4: Process phase Server and Client communicate via streams – Step 5: Close streams and connections Method close

 Pearson Education, Inc. All rights reserved. 19 With sockets, network I/O appears to Java programs to be similar to sequential file I/O. Sockets hide much of the complexity of network programming from the programmer. Software Engineering Observation 24.2

 Pearson Education, Inc. All rights reserved. 20 With Java ’ s multithreading, we can create multithreaded servers that can manage many simultaneous connections with many clients. This multithreaded-server architecture is precisely what popular network servers use. Software Engineering Observation 24.3

 Pearson Education, Inc. All rights reserved. 21 A multithreaded server can take the Socket returned by each call to accept and create a new thread that manages network I/O across that Socket. Alternatively, a multithreaded server can maintain a pool of threads (a set of already existing threads) ready to manage network I/O across the new Sockets as they are created. See Chapter 23 for more information on multithreading. Software Engineering Observation 24.4

 Pearson Education, Inc. All rights reserved. 22 Performance Tip 24.2 In high-performance systems in which memory is abundant, a multithreaded server can be implemented to create a pool of threads that can be assigned quickly to handle network I/O across each new Socket as it is created. Thus, when the server receives a connection, it need not incur the overhead of thread creation. When the connection is closed, the thread is returned to the pool for reuse.

 Pearson Education, Inc. All rights reserved Establishing a Simple Client Using Stream Sockets Four steps to create a simple client in Java – Step 1: Create a Socket to connect to server Socket connection = new Socket ( serverAddress, port ); – Step 2: Obtain Socket ’ s InputStream and Outputstream – Step 3: Process information communicated – Step 4: Close streams and connection

 Pearson Education, Inc. All rights reserved Client/Server Interaction with Stream Socket Connections Client/server chat application – Uses stream sockets – Server waits for a client connection attempt – Client connects to the server Send and receive messages – Client or server terminates the connection – Server waits for the next client to connect

 Pearson Education, Inc. All rights reserved. 25 Import ServerSocket and Socket from package java.net Declare ServerSocket server which waits for client connection Declare Socket connection which connects to the client

 Pearson Education, Inc. All rights reserved. 26

 Pearson Education, Inc. All rights reserved. 27 Create ServerSocket at port with queue of length 100 Wait for a client connection After the connection is established, obtain references to the streams for the connection Send the initial connection message to the client and process all messages received from the client

 Pearson Education, Inc. All rights reserved. 28 Use ServerSocket method accept to wait for a connection from a client Output the host name of the computer that made the connection using Socket method getInetAddress and InetAddress method getHostName Obtain Socket ’s OutputStream and use it to initialize ObjectOutputStream Method flush empties output buffer and sends header information

 Pearson Education, Inc. All rights reserved. 29 Obtain Socket ’s InputStream and use it to initialize ObjectInputStream Use ObjectInputStream method readObject to read a String from client

 Pearson Education, Inc. All rights reserved. 30 Method closeConnection closes streams and sockets Invoke Socket method close to close the socket Use ObjectOutputStream method writeObject to send a String to client

 Pearson Education, Inc. All rights reserved. 31

 Pearson Education, Inc. All rights reserved. 32

 Pearson Education, Inc. All rights reserved. 33

 Pearson Education, Inc. All rights reserved. 34 Common Programming Error 24.1 Specifying a port that is already in use or specifying an invalid port number when creating a ServerSocket results in a BindException.

 Pearson Education, Inc. All rights reserved. 35 Software Engineering Observation 24.5 When using an ObjectOutputStream and ObjectInputStream to send and receive data over a network connection, always create the ObjectOutputStream first and flush the stream so that the client ’ s ObjectInputStream can prepare to receive the data. This is required only for networking applications that communicate using ObjectOutputStream and ObjectInputStream.

 Pearson Education, Inc. All rights reserved. 36 Performance Tip 24.3 A computer ’ s input and output components are typically much slower than its memory. Output buffers typically are used to increase the efficiency of an application by sending larger amounts of data fewer times, thus reducing the number of times an application accesses the computer ’ s input and output components.

 Pearson Education, Inc. All rights reserved. 37

 Pearson Education, Inc. All rights reserved. 38

 Pearson Education, Inc. All rights reserved. 39

 Pearson Education, Inc. All rights reserved. 40 Create a Socket that will connect with port on the server Use InetAddress static method getByName to obtain an InetAdress object containing the IP address specified as a command- line argument to the application Display a message indicating the name of the server computer to which the client has connected Method flush empties output buffer and sends header information Obtain Socket ’s OutputStream and use it to initialize ObjectOutputStream

 Pearson Education, Inc. All rights reserved. 41 Read a String object from server Invoke Socket method close to close the socket

 Pearson Education, Inc. All rights reserved. 42 Use ObjectOutputStream method writeObject to send a String to server

 Pearson Education, Inc. All rights reserved. 43

 Pearson Education, Inc. All rights reserved. 44

 Pearson Education, Inc. All rights reserved. 45

 Pearson Education, Inc. All rights reserved Connectionless Client/Server Interaction with Datagrams Connectionless transmission with datagrams – No connection maintained with other computer – Break message into separate pieces and send as packets – Message arrive in order, out of order or not at all – Receiver puts messages in order and reads them

 Pearson Education, Inc. All rights reserved. 47 Use a DatagramSocket as our server

 Pearson Education, Inc. All rights reserved. 48 Use the DatagramSocket constructor that takes an integer port number argument to bind the server to a port where it can receive packets from clients Create a DatagramPacket in which a received packet of information can be stored Use DatagramSocket method receive to wait for a packet to arrive at the server

 Pearson Education, Inc. All rights reserved. 49 Use DatagramPacket method getAddress to obtain the host name of the computer from which the packet was sent Use DatagramPacket method getPort to obtain the port number through which the host computer sent the packet Use DatagramPacket method getLength to obtain the number of bytes of data sent Use DatagramPacket method getData to obtain an byte array containing the data Create a DatagramPacket, which specifies the data to send, the number of bytes to send, the client computer’s Internet address and the port where the client is waiting to receive packets

 Pearson Education, Inc. All rights reserved. 50 Use method send of DatagramSocket to send the packet over the network

 Pearson Education, Inc. All rights reserved. 51 Server window after packet of data is received from client

 Pearson Education, Inc. All rights reserved. 52

 Pearson Education, Inc. All rights reserved. 53 Convert the String to a byte array Create a DatagramPacket and initialize it with the byte array, the length of the string that was entered by the user, the IP address to which the packet is to be sent and the port number at which the server is waiting Use DatagramPacket method send to send the packet

 Pearson Education, Inc. All rights reserved. 54 Create a DatagramSocket for sending and receiving packets

 Pearson Education, Inc. All rights reserved. 55 Create a DatagramPacket to store received information Use DatagramPacket method getAddress to obtain the host name of the computer from which the packet was sent Use DatagramPacket method getPort to obtain the port number through which the host computer sent the packet Use DatagramPacket method getLength to obtain the number of bytes of data sent Use DatagramPacket method getData to obtain an byte array containing the data

 Pearson Education, Inc. All rights reserved. 56

 Pearson Education, Inc. All rights reserved. 57 Client window after sending packet to Server and receiving packet back from Server

 Pearson Education, Inc. All rights reserved. 58 Common Programming Error 24.2 Specifying a port that is already in use or specifying an invalid port number when creating a DatagramSocket results in a SocketException.

 Pearson Education, Inc. All rights reserved Client/Server Tic-Tac-Toe Using a Multithreaded Server Multiple threads – Server uses one thread per player Allow each player to play game independently

 Pearson Education, Inc. All rights reserved. 60

 Pearson Education, Inc. All rights reserved. 61

 Pearson Education, Inc. All rights reserved. 62 Create array players with 2 elements Create ServerSocket to listen on port 12345

 Pearson Education, Inc. All rights reserved. 63 Loop twice, blocking at line 79 each time while waiting for a client connection Create a new Player object to manage the connection as a separate thread Execute the Player in the runGame thread pool

 Pearson Education, Inc. All rights reserved. 64

 Pearson Education, Inc. All rights reserved. 65

 Pearson Education, Inc. All rights reserved. 66

 Pearson Education, Inc. All rights reserved. 67 Get the streams to send and receive data

 Pearson Education, Inc. All rights reserved. 68 Format output notifying the other player of the move Call Formatter method flush to force the output to the client Send player’s mark

 Pearson Education, Inc. All rights reserved. 69 Send message indicating one player connected and waiting for another player to arrive Begin the game

 Pearson Education, Inc. All rights reserved. 70 Send message indicating player O connected Read a moveCheck the moveSend message indicating the move is valid

 Pearson Education, Inc. All rights reserved. 71 Send message indicating the move is invalid

 Pearson Education, Inc. All rights reserved. 72

 Pearson Education, Inc. All rights reserved. 73

 Pearson Education, Inc. All rights reserved. 74

 Pearson Education, Inc. All rights reserved. 75

 Pearson Education, Inc. All rights reserved. 76 Connect to the serverGet the streams to send and receive data Read mark character from server

 Pearson Education, Inc. All rights reserved. 77 Loop continually Read and process messages from server If valid move, write message and set mark in square

 Pearson Education, Inc. All rights reserved. 78 If invalid move, display message If opponent moves, set mark in square

 Pearson Education, Inc. All rights reserved. 79

 Pearson Education, Inc. All rights reserved. 80 Send the move to the server

 Pearson Education, Inc. All rights reserved. 81

 Pearson Education, Inc. All rights reserved. 82

 Pearson Education, Inc. All rights reserved. 83

 Pearson Education, Inc. All rights reserved. 84 Fig | Sample outputs from the client/server Tic-Tac-Toe program. (Part 1 of 2.)

 Pearson Education, Inc. All rights reserved. 85 Fig | Sample outputs from the client/server Tic-Tac-Toe program. (Part 2 of 2.)

 Pearson Education, Inc. All rights reserved Security and the Network By default, applets cannot perform file processing Applets often limited in machine access – Applets can communicate only with the machine from which it was originally downloaded Java Security API – Digitally signed applets – Applets given more privileges if from trusted source

 Pearson Education, Inc. All rights reserved. 87 練習 Ex24_08