Principles, Models, and Applications for Distributed Systems M

Similar documents
NETWORK PROGRAMMING IN JAVA USING SOCKETS

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

Java Network. Slides prepared by : Farzana Rahman

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

Socket Programming in Java

Java Programming: Sockets in Java

Network Communication

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

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

Network Programming with Sockets. Process Management in UNIX

Network/Socket Programming in Java. Rajkumar Buyya

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

Network Programming using sockets

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

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

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

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

Socket-based Network Communication in J2SE and J2ME

SSC - Communication and Networking Java Socket Programming (II)

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Socket Programming. Announcement. Lectures moved to

Network Programming TDC 561

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

Division of Informatics, University of Edinburgh

Socket programming. Complement for the programming assignment INFO-0010

Objectives of Lecture. Network Architecture. Protocols. Contents

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

Creating a Simple, Multithreaded Chat System with Java

Transport Layer Protocols

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

Data Communication & Networks G

Socket Programming in C/C++

NETWORKING FEATURES OF THE JAVA PROGRAMMING LANGUAGE

INPUT AND OUTPUT STREAMS

Interprocess Communication Message Passing

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

Assignment 4 Solutions

Tutorial on Socket Programming

Network-based Applications. Pavani Diwanji David Brown JavaSoft

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

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

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

TCP/IP - Socket Programming

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

AVRO - SERIALIZATION

Protocols and Architecture. Protocol Architecture.

Building a Multi-Threaded Web Server

Computer Networks. Chapter 5 Transport Protocols

Lesson: All About Sockets

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

Computer Networks/DV2 Lab

Networks. Inter-process Communication. Pipes. Inter-process Communication

Lab 4: Socket Programming: netcat part

[Prof. Rupesh G Vaishnav] Page 1

Introduction to Socket programming using C

The POSIX Socket API

Transparent Redirection of Network Sockets 1

Socket Programming. Srinidhi Varadarajan

WRITING DATA TO A BINARY FILE

Implementing and testing tftp

q Connection establishment (if connection-oriented) q Data transfer q Connection release (if conn-oriented) q Addressing the transport user

Operating Systems Design 16. Networking: Sockets

Computer Networks Practicum 2015

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

Chapter 3. Internet Applications and Network Programming

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

Computer Networks UDP and TCP

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

Chapter 11. User Datagram Protocol (UDP)

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

ICOM : Computer Networks Chapter 6: The Transport Layer. By Dr Yi Qian Department of Electronic and Computer Engineering Fall 2006 UPRM

Building Applications With Sockets

Implementing Network Software

Ethernet. Ethernet. Network Devices

Chapter 2: Remote Procedure Call (RPC)

CS 1302 Ch 19, Binary I/O

Generalised Socket Addresses for Unix Squeak

Real Time Programming: Concepts

An Overview of Java. overview-1

JAVA - FILES AND I/O

CPS221 Lecture: Layered Network Architecture

What is an I/O Stream?

Communication. Layered Protocols. Topics to be covered. PART 1 Layered Protocols Remote Procedure Call (RPC) Remote Method Invocation (RMI)

Socket Programming in the Data Communications Laboratory

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

Design and Implementation of an Intelligent Network Monitoring and Management Tool in Internet and Intranet

Preface. Intended Audience

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

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

Mail User Agent Project

Computer Networks Network architecture

Basic Networking Concepts. 1. Introduction 2. Protocols 3. Protocol Layers 4. Network Interconnection/Internet

Network Programming. Writing network and internet applications.

The OSI model has seven layers. The principles that were applied to arrive at the seven layers can be briefly summarized as follows:

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

Intranet, Extranet, Firewall

TCP/IP Fundamentals. OSI Seven Layer Model & Seminar Outline

CS335 Sample Questions for Exam #2

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

Transcription:

Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M C/S applications using Java Sockets Luca Foschini

SOCKET for COMMUNICATION Need for a standard to connect distributed devices distinct, different, highly heterogeneous We will use sockets as a communication standard. Sockets permit to access communication mechanisms offered by the operating system. socket represents the local host (endpoint) in a bi-directional communication channel across network (from/to other hosts). Client and Server applications running on distinct devices can communicate using two different types of communication with different costs and quality guarantees. Sockets originated in Unix BSD 4.2 Java Sockets 2

