Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 25 Networking.

Slides:



Advertisements
Similar presentations
4.01 How Web Pages Work.
Advertisements

COEN 445 Communication Networks and Protocols Lab 4
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
The Internet Useful Definitions and Concepts About the Internet.
1 Java Networking – Part I CS , Spring 2008/9.
Layer 7- Application Layer
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.
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 L23 (Chapter 25) Networking.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
1 Software Testing and Quality Assurance Lecture 32 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Topics in this presentation: The Web and how it works Difference between Web pages and web sites Web browsers and Web servers HTML purpose and structure.
Implementing Application Protocols. Overview An application protocol facilitates communication between applications. For example, an client uses.
15-Jul-15 Basic Protocols. 2 Sockets Sockets, or ports, are a very low level software construct that allows computers to talk to one another When you.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Inter-process Communication.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
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.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
DATA COMMUNICATION DONE BY: ALVIN SAMPATH CARLVIN SAMPATH.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
1 Computer Communication & Networks Lecture 28 Application Layer: HTTP & WWW p Waleed Ejaz
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
HOW WEB SERVER WORKS? By- PUSHPENDU MONDAL RAJAT CHAUHAN RAHUL YADAV RANJIT MEENA RAHUL TYAGI.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
1 Version 3.0 Module 11 TCP Application and Transport.
1 CSC111H Client-Server: An Introduction Dennis Burford
Chapter 18 Networking F Client/Server Communications F Simple Client/Server Applications F Serve Multiple Clients F Create Applet Clients F Send and Retrieve.

