Network/Socket Programming in Java. Rajkumar Buyya



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

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Java Network. Slides prepared by : Farzana Rahman

Division of Informatics, University of Edinburgh

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

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

Network Communication

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

JAVA Program For Processing SMS Messages

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

Assignment 4 Solutions

Lesson: All About Sockets

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

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

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

Creating a Simple, Multithreaded Chat System with Java

Building a Multi-Threaded Web Server

Socket-based Network Communication in J2SE and J2ME

Advanced Network Programming Lab using Java. Angelos Stavrou

Socket Programming in Java

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

Communicating with a Barco projector over network. Technical note

AVRO - SERIALIZATION

Java Programming: Sockets in Java

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

An Android-based Instant Message Application

Building a Java chat server

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

JAVA - FILES AND I/O

Socket programming. Complement for the programming assignment INFO-0010

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

Network Programming using sockets

Socket Programming. Announcement. Lectures moved to

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

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

Distributed Network Management Using SNMP, Java, WWW and CORBA

public static void main(string[] args) { System.out.println("hello, world"); } }

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Getting Started with the Internet Communications Engine

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

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Data Communication & Networks G

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

INPUT AND OUTPUT STREAMS

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Accessing PostgreSQL through JDBC via a Java SSL tunnel

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

Mail User Agent Project

An Overview of Java. overview-1

Remote Method Invocation

Domain Name System (DNS) Omer F. Rana. Networks and Data Communications 1

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

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

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

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

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

Threads & Tasks: Executor Framework

5 HDFS - Hadoop Distributed System

DB2 Connect for NT and the Microsoft Windows NT Load Balancing Service

What is an I/O Stream?

Example of Standard API

MSDG Services Integration Document Draft Ver 1.2

Intel Retail Client Manager Audience Analytics

Agenda. Distributed System Structures. Why Distributed Systems? Motivation

Middleware Lou Somers

File Sharing. Peter Lo. CP582 Peter Lo

XII. Distributed Systems and Middleware. Laurea Triennale in Informatica Corso di Ingegneria del Software I A.A. 2006/2007 Andrea Polini

OBJECT ORIENTED PROGRAMMING LANGUAGE

WRITING DATA TO A BINARY FILE

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

Cross-platform TCP/IP Socket Programming in REXX

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

Network Attached Storage. Jinfeng Yang Oct/19/2015

Apache Thrift and Ruby

Configuring and Monitoring Hitachi SAN Servers

Amoeba Distributed Operating System

Logging in Java Applications

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

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

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Tier Architectures. Kathleen Durant CS 3200

Client-Server Applications

Connecting to the network

COSC 6397 Big Data Analytics. Distributed File Systems (II) Edgar Gabriel Spring HDFS Basics

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

AN APPLICATION OF INFORMATION RETRIEVAL IN P2P NETWORKS USING SOCKETS AND METADATA

Remote Method Invocation (RMI)

Procedure: You can find the problem sheet on Drive D: of the lab PCs. 1. IP address for this host computer 2. Subnet mask 3. Default gateway address

NetCrunch 6. AdRem. Network Monitoring Server. Document. Monitor. Manage

Overview of Web Services API

EVALUATION OF TOOLS FOR CYBER SECURITY

JAVA - MULTITHREADING

13 File Output and Input

Network Programming TDC 561

Objectives of Lecture. Network Architecture. Protocols. Contents

Transcription:

Network/Socket Programming in Java Rajkumar Buyya

Elements of C-S Computing a client, a server, and network Client Request Result Network Server Client machine Server machine

java.net Used to manage: URL streams Client/server sockets Datagrams

Part III - Networking ServerSocket(1234) Output/write stream Input/read stream Socket( 128.250.25.158, 1234) Server_name: manjira.cs.mu.oz.au 4