COMMUNICATION TYPES connection-oriented: there is a stable connection between Client and Server (state maintained only at endpoints, e.g.: Web) STREAM socket connectionless: there is no connection, each message is dispatched independently from others (e.g.: mail system) INTERNET SOCKET classes: Connection-oriented using TCP Internet protocol Socket class, Client side ServerSocket class, Server side connectionless using UDP Internet protocol DatagramSocket class for both Client and Server DATAGRAM socket Java Sockets 3

The java.net networking package contains all classes needed to use sockets in Java. Java classes hierarchy Every Java Virtual Machine (JVM) release noticeable extended its classes and interfaces. Its philosophy and mechanism do not change. 4

NAME SYSTEM We need a name system to identify each element A distributed application comprises processes distinct by their locality that exchange messages to communicate and cooperate to achieve coordinated results First problem: mutual process identification (Client and Server) across network Each process must be associated to a GLOBAL NAME unique, unambiguos, and simple Node name + process name on that node Process end points (sockets) typically are local w.r.t. the process itself This first problem is solved by low protocol layers (transport and network). For Internet sockets, transport names (TCP, UDP) and network (IP). Java Sockets 5

SOCKET NAMES A transport endpoint is uniquely identified by: an IP address (4 bytes / 32 bit) a port (16 bit integer) GLOBAL NAME usage IP level TCP and UDP abstracion Messages sent to a specific port of a specific machine (they do not directly reach a process) To reach a resource, we need a LOCAL NAME (socket) The process (one or more) binds to a port to receive (and send) messages This double name system allows to identify a specific process without knowing its local process identifier (PID) (IP network layer, PORT transport layer) An end point for a communication flow is made of an IP and a port Java Sockets 6

SOCKET NAMES Node: IP address Node: IP address Client socket Client port Client Process Server socket listen() Server Process Connected socket read() and write() Network (TCP/IP) Unique server port Java Sockets 7

GLOBAL SOCKET NAMES IP numbers IP address: e.g. 137.204.57.186 Port numbers port, 4 hex digits: XXXXh (dec. 1-65535) often represented as a single decimal number, e.g. 153, 2054 Port function is to identify a service Port numbers lower than 1024 are reserved (well-known port) services offered by processes bound to those ports are standard For example, Web is identified by port 80, i.e. the server process of a web site binds to port 80, from which it receives request for html pages. Other well known TCP ports, server side: port 21 ftp, port 23 telnet, port 25 mail, Java Sockets 8

SEQUENTIAL SERVER Server that manages a single request at a time Connectionless sequential server (UDP) Stateless services Failure management not relevant C1 Cn #serverport Single queue Answers to client port Server Process Controller Actions Connected sequentail server (TCP) Reliable services Limited state Overhead due to connection control C1 Cn #portc1 C1 #serverport #portac1 Unique initial queue Single established connection Answers to Client port #serverport Server Process Controller Actions Java Sockets 9

PARALLEL SERVER Concurrent server with multiple requests at a time (multi-process) Uses multiple processes, a master server spawns an internal process for each service Have to guarantee that the cost due to process spawn does not exceed the gain of having a dedicated #port1 C1 process #serverport Solution that uses processes created beforehand to manage fast service requests. Fixed initial number of processes, other created on-demand and kept running up to a certain time-span. Cn C1 Ci Cn #port1 #portn #portn Active Client processes Server Process Controller Different ports Server Processes process spawn Actions Java Sockets 10

CONCURRENT SERVER Concurrent server with multiple requests at a time (single-process) Hard to realize in Java, with a single process that provides different services. Solution using a single process server that manages many requests at the same time C1 Cn C1 Ci #port1 initial requests #portn connection Server Process Controller Server Process Controller #server port Actions Cn connection Single Server Process Active Client Processes Java Sockets 11

DATAGRAM SOCKET: C-to-Java primitives mapping Datagrams are simple messages that allow Client/Server interactions. Client Process socket() bind() sendto() recvfrom() <wait for response> Server Process socket() bind() recvfrom() <wait for request> <Evaluation> sendto() Java merges these steps close() close() Java Sockets 12

