Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Java Network Programming Vishnuvardhan.M. Dept. of Computer Science - SSBN Java Overview Object-oriented Developed with the network in mind Built-in exception.
Advertisements

Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
 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.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
COMP1681 / SE15 Introduction to Programming
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Networking Support In Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
1 Network Programming Introduction zBased on Classes in the java.net package zLecture focuses on: yTCP and UDP yNetwork programming basics yIdentifying.
Networking in Java Representation and Management of Data on the Internet.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
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.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
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:
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
CS 424/524: Introduction to Java Programming Lecture 25 Spring 2002 Department of Computer Science University of Alabama Joel Jones.
DBI Representation and Management of Data on the Internet.
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 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
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.
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.
Java Sockets Programming
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Socket Programming Using JAVA Asma Shakil Semester 1, 2008/2009.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
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.
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Java Network Programming Network Programming Spring 2000 Jeffrey E. Care
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
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.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Network Programming. These days almost all devices.
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
Network Programming Introduction
Network Programming Introduction
Sockets and URLs 17-Sep-18.
„Networking”.
Sockets and URLs 13-Nov-18.
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Networking.
Sockets and URLs 3-Dec-18.
NETWORK PROGRAMMING CNET 441
Presentation transcript:

Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park

Testing (old topic) You must write test cases Release tests are designed so that they don’t give you enough information; you have to write test cases In many cases, people were failing release tests and if they had written any test for their failing methods, they would have found the error We are making some changes to provide better feedback about the quality of your test cases We will beat on you if you don’t write test cases

Overview Networking Background Concepts Network applications Java’s objected-oriented view Java’s networking API (Application Program Interface) Everything you need to know: Last lecture This lecture

Client / Server Model Relationship between two computer programs Client Initiates communication Requests services Server Receives communication Provides services Other models Peer-to-peer (P2P) Client Server Client

Client Programming Basic steps 1. Determine server location – IP address & port 2. Open network connection to server 3. Write data to server (request) 4. Read data from server (response) 5. Close network connection 6. Stop client

Simple Server Programming Basic steps 1. Determine server location - port (& IP address) 2. Create ServerSocket to listen for connections 3. While (true) { 4. accept network connection to client 5. Read data from client (request) 6. Write data to client (response) 7. Close network connection to client 8. }

Supporting multiple connections / clients Can support multiple connections / clients Loop Handles multiple connections in order Limits on how much traffic we can handle Not resilient in face of slow/stopped clients Multithreading Allows multiple simultaneous connections

Networking in Java Packages java.net  Networking java.io  I/O streams & utilities java.rmi  Remote Method Invocation java.security  Security policies java.lang  Threading classes Support at multiple levels Data transport  Socket classes Network services  URL classes Utilities & security

Java Networking API Application Program Interface Set of routines, protocols, tools For building software applications Java networking API Helps build network applications Interfaces to sockets, network resources Code implementing useful functionality Includes classes for Sockets URLs

Java Networking Classes IP addresses InetAddress Packets DatagramPacket Sockets Socket ServerSocket DatagramSocket URLs URL

InetAddress Class Represents an IP address Can convert domain name to IP address Performs DNS lookup Getting an InetAddress object getLocalHost() getByName(String host) getByAddress(byte[] addr)

InetAddress is a factory You can’t create InetAddress objects by invoking the InetAddress constructor Instead, invoke static methods of the InetAddress class These methods return either a Inet4Address or a Inet6Address both of which extend InetAddress The factory design pattern delegates responsibility for design what class of object to create to the factory

ServerSocket Class Create socket on server Constructor specifies local port Server listens to port Usage Begin waiting after invoking accept() Listen for connection (from client socket) Returns Socket for connection

ServerSocket Methods accept() close() getInetAddress() getLocalPort()

Socket Class One end of a TCP connection Client uses Constructor, providing: Machine name or IP address Port number Socket also returned by accept method Transfer data via streams standard Java I/O streams

Socket Methods getInputStream() getOutputStream() close() getInetAddress() getPort() getLocalPort()

I/O 4 fundamental interfaces InputStream - incoming stream of bytes Reader - incoming stream of characters OutputStream - outgoing stream of bytes Writer - outgoing stream of characters

Starting points FileInputStream/FileOutputStream FileReader/FileWriter ByteArrayInputStream/ByteArrayOutputStream StringReader/StringWriter Socket inputStream / outputStream

Decorator design pattern The java I/O libraries demonstrate the decorator design pattern You have some interface X, and a bunch of class that both implement X and take an X object as an argument A decorate intercepts / monitors / modifiers calls that are usually delegated to the object specified at construction time For example, a LineNumberReader used to decorate any Reader, gives the ability to find out what line number you are currently at

Adapter design pattern The I/O libraries also demonstrate the adapter design pattern Given two interfaces, an X-to-Y adapter takes an X object when constructed and implements the Y interface Y call’s are translated into corresponding X calls on the contained object For example, an InputStreamReader takes any InputStream and produces a Reader

Some important facts Once you have written to a writer or outputStream, it may not be immediately sent flush or close the output stream to ensure the data is sent A socket, it’s inputStream and it’s outputStream are all linked closing any one closes all three Have to carefully choreograph who reads and who writes when if both ends are reading, waiting for the other to send something, they deadlock

UDP/Datagram Sockets Create DatagramSockets bound to a port for clients, can bind to “arbitrary, unspecified but free port” Create DatagramPacket Stores data to be sent/received, address and port destination for packets being sent source for received packets can reuse the byte arrays associated with a Datagram avoid allocating arrays

DatagramPacket Class Data in packet represented as byte array

DatagramPacket Methods getAddress() getData() getLength() getPort()

DatagramSocket Class Create UDP socket Constructor specifies port can let system find arbitrary free port Send / receive DatagramPacket

DatagramSocket Methods close() getLocalAddress() - only interesting if you have multiple IP addresses on the local machine getLocalPort() receive(DatagramPacket p) send(DatagramPacket p) setSoTimeout(int t) - timeout getSoTimeout()

URL Class Provides high-level access to network data Abstracts the notion of a connection Constructor opens network connection To resource named by URL

URL Constructors URL( fullURL ) URL( " ) URL( baseURL, relativeURL ) URL base = new URL(" ); URL class = new URL( base, "/class/index.html " ); URL( protocol, baseURL, relativeURL ) URL( "http", "/class/index.html" ) URL( protocol, baseURL, port, relativeURL ) URL( "http", 80,"/class/index.html" )

URL Methods getProtocol( ) getHost( ) getPort( ) getFile( ) openConnection() returns URLConnection URLConnection getType() getInputStream()

URL Reader URLConnection connection = url.openConnection(); System.out.println("Type : " + connection.getContentType()); BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close();