Sockets process sends/receives messages to/from its socket
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
1 Welcome to CSC 301 Web Programming Charles Frank.
Application Layer Khondaker Abdullah-Al-Mamun Lecturer, CSE Instructor, CNAP AUST.
CS 7: Introduction to Computer Programming Java and the Internet Sections ,2.1.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 43 Remote Method Invocation.
ECEN “Internet Protocols and Modeling”, Spring 2012 Course Materials: Papers, Reference Texts: Bertsekas/Gallager, Stuber, Stallings, etc Class.
Chapter 16: Networking F Client/Server Communications F Serving Multiple Clients F Applet Clients F Viewing HTML Pages F Retrieving Files from Web Servers.
1 Web Servers (Chapter 21 – Pages( ) Outline 21.1 Introduction 21.2 HTTP Request Types 21.3 System Architecture.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Module: Software Engineering of Web Applications Chapter 2: Technologies 1.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Computer Network Architecture Lecture 6: OSI Model Layers Examples 1 20/12/2012.
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
COMPUTER NETWORKS Hwajung Lee. Image Source:
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 29 Remote Method.
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.
4.01 How Web Pages Work.
4.01 How Web Pages Work.
Instructor Materials Chapter 5 Providing Network Services
MCA – 405 Elective –I (A) Java Programming & Technology
Chapter 18 Networking Client/Server Communications
Application layer Lecture 7.
Process-to-Process Delivery:
4.01 How Web Pages Work.
Presentation transcript:

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 25 Networking

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Objectives F To comprehend socket-based communication in Java (§25.2). F To understand client/server computing (§25.2). F To implement Java networking programs using stream sockets (§25.2). F To obtain Internet address using the InetAddress class (§25.3). F To develop servers for multiple clients (§25.4). F To develop applets that communicate with the server (§25.5). F To send and receive objects on the network (§25.6). F To create applications or applets to retrieve files from the network (§25.7). F To render HTML files using the JEditorPane class (§25.8). F To implement Java networking programs using datagram sockets (§25.10 Optional).

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Networking Basics F Internet protocol (IP) addresses –Every host on Internet has a unique IP address , , , – More convenient to refer to using hostname string cse.ust.hk, tom.com, localhost –One hostname can correspond to multiple internet addresses: u ; ; ; ; … –Domain Naming Service (DNS) maps names to numbers

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved l Ports l Many different services can be running on the host l A port identifies a service within a host l Many standard port numbers are pre-assigned time of day 13, ftp 21, telnet 23, smtp 25, http 80 see /etc/services on workstation for list of all assigned ports l IP address + port number = "phone number“ for service Networking Basics

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved l protocols : rules that facilitate communications between machines l Examples: l HTTP: HyperText Transfer Protocol l FTP: File Transfer Protocol l SMTP: Simple Message Transfer Protocol l TCP: Transmission Control Protocol l UDP: User Datagram Protocol, good for, e.g., video delivery) l Protocols are standardized and documented So machines can reliably work with one another Networking Basics

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved l Client-Server interaction l Communication between hosts is two-way, but usually the two hosts take different roles l Server waits for client to make request Server registered on a known port with the host ("public phone number") Usually running in endless loop Listens for incoming client connections Networking Basics

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved l Client "calls" server to start a conversation Client making calls uses hostname/IP address and port number Sends request and waits for response l Standard services always running ftp, http, smtp, etc. server running on host using expected port l Server offers shared resource (information,database, files, printer, compute power) to clients Networking Basics

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Using telnet to try out some services of servers: l Telnet assumes you want to connect to port 23 on the receiving host (port 23 is where the telnet server is listening) l However there is an optional argument after the hostname that allows you to connect to a different port l Try the following Get time: telnet time-A.timefreq.bldrdoc.gov 13 Get HTML page: telnet 80 and enter a GET command Many servers now refuse telnet connections due to security reasons. Networking Basics

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Socket-Level Programming l java.net.Socket is an abstraction of a bi-directional communication channel between hosts l Send and receive data using streams ClientServer OutputStream InputStream OutputStream

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved A side note F Most machines in CSD now refuse socket connections due to security considerations. F However, you can –Run a sever on any one of lab 4 machines and connect to the server from another lab 4 machine. –Run severs on one of scpu1-14 and connect to the server from others or from the PC network.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Client/Server Communications The server must be running when a client starts. The server waits for a connection request from a client. To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections. After a server socket is created, the server can use this statement to listen for connections. The client issues this statement to request a connection to a server. After the server accepts the connection, communication between server and client is conducted the same as for I/O streams.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Data Transmission through Sockets InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream();

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved A Client/Server Example F Problem: Write a client to send data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved A Client/Server Example, cont.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved A Client/Server Example, cont. Server Code Client Code Note: Start the server, then the client. Start Server Start Client

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The InetAddress Class Occasionally, you would like to know who is connecting to the server. You can use the InetAddress class to find the client's host name and IP address. The InetAddress class models an IP address. You can use the statement shown below to create an instance of InetAddress for the client on a socket. InetAddress inetAddress = socket.getInetAddress(); Next, you can display the client's host name and IP address, as follows: System.out.println("Client's host name is " + inetAddress.getHostName()); System.out.println("Client's IP Address is " + inetAddress.getHostAddress()); IdentifyHostNameIP

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Serving Multiple Clients Multiple clients are quite often connected to a single server at the same time. Typically, a server runs constantly on a server computer, and clients from all over the Internet may want to connect to it. You can use threads to handle the server's multiple clients simultaneously. Simply create a thread for each connection. Here is how the server handles the establishment of a connection: while (true) { Socket socket = serverSocket.accept(); Thread thread = new ThreadClass(socket); thread.start(); } The server socket can have many connections. Each iteration of the while loop creates a new connection. Whenever a connection is established, a new thread is created to handle communication between the server and the new client; and this allows multiple connections to run at the same time.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Serving Multiple Clients Server for Multiple Clients Note: Start the server first, then start multiple clients. Start Server Start Client

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Applet Clients Due to security constraints, applets can only connect to the host from which they were loaded. Therefore, the HTML file must be located on the machine on which the server is running.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating Applet Clients Write an applet that shows the number of visits made to a Web page. The count should be stored in a file on the server side. Every time the page is visited or reloaded, the applet sends a request to the server, and the server increases the count and sends it to the applet. The applet then displays the new count in a message, such as You are visitor number 11. Start ServerAppletClientCountServerStart Client

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Passing Objects in Network Programs Write a program that collects student information from a client and send them to a server. Passing student information in an object. Student Sever Student Class Start Server Note: Start the server first, then the client. Start ClientStudent Client

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Retrieving Files from Web Servers You developed client/server applications in the previous sections. Java allows you to develop clients that retrieve files on a remote host through a Web server. In this case, you don’t have to create a custom server program. The Web server can be used to send the files to the clients.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The URL Class Audio and images are stored in files. The java.net.URL class can be used to identify the files on the Internet. In general, a URL (Uniform Resource Locator) is a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory. You can create a URL object using the following constructor: public URL(String spec) throws MalformedURLException For example, the following statement creates a URL object for try { URL url = new URL(" } catch(MalformedURLException ex) { }

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Creating a URL Instance To retrieve the file, first create a URL object for the file. The java.net.URL. For example, the following statement creates a URL object for URL url = new URL(" You can then use the openStream() method defined in the URL class to open an input stream to the file's URL. InputStream inputStream = url.openStream();

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Retrieving Remote Files This example demonstrates how to retrieve a file from a Web server. The program can run as an application or an applet. The user interface includes a text field in which to enter the URL of the filename, a text area in which to show the file, and a button that can be used to submit an action. A label is added at the bottom of the applet to indicate the status, such as File loaded successfully or Network connection problem. ViewRemoteFile Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JEditorPane Swing provides a GUI component named javax.swing.JEditorPane that can be used to display plain text, HTML, and RTF files automatically. So you don’t have to write code to explicit read data from the files. JEditorPane is a subclass of JTextComponent. Thus it inherits all the behavior and properties of JTextComponent. To display the content of a file, use the setPage(URL) method as follows: public void setPage(URL url) throws IOException JEditorPane generates javax.swing.event.HyperlinkEvent when a hyperlink in the editor pane is clicked. Through this event, you can get the URL of the hyperlink and display it using the setPage(url) method.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating a Web Browser Viewing HTML Files Using the JEditorPane. JEditorPane can be used to display HTML files. WebBrowser Run

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Case Studies: Distributed TicTacToe Games TicTacToeServerRun Server TicTacToeClientRun Client Optional

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Distributed TicTacToe, cont.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Distributed TicTacToe Game