DATAGRAM SOCKET DATAGRAM sockets enable message exchange between two threads without establishing a connection It is unreliable and lossy (in case of network problem) and delivery may be out-of-order (due to UDP protocol specification) Same DATAGRAM socket class for both Client and Server java.net.datagramsocket public final class DatagramSocket extends Object One constructors is (other ones available, see Javadocs ): DatagramSocket( InetAddress localaddress, int localport)throws SocketException; DatagramSocket constructor builds a UDP socket and locally binds it to the specified IP address and port: the socket is ready to send and receive packets. Java Sockets 13

DATAGRAM SOCKET MESSAGE EXCHANGE using sockets, based on rudimental communication mechanisms: send and receive user packets On a sock instance of DatagramSocket one can: void send(datagrampacket p); void receive(datagrampacket p); These methods are real communication functions: the former sends a message (datagram), the latter blocks until it receives the first available datagram. send function assures only that the message is dispatched to the kernel, which will arrange the real send (asynchronous w.r.t. receive) receive function, which delegates real reception to the kernel, blocks the receiver until it gets data (synchronous with the receiver) A single datagram unblocks the receive function. Java Sockets 14

DATAGRAM SOCKET: SEND and RECEIVE send and receive functions need an initialized socket and a support structure: sock.send(datagrampacket p); sock.receive(datagrampacket p); used as input by receive and as output by send. There are many other support classes For instance, DatagramPacket and other constants e.g. integers for port numbers, constants for IP names, integer as IP addresses and as String as domain names InetAddress Many constructors throw specific exceptions: SocketException, SecurityException, Java Sockets 15

COMMUNICATION MODEL Before exchanging messages, datagram socket must be correctly created and need to know each other Sender must insert the receiver address inside the message Messages need information at different levels: Application Control message (and its size) destination node and port of the receiving socket Send socket Socket Receive socket Socket PortX Sender IP Name PortY Receiver IP Name No delivery guarantees due to support protocol (UDP and IP). Java Sockets 16

DatagramPacket class SUPPORT CLASSES Class to build a datagram that specify what to send (data) and to whom (control). Data specifies a byte array to write/read Control port value (int) and InetAddress InetAddress class that represents IP addresses. It provides public static methods only: public static InetAddress getbyname (String hostname); Determines the InetAddress for a given host (passing null returns local loopback address) public static InetAddress[] getallbyname(string hostname); Given the name of a host, returns an array of its IP addresses (when the same logic name corresponds to more than one IP address) public static InetAddress getlocalhost(); Returns InetAddress for the local machine. Java Sockets 17