Server side Socket Operations 1. Open Server Socket: ServerSocket server; DataOutputStream os; DataInputStream is; server = new ServerSocket( PORT ); 2. Wait for Client Request: Socket client = server.accept(); 3. Create I/O streams for communicating to clients is = new DataInputStream( client.getinputstream() ); os = new DataOutputStream( client.getoutputstream() ); 4. Perform communication with client Receiive from client: String line = is.readline(); Send to client: os.writebytes("hello\n"); 5. Close sockets: client.close(); For multithreade server: while(true) { i. wait for client requests (step 2 above) ii. create a thread with client socket as parameter (the thread creates streams (as in step (3) and does communication as stated in (4). Remove thread once service is provided.

Client side Socket Operations 1. Get connection to server: client = new Socket( server, port_id ); 2. Create I/O streams for communicating to clients is = new DataInputStream( client.getinputstream() ); os = new DataOutputStream( client.getoutputstream() ); 3. Perform communication with client Receiive from client: String line = is.readline(); Send to client: os.writebytes("hello\n"); 4. Close sockets: client.close();

A simple server (simplified code) import java.net.*; import java.io.*; public class ASimpleServer { public static void main(string args[]) { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); 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(); 7

A simple client (simplified code) 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 1234 Socket s1 = new Socket("130.63.122.1",1234); // 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(); 8

Echo Server Client.. //client.java: client interface to server import java.io.*; import java.net.*; public class client { int port_id; String server; Socket slink; DataOutputStream os; DataInputStream is; DataInputStream kbd; public client( String args[] ) { server = args[0]; port_id = Integer.valueOf(args[1]).intValue(); try { slink = new Socket( server, port_id ); os = new DataOutputStream( slink.getoutputstream() ); is = new DataInputStream( slink.getinputstream() ); kbd = new DataInputStream( System.in );

Echo Server Client.. catch( UnknownHostException e ) { System.err.println( "Don't know about host: " ); System.exit(1); catch( IOException e ) { System.err.println( "Could not get I/O for the connection to "+server); System.exit(1); void communicate() { while(true) { try { System.out.print("Enter Input <end to stop>: ");

Echo Server Client.. if( line.equals("end") ) { os.close(); is.close(); slink.close(); break; String line2 = is.readline(); System.out.println("Output: "+line2); catch( IOException e ) { System.out.println(e); public static void main( String [] args ) { if( args.length < 2 ) { System.out.println("Usage: java client server_name port_id" ); System.exit(1);

Echo Server... // server.java: echo server import java.io.*; import java.net.*; public class server { // public final static int PORT = 4779; public static void main( String [] args ) { ServerSocket server = null; DataOutputStream os = null; DataInputStream is = null; boolean shutdown = false; if( args.length < 1 ) { System.out.println( "Usage: java server port_num" ); System.exit( 1 ); int PORT = Integer.valueOf(args[0]).intValue();

Echo Server... catch( IOException e ) { System.err.println( "Could not get I/O for the connection to: "); while(!shutdown) { if( server!= null ) { try { Socket client = server.accept(); System.out.println("Connected"); InetAddress cip = client.getinetaddress(); System.out.println( "Client IP Addr: "+cip.tostring()); is = new DataInputStream( client.getinputstream() ); os = new DataOutputStream(

Echo Server... if( line.startswith("end" ) ) { shutdown = true; break; os.writebytes(line.touppercase()); os.writebytes("\n"); System.out.println(line); is.close(); client.close(); catch( UnknownHostException e ) { System.err.println( "Server Open fails" ); catch( IOException e ) { System.err.println( "Could not get I/O for the connection

Echo Server System.out.println( "Server Down" ); try { server.close(); catch(ioexception e) {

Threads in Action... Multithreaded Server Client Process Server Process Server Threads Client Process User Mode Message Passing Facility Kernel Mode

Client/Server Computing Rajkumar Buyya

Client Server Definition server software accepts requests for data from client software and returns the results to the client

Elements of C-S Computing a client, a server, and network Client Request Result Network Server Client machine Server machine

Where Operations are Done In CS Relationship most of the application processing is done on a computer (client side), which obtains application services (such as database services) from another computer (server side) in a master slave configuration.

CS-Focus is on In client-server computing major focus is on SOFTWARE

Application Tasks User Interface Presentation Logic Application Logic Data Requests & Results Physical Data Management

Client (dumb) - Server Model Client Keystroke Server Presentation Logic Network Application Logic Displays DBMS

True Client-Server Model Client Presentation Logic Keystroke Processed Results Network Server Application Logic DBMS

Distributed Client-Server Model Client Application Logic Presentation Logic Processed Queries Processed Results Network Server Application Logic DBMS

Client-server computing is distributed access, not a distributed computing.

RPC Look and Feel like Local Calls results= bar(arguments) calling procedure results= called bar(arguments) procedure calling procedure (client) arguments results results= bar(arguments) called procedure (client) Local Procedure Call arguments client stub network transport request message results reply message Network Remote Procedure Call results arguments server stub network transport reply message request message

Flow Control in a Sychronous RPC Client Machine Server Machine Service Daemon Listening Client Program Client Waiting RPC Call with Request Network return ( ) reply Invoke Service Service Call return() answer Service Executes Request Completed May be the same machine

Multithreaded Server Client Process Server Process Server Threads Client Process User Mode Message Passing Facility Kernel Mode

Categories of Servers File Server Data Server Compute Server Database Server Communication Server Video Server

File Server File Servers manage a work group s application and data files, so that they may be shared by the group. Very I/O oriented Pull large amount of data off the storage subsystem and pass the data over the network Requires many slots for network connections and a large-capacity, fast hard disk subsystem.

Compute Server Performs Application logic processing Compute Servers requires processors with high performance capabilities large amounts of memory relatively low disk subsystems By separating data from the computation processing, the compute server s processing capabilities can be optimized

Data Server Data-oriented; used only for data storage and management Since a data server can serve more than one compute server, compute-intensive applications can be spread among multiple severs Does not prefer any application processing Performs processes such as validation, required as part management function. logic data of the data Requires fast processor, large amount of memory and substantial Hard disk capacity. Data Server Compute Server

Database Server Most typical use of technology in client-server Accepts requests for data, retrieves the data from its database(or requests data from another node)and passes the results back. Compute server with data server provides the same functionality. The server requirement depends on the size of database, speed with which the database must be updated, number of users and type of network used.

Communication Server Provides gateway to other LANs, networks & Computers E-mail Server & internet server Modest system requirements multiple slots fast processor to translate networking protocols

Internet Server Internet Server PC client Local Area Network UNIX workstations

Distributed processing application connects to remote database S Q L * Forms SQL *Net TCP/IP UNIX Server Distributed database application connects to local database which connects to remote database SQL *Net TCP/IP SQL * Forms SQL *Net TCP/IP ORACL E ORACLE Database Configurations

Client-Server Waves Ethernet era client/server Intergalactic era client/server First Wave Second Wave Third Wave File servers groupware Database servers TP monitors Distributed objects 1982 1986 1990 1994 1998

The Client/Server Infrastructure Client Middleware Server Service Specific SQL/IDAPI TxRPC Mail ORB Objects GUI/OOUI DSM Groupware DSM Operating System SNMP CMIP DME Directory Security Distributed file RPC NOS Messaging Transport Stack Peer-to-peer NetBIOS TCP/IP IPX/SPX SNA TP monitor DBMS DSM Operating System