Java Network. Slides prepared by : Farzana Rahman



Similar documents
Network Programming using sockets

Data Communication & Networks G

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

Network/Socket Programming in Java. Rajkumar Buyya

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Goal: learn how to build client/server application that communicate using sockets. An interface between application and network

Network Communication

Chapter 2: Application layer

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Lesson: All About Sockets

Socket Programming in Java

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Abhijit A. Sawant, Dr. B. B. Meshram Department of Computer Technology, Veermata Jijabai Technological Institute

Java Programming: Sockets in Java

Creating a Simple, Multithreaded Chat System with Java

Division of Informatics, University of Edinburgh

Socket-based Network Communication in J2SE and J2ME

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Chapter 2 Application Layer

Socket Programming. A er learning the contents of this chapter, the reader will be able to:

Assignment 4 Solutions

NETWORKING FEATURES OF THE JAVA PROGRAMMING LANGUAGE

Socket programming. Complement for the programming assignment INFO-0010

An Overview of Java. overview-1

Network Programming TDC 561

Data Communications & Networks. Session 2 Main Theme Application Layer. Dr. Jean-Claude Franchitti

Socket Programming. Announcement. Lectures moved to

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

Computer Networks/DV2 Lab

Detailed Table of Contents

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Network Programming with Sockets. Process Management in UNIX

Building a Multi-Threaded Web Server

Mail User Agent Project

Building a Java chat server

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

AVRO - SERIALIZATION

presentation DAD Distributed Applications Development Cristian Toma

Remote Method Invocation in JAVA

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

The exam has 110 possible points, 10 of which are extra credit. There is a Word Bank on Page 8. Pages 7-8 can be removed from the exam.

Network-based Applications. Pavani Diwanji David Brown JavaSoft

ELEN 602: Computer Communications and Networking. Socket Programming Basics

1 Introduction: Network Applications

IT6503 WEB PROGRAMMING. Unit-I

Computer Networks. Instructor: Niklas Carlsson

Socket = an interface connection between two (dissimilar) pipes. OS provides this API to connect applications to networks. home.comcast.

Communicating with a Barco projector over network. Technical note

Chapter 3. Internet Applications and Network Programming

Report of the case study in Sistemi Distribuiti A simple Java RMI application

Advanced Network Programming Lab using Java. Angelos Stavrou

Introduction to Socket programming using C

Mobile Application Languages XML, Java, J2ME and JavaCard Lesson 04 Java

INPUT AND OUTPUT STREAMS

Cape Girardeau Career Center CISCO Networking Academy Bill Link, Instructor. 2.,,,, and are key services that ISPs can provide to all customers.

TITLE NETWORK PROGRAMMING (SECURED CLIENT-SERVER CHAT APPLICATION) A PROJECT REPORT PRESENTED ACHILE UGBEDE JOHN CE/2007/165

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

The difference between TCP/IP, UDP/IP and Multicast sockets. How servers and clients communicate over sockets

Lecture 7: Java RMI. CS178: Programming Parallel and Distributed Systems. February 14, 2001 Steven P. Reiss

Applets, RMI, JDBC Exam Review

Sockets. Programação de Sockets em Java. Socket Abstractions. Camada de Transporte

JAVA Program For Processing SMS Messages

Transparent Redirection of Network Sockets 1

Chapter 11. User Datagram Protocol (UDP)

Intranet, Extranet, Firewall

!"# $ %!&' # &! ())*!$

Objectives of Lecture. Network Architecture. Protocols. Contents

Socket Programming. Srinidhi Varadarajan

Java Network Programming

Building Applications With Sockets

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

Agent Languages. Overview. Requirements. Java. Tcl/Tk. Telescript. Evaluation. Artificial Intelligence Intelligent Agents

IMPLEMENTATION OF AN AGENT MONITORING SYSTEM IN A JINI ENVIRONMENT WITH RESTRICTED USER ACCESS

DNS: Domain Name System

15 RMI through firewalls

Network Programming. Writing network and internet applications.

SSC - Communication and Networking Java Socket Programming (II)

File Transfer And Access (FTP, TFTP, NFS) Chapter 25 By: Sang Oh Spencer Kam Atsuya Takagi

TCP/IP - Socket Programming

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Transparent Redirection of Network Sockets 1

Chapter 2: Remote Procedure Call (RPC)

Fundamentals of Symbian OS. Sockets. Copyright Symbian Software Ltd.

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

Preface. Intended Audience

LESSON Networking Fundamentals. Understand TCP/IP

Remote Method Invocation

COM 440 Distributed Systems Project List Summary

CPS221 Lecture: Layered Network Architecture

41376 UDP performing get device status Command Workstation (CWS), Harmony, Bi-directional Driver TCP/UDP

Crash Course in Java

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

Transcription:

Java Network Programming 1

Important Java Packages java.net java.io java.rmi java.security java.lang TCP/IP networking I/O streams & utilities Remote Method Invocation Security policies Threading classes 2

Networking Basics Applications Layer Standard apps HTTP FTP Telnet User apps Transport Layer TCP UDP Programming Interface: Sockets Network Layer IP Link Layer Device drivers TCP/IP Stack Application (http,ftp,telnet, ) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..) 3

Networking Basics TCP (Transport Control Protocol) is a connectionoriented protocol that provides a reliable flow of data between two computers. Example applications: HTTP FTP Telnet TCP/IP Stack Application (http,ftp,telnet, ) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..) 4

Networking Basics UDP (User Datagram Protocol) is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. Example applications: Clock server Ping TCP/IP Stack Application (http,ftp,telnet, ) Transport (TCP, UDP,..) Network (IP,..) Link (device driver,..) 5

