COMP1681 / SE15 Introduction to Programming

Slides:



Advertisements
Similar presentations
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Advertisements

Socket Programming.
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
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.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Networks 1 CS502 Spring 2006 Network Input & Output CS-502 Operating Systems Spring 2006.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS-3013 & CS-502, Summer 2006 Network Input & Output1 CS-3013 & CS-502, Summer 2006.
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.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Appendix F: Network Programming in Java ©SoftMoore ConsultingSlide 1.
1 ELEN602 Lecture 2 Review of Last Lecture Layering.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 12 Communicating over.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Chapter 2 Applications and Layered Architectures Sockets.
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.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
NETWORK PROGRAMMING.
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.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
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.
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.
Networking Mehdi Einali Advanced Programming in Java 1.
Network Programming. These days almost all devices.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Object-Orientated Analysis, Design and Programming
Network Programming Introduction
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.
NETWORK PROGRAMMING CNET 441
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Sockets and URLs 13-Nov-18.
Networking.
Sockets and URLs 3-Dec-18.
27.
Networking.
Presentation transcript:

COMP1681 / SE15 Introduction to Programming Fast Track Session 2 Java Network Programming

Today’s Learning Objectives For you to learn the various ways in which Java supports the development of networked applications For you to see simple examples of client and server programs written in Java SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Lecture Outline Overview Accessing resources by URL Low-level protocol support Address handling Sockets Writing servers Non-standard libraries SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Java & Networking Java was marketed as a programming language for ‘network-centric computing’… …so it provides good support in standard library for writing networked applications HTTP TCP & UDP Relevant packages are java.net java.io, java.nio javax.net, javax.net.ssl SE15 Fast-Track: Java Network Programming

Treating URLs Like Filenames Create a java.net.URL object, given the URL of a resource expressed as a string Call its openStream method to get an InputStream from which we can read data… …or, for more control, call openConnection to get a URLConnection object getContentLength gives document size getContentType gives doc type (HTML, GIF image…) getInputStream gives stream that we can read SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Example URL url = new URL(urlString); URLConnection conn = new URLConnection(url); if (conn.getContentType().equals("text/html")) { BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line = in.readLine(); while (line != null) { System.out.println(line); line = in.readLine(); } in.close(); } SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming IP, TCP & UDP Data travel over the network as IP datagrams Datagram payload conforms to TCP, UDP or ICMP Transmission Control Protocol (TCP) Provides a reliable connection between endpoints Endpoints defined by port numbers Uses sequence numbers to order datagrams Retransmits datagrams if necessary User Datagram Protocol (UDP) Simpler than TCP No promise of reliable delivery SE15 Fast-Track: Java Network Programming

Hostnames & IP Addresses Networked machines are identified by IP address IPv4 address is a 32-bit integer, typically expressed in ‘dotted quad’ notation Example: 129.11.144.9 Human-readable hostname is resolved to an IP address using the domain name system (DNS) Example: cslin-gps.leeds.ac.uk → 129.11.144.9 Java encapsulates hostnames and IP addresses in the InetAddress class SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming InetAddress Class Provides static factory methods for obtaining InetAddress objects getByName getByAddress getAllByName (all available addresses for host) getLocalHost (address of local machine) InetAddress objects support getHostName (→ hostname as string) getAddress (→ IP address as array of bytes) getHostAddress (→ IP address as string) SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Sockets Provide an abstraction of a TCP connection Much easier to use in Java than in C Much less set-up code No platform variation (e.g. Unix vs. Windows) Object-oriented abstraction Socket must be bound to a local endpoint and connected to a remote endpoint Done automatically by Socket constructor Usually we specify remote address and port and let the system choose an ephemeral port on local machine SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Example of Socket Code Socket socket = new Socket(address, port); BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream())); String line = input.readLine(); while (line != null) { System.out.println(line); line = input.readLine(); } input.close(); socket.close(); SE15 Fast-Track: Java Network Programming

An HTTP Client Using Sockets Socket socket = new Socket(address, port); BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter output = new PrintWriter( new OutputStreamWriter(socket.getOutputStream())); output.print("GET " + document + " HTTP/1.0\n\n"); output.flush(); String line = input.readLine(); while (line != null) { System.out.println(line); line = input.readLine(); } ... SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Writing Servers Socket alone isn’t sufficient to write a server We need something to ‘sit by the phone, waiting for incoming calls’… Java provides ServerSocket class to Listen on a particular port Negotiate connection with client Open a Socket connection between hosts SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Using ServerSocket Create ServerSocket object Call accept to listen for incoming connection attempts Blocks until client attempts to connect Returns Socket object when connection succeeds Call getInputStream and/or getOutputStream on Socket to get streams that communicate with client Interact with client according to agreed protocol Server, client or both close connection Return to Step 2 SE15 Fast-Track: Java Network Programming

Beyond The Standard Library JavaMail API Jakarta Commons, jakarta.apache.org/commons/ Commons Net Support for FTP, NNTP, SMTP, POP3, Telnet… Commons HttpClient Authentication, HTTPS, cookie handling… Commons Email Simplified interface to JavaMail SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Summary We have Seen how easy it is to read data from the web, given the URL of a resource Examined how sockets can be used in Java to created network clients of various kinds Considered how a simple server can be written in Java Looked at some of the advanced applications that are possible using non-standard libraries SE15 Fast-Track: Java Network Programming

SE15 Fast-Track: Java Network Programming Exercise Implement a very simple game (e.g., number guessing, Hangman or Pontoon) as a networked application Devise a protocol for server and client to follow Write a class to act as a server + one to act as a client Get help at http://java.sun.com/docs/books/tutorial/networking/ SE15 Fast-Track: Java Network Programming