DATA for DATAGRAMPACKET DatagramPacket contains user level application data DatagramPacket( byte [] buf, // data as byte array int offset, // start offset int length, // data length InetAddress address, int port); // IP address and port There are other constructors and utility functions, for example: InetAddress getaddress() get associated IP address void setaddress(inetaddress addr)change IP address int getport(), void setport(int port) byte[] getdata(), void setdata(byte[] buf), get associated port number set associated port number extract data put data Java Sockets 18

DATAGRAMPACKET DatagramPacket is a complete container that helps developers It has different usages when it is being sent or received: sock.send (DatagramPacketp) when sending, we must reserve some space where the user can write desired data and some space to record control data relative to the receiver (chosen by the sender) Only after this set up step, we send the packet... sock.receive (DatagramPacketp) when receiving, we must set up needed structures to receive both user and control data Only after reception, we can work on the packet and extract desired data A packet can be re-used. Java Sockets 19

COMMUNICATION SCHEMA Create socket DatagramSocket socket = new DatagramSocket(); Sender: Prepare data and send byte[] buf = {'C','i','a','o'}; InetAddress addr = InetAddress.getByName("137.204.59.72"); int port = 1900; DatagramPacket packet = new DatagramPacket(buf, buf.length, addr, port); socket.send(packet); // send packet Other send or receive operations. Java Sockets 20

COMMUNICATION: RECEIVE Create socket: DatagramSocket socket = new DatagramSocket(1900); Receiver: prepare, wait, receive int recport; InetAddress recaddress; byte[] buf = new byte[200]; byte [] req; DatagramPacket packet = new DatagramPacket(buf, buf.length, recaddress, recport); packet.setdata(buf); // attach res as application data area of the packet socket.receive(packet); // receive packet (synchronous wait) // extract application and control data from datagram recport = packet.getport(); recaddress = req = packet.getaddress(); packet.getdata(); // use data Java Sockets 21

COMMUNICATION: RECEIVE AND SEND Note that upon receiving a UDP packet, the resulting DatagramPacket instance is associated with the remote host that sent it. Thus, if a process receives a DatagramPacket and wants to reply to the sender, it can change the application data of the packet and send it without setting again control data: the destination host (set automatically by the operating system) will be the host already associated with the packet. socket.receive(packet); // the received packet is // already associated to // the sender byte[] replydata = {'r', 'e', 'p','l', 'y'}; packet.setdata(replydata); socket.send(packet); Java Sockets 22

ByteArrayOutputStream and ByteArrayInputStream Working directly with a byte array (byte[]) is time-consuming and error prone (one should set bytes one by one). Java provides ByteArrayOutputStream and ByteArrayInputStream as utility classes to write/read data from a byte array. Data(Output/Input)Stream can wrap ByteArray(Output/Input)Stream to ease writing/reading Java object on a byte array. How to write primitive Java types on a byte array stream: ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeint(42); // wrap as data stream, and use it dos.writeutf("test string"); // write some data (String) dos.flush(); dos.close(); //!!! flush and close byte[] buf = baos.tobytearray(); // buf contains written data More info about DataInput/OutputStream in a few slides Java Sockets 23

ByteArrayOutputStream and ByteArrayInputStream How to read data from a byte stream: byte[] buf = baos.tobytearray(); ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(bais); int value = dis.readint(); // read an int String text = dis.readutf(); // read a string dis.close(); //!!! close stream Note that read and write operations must be symmetrical: (for example) trying to read a String while the array contains an integer will raise an exception or cause undefined behaviour. Java Sockets 24

DATAGRAM SOCKET OPTIONS Socket options allow to finely tune sockets behaviour. receive is blocking (synchronous wait) SetSoTimeout (int timeout) throws This options defines a timeout in msec, after which the function terminates and throws an exception If timeout is set to 0, it is disabled (i.e. timeout is equal to infinity) There are many other (advanced) options. Java Sockets 25

STREAM SOCKET A STREAM socket is an end point of a virtual communication channel, established before any data exchange at-most once semantic Communication point-to-point between Client and Server, bi-directional, reliable, data (byte) delivered in order at most once (FIFO, like Unix pipes) If data is lost??? We can t say much Connection between Client process and Server process is unambiguously identified by a unique quadruplet and a protocol <Client IP address; Client port; Server IP address; Server port> STREAM sockets use TCP (+ IP) as communication protocol TCP is the transport protocol and provides the port abstraction; IP is the network protocol and provides identifiers for each node Stream communication between Client and Server needs a connection setup and is asymmetric. Java Sockets 26

Stream sockets create a reliable communication flow between a Server and a Client. Client Java (Socket) merges these steps (bind() is optional) Asymmetric Active role Symmetric STREAM SOCKET: C-to-Java primitives mapping socket() bind() (optional) connect() send()/write() recv()/read() socket() bind() listen() accept() recv()/read() send()/write() Server Java (ServerSocket) merges these 3 steps Passive role close()/shutdown() close()/shutdown() Java Sockets 27

STREAM SOCKET Java uses two different socket types for different roles: one for both Client and Server processes and one only for Server process. Different classes for Client and Server java.net.socket and java.net.serversocket Client Application Socket Connection request Server Application Server Socket Client Application Socket Socket Socket Whenever possible, Java hides implementation details, for example by wrapping many setup steps in constructors. Java Sockets 28

STREAM SOCKET: CLIENT Socket class represents an active, connected stream socket (TCP) between a Client and a Server Socket constructors build the socket, bind it to a local port, and connect it to a port on a remote host running the server. The resulting connection is bi-directional (full duplex) Server Application Client Application Socket Server Socket Port IP Name Creating a socket causes atomically its connection to the server (which must exist). (Unix API are more complex and complete, see: socket, bind, connect). Java Sockets 29

STREAM CLIENT: CONSTRUCTORS public Socket(InetAddress remotehost, int remoteport) throws IOException; Builds a client socket stream and connects it to the specified host on the specified port (Unix equivalent: socket, bind, connect) public Socket (String remotehost, int remoteport)throws Builds a client socket stream and connects it to the specified port of the host whose logic name is remotehost public Socket(InetAddress remotehost, int remoteport, InetAddress localhost, int localport)throws IOException; Builds a client socket stream, binds it to a local port (if localport is 0, it is chosen automatically) and connects it to a port of the remote host. Building a socket atomically causes socket connection to the server (or throws an exception). Java Sockets 30

CLIENT STREAM: MANAGEMENT OPEN implicit by the constructor creating successfully a stream socket implies establishing a bidirectional byte-oriented communication flow (stream) between two processes and requires resources allocation at the two end points. CLOSE needed to free system resources Connections are resources: defining and building them is not for free; they must be managed, kept alive, and then freed Usually: keep only needed connections, close unneeded connections, limit multiple connections close() method closes the socket and disconnects Client from Server public synchronized void close() throws SocketException; Java Sockets 31

CLIENT STREAM: SUPPORT To get additional information about socket public InetAddress getinetaddress(); // remote Returns remote host s IP address public InetAddress getlocaladdress(); // local Returns local IP address public int getport(); // remote port Returns remote port public int getlocalport(); // local Returns local port Example: int port = socket.getport(); Additional information about a socket are available runtime. Java Sockets 32

STREAM: SUPPORT RESOURCES After building a connection we can send and receive data. Read/write from/to socket (IN/OUT) IN/OUT Client Application Socket Server Application Server Socket Port IP Name Port IP Name Java Sockets 33

JAVA COMMUNICATION STREAM Read and write from/to a socket after getting stream resources from the socket public InputStream getinputstream() public OutputStream getoutputstream() These methods return a stream object to wrap the communication channel (InputStream and OutputStream classes) A stream can only send/receive raw bytes, without any additional formatting information TCP delivers bytes ordered and without duplicates (a byte is available on the receiving site only if all preceding bytes already arrived); bytes are delivered at most once If there is an error? No error information A different Java stream can wrap a stream socket to provide high level formatting functions (e.g. DataInputStream) Java Sockets 34

DataOutputStream and DataInputStream DataOutputStream and DataInputStream provide methods to send and receive primitive Java types. Typical usage: define a Client/Server protocols based on Java (exchanging Java object instances): in this class we use them to create Java C/S applications (e.g. to exchange int and Strings ) Main methods to write/read primitive data: DataOutputStream DataInputStream String void writeutf(string str) String readutf() char void writechar(int v) char readchar() int void writeint(int v) int readint() float void writefloat(float v) float readfloat()......... UTF Unified Transformation Format (for strings ) Java Sockets 35

FileOutputStream and FileInputStream Java uses streams to abstract file access: FileOutputStream and FileInputStream, which are specializations respectively of OutputStream and InputStream (generic Output and Input streams) FileOutputStream inherits from OutputStream: void write(int b) throws IOException Writes the specified byte to this file output stream. void write(byte[] b) throws IOException Writes b.length bytes from the specified byte array to this file output stream. FileInputStream inherits from InputStream: int read() throws IOException Reads a byte. Returns the next byte of data, or -1 if the end of the file is reached. int read(byte[] b) throws IOException Reads up to b.length bytes of data from this input stream into an array of bytes. Returns the total number of bytes read into the buffer, or -1 if the end of file is reached. If needed, a DataOutputStream can wrap a FileOutputStream and a DataInputStream can wrap a FileInputStream. Java Sockets 36

FileOutputStream and FileInputStream: example FileOutputStream fos = new FileOutputStream("result.txt"); // open the file result.dat in the current directory byte[] buf = {'t', 'e', 's', 't', '1', '2', '3'}; // test data fos.write(buf); // write the byte array to the fle fos.flush(); //!!! Flush writings fos.close(); //!!! close the file FileInputStream fis = new FileInputStream("result.txt"); byte[] buf2 = new byte[3]; // buffer to read data from file int numread; while ((numread = fis.read(buf2)) > 0) // repeat reads until we reach EOF { } for (int i = 0; i < numread; i++) System.out.println(buf2[i] + " "); // print each byte read from the file as an integer value fis.close(); //!!! Close the file Java Sockets 37

STREAM SERVER: ARCHITECTURE Server side we use the class java.net.serversocket, a ServerSocket is a special socket that can only accept connection requests from many Clients many pending requests at the same time and many ready connections at the same time Need to define the length of the queue of incoming connection that wait to be accepted by the server. Java ServerSocket constructor wraps many simpler steps visible when using UNIX sockets, like socket, bind, listen. Connection established when the Server process allows it (accept) accept (server side) returns a common Socket object valid for the connection request just accepted. Java Sockets 38

SERVERSOCKET: CONSTRUCTORS Socket server side: public ServerSocket(int localport) throws IOException, BindException; Constructs a socket listening on the specified port public ServerSocket(int localport,int count) Constructs a socket listening on the specified port having a connection queue of length count Server is passive : it creates a connection queue and waits for clients Server becomes active when it explicitly accepts a connection. Queued connection requests are not accepted automatically, server uses a specific API to express its will to accept a connection. Java Sockets 39

SERVERSOCKET: ACCEPT Server waits for connection requests calling accept() public Socket accept() throws IOException; accept blocks the Server until at least one connection request is pending accept returns a Socket instance which allows communication between Client and Server For new incoming connection requests, accept creates a new socket for the transport connection already created with the Client: the new Socket returned by accept is the real stream accept blocks the caller, until a new connection request arrives If there are no pending connection requests, the server blocks If there is at least one pending connection request, accept unblocks and creates the connection (connection request is removed from incoming connection requests queue) Java Sockets 40

STREAM SERVER: SUPPORT Data transmission is done the same way by both Client and Server Connection endpoints are completely homogeneous (property derived from TCP) Information about already connected socket: public InetAddress getinetaddress(); // remote Remote address public InetAddress getlocaladdress(); // local Local end point address public int getport(); // remote port Remote end point port public int getlocalport(); // local Local end point address Java Sockets 41

A parallel server, PARALLEL SERVER After accepting a connection, the server spawns a new activity (Thread) that manages the service, inherits the new connection, and closes it at the end of operations The main server immediately waits for new connection requests. PARALLEL MULTIPROCESS CONNECTED SERVER Server thread (father) accept (); <wait on ServerSocket> <spawn thread> <father continues> < provide service using Socket> New Thread Java Sockets 42

Example C/S STREAM PROTOCOL Remote CoPy (RCP) is a distributed Client/Server application that copies a file from C to S Design both the Client and Server applications. Client application must be started as: rcp_client servernode serverport filename destfilename servernode and serverport identify the Server, filename is the filename of a file existent and readable in the Client filesystem. The Client process must send the file filename to the Server, that must write it into the current directory as destfilename. Connection oriented: Client wants to transfer a lot of data, each byte must be received in order and at-most-once. The Client/Server connection is used for both coordination and data transfer. Java Sockets 43

Example: RCP CLIENT / 1 RCP Client code snippets... UTF: standard text representation format (use of write/readutf to write and read Strings) try { rcpsocket = new Socket(host, port); outsocket = new DataOutputStream(rcpSocket.getOutputStream()); insocket = new DataInputStream(rcpSocket.getInputStream()); outsocket.writeutf(destfilename); String resp = insocket.readutf(); System.out.println(resp); if (response.equalsignorecase("msgsrv: waitingforfile")) { // send file Java Sockets 44

Example: RCP CLIENT / 2 sourcefile = new FileInputStream(localFName); byte[] buf = new byte[1000]; // buffer to read a fixed portion // of the file int numbytes; while ((numbytes = sourcefile.read(buf)) > 0) { // read up to buf.length bytes and put the number // of read bytes in numbytes // Repeat reading until we consume the whole file } outsocket.write(buf, 0, numbytes); // send read bytes rcpsocket.close(); // close socket catch (IOException e) {... } Java Sockets 45

RCP: SEQUENTIAL SERVER / 1 try { //... ss = new ServerSocket(serverPort, 5); System.out.println("Listening on port " + ss.getlocalport()); while (true) { // endless loop Socket s = ss.accept(); // wait for a connection System.out.println("Connection "+ s); insock = new DataInputStream(s.getInputStream()); outsock = new DataOutputStream(s.getOutputStream()); String filename = insock.readutf(); System.out.println("Recevied file request: " + filename); outsock.writeutf("msgsrv: waitingforfile"); Java Sockets 46

RCP: SEQUENTIAL SERVER / 2 FileOutputStream fileout = new FileOutputStream(filename); byte[] buf = new byte[200]; int numbytes; while ((numbytes = insock.read(buf)) > 0) // same as before fileout.write(buf, 0, numbytes); s.close(); fileout.flush(); fileout.close(); //! flush output file and close it } catch (IOException e) {... } Java Sockets 47

try { RCP: PARALLEL SERVER rcpsocket = new ServerSocket(port); System.out.println("Listening port:"+rcpsocket.getlocalport()); while(true) { rcpsocketconn = rcpsocket.accept(); } } servicethread = new ServiceRcp (rcpsocketconn); servicethread.start(); catch (IOException e) {System.err.println(e);} Create a new process for each accepted connection. Calling close on a socket closes it (if many threads have a reference to the same socket and any of them calls close, the socket is closed for all of them)??? How many opened socket per process? Java Sockets 48

RCP: SERVER THREAD / 1 public class ServiceRcp extends Thread {... ServiceRcp(Socket incoming){rcpsocketsrv = incoming;} public void run() { System.out.println("thread #" + Thread.currentThread()); System.out.println("Connection: " + rcpsocketsrv); try {outsocket= new DataOutputStream (rcpsocketsrv.getoutputstream()); insocket = new DataInputStream(rcpSocketSrv.getInputStream()); filename = insocket.readutf(); String filename = insock.readutf(); Java Sockets 49

RCP: SERVER THREAD / 2 System.out.println("Recevied file request: " + filename); outsock.writeutf("msgsrv: waitingforfile"); fileout = new FileOutputStream(filename); byte[] buf = new byte[200]; int numbytes; while ((numbytes = insock.read(buf)) > 0) { // same as before fileout.write(buf, 0, numbytes); } /* free resources*/ rcpsocketsrv.close(); fileout.flush(); fileout.close(); //! flush output file and close it } catch (IOException e) {... } System.out.println("End of service #" + Thread.currentThread()); } catch (IOException e) { System.err.println(e); exit(1);} Java Sockets 50

SOCKET CLOSE A Java socket requires application level resources, in addition it requires other operating system level resources, which are allocated to it until the application calls socket.close() It is crucial to close socket to let the system know that it can free the resource associated to the socket itself After closing a socket, some resources remain allocated for some time (depending on importance of pending operations, the input buffer is not saved) For closed connected sockets, the output buffer is saved to keep sending data to peers A peer can detect a closed socket thanks to exception, functions and events, that are notified if it tries to read or write data from/to the socket Any peer can close the socket. Socket close impacts the peer. Java Sockets 51

GRACEFUL SOCKET CLOSE Closing a socket means closing two communication flows Each peer is more responsible for one of the two flows: the read operations of the other peer depend on its output stream. There are methods to close only a flow of a connection: shutdowninput() and shutdownoutput(); Usually Java applications use shutdownoutput() If a peer shuts down its output flow, the output buffer is saved in memory to send remaining data to the other peer, the input memory depends on the actions of the other peer. Java Sockets 52

SOCKET OPTIONS Socket options allow to fine tune sockets behaviour Socket options for stream sockets: SetSoLinger (boolean on, int linger) After closing a socket, the system tries to deliver packets still in the output queue. This options discards such packets after linger seconds. SetTcpNoDelay (boolean on) throws... Output data is sent as soon as possible, without buffering SetKeepAlive (boolean on) throws... Enables and disables the keepalive option All supported options are declared in the SocketOptions interface, with provides both get and set methods. Java Sockets 53