Java Sockets Programming Java uses BSD-style sockets to interface with TCP/IP services (java.net package) Java distinguishes between UDP, TCP server & TCP client sockets Behind-the-scenes classes do the actual work & can be updated or swapped out transparently 6

IP Addresses & Hostnames java.net.inetaddress class Represents a single IP address Factory class no public constructor Performs transparent DNS lookups or reverse lookups java.net.unkownhostexception thrown if DNS system can t find IP address for specific host 7

TCP Server Sockets java.net.serversocket class Binds to a local port to listen for initial connections Can be bound to a local IP for multi-homed machines accept() method returns a java.net.socket, not an integer descriptor 8

TCP Client Sockets java.net.socket class Combines socket with socket options (timeout, linger, keep alive, no delay, etc) Encapsulates a java.io.inputstream and a java.io.outputstream can be retrieved for use in a layered I/O system 9

Socket functional calls socket (): Create a socket bind(): bind a socket to a local IP address and port # listen(): passively waiting for connections connect(): initiating connection to another socket accept(): accept a new connection Write(): write data to a socket Read(): read data from a socket sendto(): send a datagram to another UDP socket recvfrom(): read a datagram from a UDP socket close(): close a socket (tear down the connection) 10

JAVA TCP Sockets In Package java.net java.net.socket Implements client sockets (also called just sockets ). An endpoint for communication between two machines. Constructor and Methods Socket(String host, int port): Creates a stream socket and connects it to the specified port number on the named host. InputStream getinputstream() OutputStream getoutputstream() close() java.net.serversocket Implements server sockets. Waits for requests to come in over the network. Performs some operation based on the request. Constructor and Methods ServerSocket(int port) Socket Accept(): Listens for a connection to be made to this socket and accepts it. This method blocks until a connection is made. 11

TCPClient.java import java.io.*; import java.net.*; class TCPClient { public static void main(string argv[]) throws Exception { String sentence; String modifiedsentence; BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in)); Socket clientsocket = new Socket("hostname", 6789); DataOutputStream outtoserver = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader infromserver = new BufferedReader (new InputStreamReader(clientSocket.getInputStream())); sentence = infromuser.readline(); outtoserver.writebytes(sentence + '\n'); modifiedsentence = infromserver.readline(); System.out.println("FROM SERVER: " + modifiedsentence); clientsocket.close(); } } 12

TCPServer.java import java.io.*; import java.net.*; class TCPServer { public static void main(string argv[]) throws Exception { String clientsentence; String capitalizedsentence; ServerSocket welcomesocket = new ServerSocket(6789); while(true) { Socket connectionsocket = welcomesocket.accept(); BufferedReader infromclient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outtoclient = new DataOutputStream(connectionSocket.getOutputStream()); clientsentence = infromclient.readline(); capitalizedsentence = clientsentence.touppercase() + '\n'; outtoclient.writebytes(capitalizedsentence); } } } 13

Another simple server // SimpleServer.java: a simple server program import java.net.*; import java.io.*; public class SimpleServer { public static void main(string args[]) throws IOException { } // Register service on port 1254 ServerSocket s = new ServerSocket(1254); Socket s1=s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getoutputstream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeutf("hi there"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } 14

Another simple client // SimpleClient.java: a simple client program import java.net.*; import java.io.*; public class SimpleClient { public static void main(string args[]) throws IOException { } // Open your connection to a server, at port 1254 Socket s1 = new Socket("mundroo.cs.mu.oz.au",1254); // Get an input file handle from the socket and read the input InputStream s1in = s1.getinputstream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readutf()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1in.close(); s1.close(); } 15

UDP Sockets java.net.datagramsocket class Java makes no distinction between client/server for UDP sockets Connected mode UDP supported in Java 2 Can be bound to both a local port & a local IP address multi-homed support 16

UDP Datagrams java.net.datagrampacket class Expects a byte array of data Address optional for connected-mode UDP This class is final can t be extended! java.net.datagramsocket instances can only send instances of java.net.datagrampacket 17

Threading Java doesn t support the notion of forking processes; how do we support concurrency? Java was designed to support multi-threading! In server environments we can spawn new threads to handle each client Thread groups allow for collective control of many threads 18

Java Servlets Servlets are the Java analog to CGI Advantages of servlets: full access to other Java APIs, persistence between invocations, guaranteed portability Servlets can be generic services or specific to HTTP Slides prepared by 19: Farzana Rahman

HTTP Servlets javax.servlet.http.httpservlet class Uses HTTP to receive requests and generate responses Full support for all HTTP methods, cookies, sessions, persistent connections Slides prepared by 20: Farzana Rahman

Java Applets Client-side Java programs that run in a browser Applets have special security restrictions called the applet sandbox Only applets loaded over the network are subject to the applet sandbox The applet sandbox is controlled by a java.lang.securitymanager Slides prepared by 21: Farzana Rahman

Applet Sandbox Can t load libraries or define native methods Can t access local host filesystem Can t open sockets to hosts other than originating host Can t use Runtime.exec() Applet windows have a unique appearance Restricted access to certain system properties Slides prepared by 22: Farzana Rahman

Remote Method Invocation (RMI) RMI is the Java analog to RPC RMI servers use a naming service (rmiregistry) to register remote objects RMI servers use a special security policy implemented by RMISecurityManager The default RMI transport mechanism is via TCP sockets this is transparent to RMI code! Any object transferred in an RMI call must implement the Serializable interface Slides prepared by 23: Farzana Rahman

Java Naming & Directory Interface (JNDI) JNDI provides a generic API that can be used to interface with any naming system JNDI uses SPIs (service provider interfaces) to access many different types of naming & directory services from the JNDI API Sun supplies JNDI SPIs for LDAP, NIS, COS (CORBA naming), RMI registry & local filesystem Slides prepared by 24: Farzana Rahman

END 25