سوکت (ارتباط بین کاربردها)

Slides:



Advertisements
Similar presentations
INTERPROCESS COMMUNICATION
Advertisements

INTERPROCESS COMMUNICATION
Inter Process Commonication
1 Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3, © Addison-Wesley 2001.
2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Socket Programming.
1 Java Networking – Part I CS , Spring 2008/9.
Internetworking (Contd) Chapter 4. Figure 3.26 ATM protocol layers.
OCT Information System Management 1 Organizational Communications and Distributed Object Technologies Week 5: Inter-process Communications.
CSS434 IPC1 CSS434 Interprocess Communication Textbook Ch4 Professor: Munehiro Fukuda.
CS Distributed Computing Systems Chapter 4: Interprocess Communication Chin-Chih Chang, From Coulouris, Dollimore and Kindberg.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Distributed systems (NET 422) Prepared by Dr. Naglaa Fathi Soliman Princess Nora Bint Abdulrahman University College of computer.
OCT -- OCT Chapter 4 Coulouris Interprocess Communication.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 3, © Addison-Wesley.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
2: Application Layer 1 Socket Programming TCP and UDP.
1 Inter-Process Communication: Network Programming using TCP Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
1 Tuesday, December 16, 2008 The practical scientist is trying to solve tomorrow's problem on yesterday's computer. Computer scientists often have it the.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
1 Tuesday, December 23, 2008 If one ox could not do the job they did not try to grow a bigger ox, but used two oxen. - Grace Murray Hopper ( )
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Socket Programming Lee, Sooyong
Slides for Chapter 4: Interprocess Communication
Chapter 4: Interprocess Communication‏ Pages
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
Distributed Systems Concepts and Design Chapter 4.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
Slides for Chapter 4: Interprocess Communication
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
Chapter 3: Interprocess Communication
Source: George Colouris, Jean Dollimore, Tim Kinderberg & Gordon Blair (2012). Distributed Systems: Concepts & Design (5 th Ed.). Essex: Addison-Wesley.
Tanenbaum & Van Steen, Distributed Systems: Principles and Paradigms, 2e, (c) 2007 Prentice-Hall, Inc. All rights reserved Interprocess.
Java Socket programming. Socket programming with TCP.
From Coulouris, Dollimore, Kindberg and Blair Distributed Systems: Concepts and Design Edition 5, © Addison-Wesley 2012 Slides for Chapter 4: Interprocess.
1 Lecture 4: Interprocess Communication Haibin Zhu, PhD. Assistant Professor Department of Computer Science Nipissing University © 2002.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore and Kindberg Distributed Systems: Concepts and Design Edition 4, © Addison-Wesley.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
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.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Slides for Chapter 4: Interprocess Communication From Coulouris, Dollimore, Kindberg Distributed Systems: Concepts and Design Edition 5, © Addison-Wesley.
Network Programming Communication between processes Many approaches:
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
MCA – 405 Elective –I (A) Java Programming & Technology
Socket programming with TCP
Unicast VS Multicast.
Text extensions to slides © David E. Bakken,
Slides for Chapter 4: Interprocess Communication
Networking and Communications
Chapter 2: outline 2.1 principles of network applications
Socket programming - Java
Outline Introduction Networking Basics Understanding Ports and Sockets
Text extensions to slides © David E. Bakken,
Distributed Objects: Communication and System Support
Socket Programming.
Inter-process Communication Models
Distributed Objects: Communication and System Support
Slides for Chapter 4: Interprocess Communication
Socket Programming 2: Application Layer.
Chapter 2: Application layer
Socket Programming with UDP
Review Communication via paired sockets, one local and one remote
Presentation transcript:

سوکت (ارتباط بین کاربردها)

Sockets, message passing, multicast support Applications Underlying interprocess communication primitives: Sockets, message passing, multicast support UDP and TCP

message agreed port any port socket Internet address = 138.37.88.249 Internet address = 138.37.94.248 other ports client server

Socket API (Java): 1 - API for Internet Protocols : UDP Datagram 2 - API for Internet Protocols: TCP stream

API for Internet Protocols : UDP Datagram message size: up to 216 bytes, usually restrict to 8K receive from any: doesn't specify sender origin failure model: failures: can be dropped ordering: can be out of order use of UDP DNS less overhead: no state information, extra messages, latency due to start up

UDP server repeatedly receives a request and sends it back to the client import java.net.*; import java.io.*; public class UDPServer{ public static void main(String args[]){ DatagramSocket aSocket = null; try{ aSocket = new DatagramSocket(6789); byte[] buffer = new byte[1000]; while(true){ DatagramPacket request = new DatagramPacket(buffer, buffer.length); aSocket.receive(request); DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(), request.getAddress(), request.getPort()); aSocket.send(reply); } }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e) {System.out.println("IO: " + e.getMessage());} }finally {if(aSocket != null) aSocket.close();}

(Java API) UDP client sends a message to the server and gets a reply import java.net.*; import java.io.*; public class UDPClient{ public static void main(String args[]){ // args give message contents and server hostname DatagramSocket aSocket = null; try { aSocket = new DatagramSocket(); byte [] m = args[0].getBytes(); InetAddress aHost = InetAddress.getByName(args[1]); int serverPort = 6789; DatagramPacket request = new DatagramPacket(m, m.length(), aHost, serverPort); aSocket.send(request); byte[] buffer = new byte[1000]; DatagramPacket reply = new DatagramPacket(buffer, buffer.length); aSocket.receive(reply); System.out.println("Reply: " + new String(reply.getData())); }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e){System.out.println("IO: " + e.getMessage());} }finally {if(aSocket != null) aSocket.close();} }

ایجاد فضای بسته دیتا گرام کلاینت UDP Datagram سرور ایجاد سوکت با شماره پورت ایجاد سوکت بدون پورت ایجاد فضای بسته دیتا گرام ایجاد بسته درخواست دریافت بسته از سوکت ارسال بسته درخواست تهیه بسته پاسخ ایجاد فضای بسته پاسخ ارسال بسته از طریق سوکت دریافت بسته پاسخ بستن سوکت بستن سوکت

API for Internet Protocols: TCP stream message size: unlimited lost messages: sequence #, ack, retransmit after timeout of no ack flow control: sender can be slowed down or blocked by the receiver message duplication and ordering: sequence # message destination: establish a connection, one sender-one receiver, high overhead for short communication matching of data items: two processes need to agree on format and order (protocol) blocking: non-blocking send, blocking receive (send might be blocked due to flow control) concurrency: one receiver, multiple senders, one thread for each connection failure model checksum to detect and reject corrupt packets sequence # to deal with lost and out-of-order packets connection broken if ack not received when timeout could be traffic, could be lost ack, could be failed process.. can't tell if previous messages were received use of TCP: http, ftp, telnet, smtp

Client and Server of TCP stream The client role involves creating a stream socket bound to any port and then making a connect request asking for a connection to a server at its server port. The server role involves creating a listening socket bound to a server port and waiting for clients to request connections. The listening socket maintains a queue of incoming connection requests. In the socket model, when the server accepts a connection, a new stream socket is created for the server to communicate with a client, meanwhile retaining its socket at the server port for listening for connect requests from other clients.

Client and Server of TCP stream The following are some issues related to stream communication: Message sizes: The application can choose how much data it writes to a stream or reads from it. It may deal in very small or very large sets of data. The underlying implementation of a TCP stream decides how much data to collect before transmitting it as one or more IP packets. Matching of data items: Two communicating processes need to agree as to the contents of the data transmitted over a stream. Threads: When a server accepts a connection, it generally creates a new thread in which to communicate with the new client.

The Java interface to TCP streams is provided in the classes ServerSocket and Socket: ServerSocket: This class is intended for use by a server to create a socket at a server port for listening for connect requests from clients. Its accept method gets a connect request from the queue or, if the queue is empty, blocks until one arrives. The result of executing accept is an instance of Socket – a socket to use for communicating with the client. Socket: This class is for use by a pair of processes with a connection. The client uses a constructor to create a socket, specifying the DNS hostname and port of a server.socket to use for communicating with the client.

The Socket class provides the methods getInputStream and getOutputStream for accessing the two streams associated with a socket. The return types of these methods are DataInputStream and DataOutputStream, respectively – abstract classes that define methods for reading and writing bytes. The return values can be used as the arguments of constructors for suitable input and output streams. The client and server processes use the method writeUTF of DataOutputStream to write it to the output stream and the method readUTF of DataInputStream to read it from the input stream.

public class TCPServer { public static void main (String args[]) { TCP server makes a connection for each client and then echoes the client’s request import java.net.*; import java.io.*; public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen :"+e.getMessage());} // this figure continues on the next slide

continued class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} } public void run(){ try { // an echo server String data = in.readUTF(); out.writeUTF(data); } catch(EOFException e) {System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("IO:"+e.getMessage());} } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}

TCP client makes connection to server, sends request and receives reply import java.net.*; import java.io.*; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname of destination Socket s = null; try{ int serverPort = 7896; s = new Socket(args[1], serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out = new DataOutputStream( s.getOutputStream()); out.writeUTF(args[0]); // UTF is a string encoding see Sn 4.3 String data = in.readUTF(); System.out.println("Received: "+ data) ; }catch (UnknownHostException e){ System.out.println("Sock:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("IO:"+e.getMessage());} }finally {if(s!=null) try {s.close();}catch (IOException e) {System.out.println("close:"+e.getMessage());}} }

کلاینت TCP Stream سرور ایجاد سوکت سرور ایجاد سوکت کلاینت پذیرش ارتباط درخواست ارتباط سوکت جدید، ایجاد ترد ایجاد دو استریم ورودی/ خروجی ایجاد دو استریم ورودی/ خروجی نوشتن خواندن خواندن نوشتن بستن سوکت بستن سوکت

The pairwise exchange of messages is not the best model for communication from one process to a group of other processes, which may be necessary, for example, when a service is implemented as a number of different processes in different computers, perhaps to provide fault tolerance or to enhance availability. A multicast operation is more appropriate – this is an operation that sends a single message from one process to each of the members of a group of processes, usually in such a way that the membership of the group is transparent to the sender.

Multicasting Multicast provide a useful infrastructure for constructing distributed systems with the following characteristics: 1. Fault tolerance based on replicated services: A replicated service consists of a group of servers. Client requests are multicast to all the members of the group, each of which performs an identical operation. 2. Discovering services in spontaneous networking: Multicast messages can be used by servers and clients to locate available discovery services in order to register their interfaces or to look up the interfaces of other services in the distributed system. 3. Better performance through replicated data: Data are replicated to increase the performance of a service – in some cases replicas of the data are placed in users’ computers. 4. Propagation of event notifications: Multicast to a group may be used to notify processes when something happens.

Java API to IP multicast: The Java API provides a datagram interface to IP multicast through the class MulticastSocket, which is a subclass of DatagramSocket with the additional capability of being able to join multicast groups. The class MulticastSocket provides two alternative constructors, allowing sockets to be created to use either a specified local port (6789), or any free local port. A process can join a multicast group with a given multicast address by invoking the joinGroup method of its multicast socket. Effectively, the socket joins a multicast group at a given port and it will receive datagrams sent by processes on other computers to that group at that port. A process can leave a specified group by invoking the leaveGroup method of its multicast socket.

Multicast peer joins a group and sends and receives datagrams import java.net.*; import java.io.*; public class MulticastPeer{ public static void main(String args[]){ // args give message contents & destination multicast group (e.g. "228.5.6.7") MulticastSocket s =null; try { InetAddress group = InetAddress.getByName(args[1]); s = new MulticastSocket(6789); s.joinGroup(group); byte [] m = args[0].getBytes(); DatagramPacket messageOut = new DatagramPacket(m, m.length, group, 6789); s.send(messageOut); // this figure continued on the next slide

continued // get messages from others in group byte[] buffer = new byte[1000]; for(int I = 0 ; i < 3 ; i++) { DatagramPacket messageIn = new DatagramPacket(buffer, buffer.length); s.receive(messageIn); System.out.println("Received:" + new String(messageIn.getData())); } s.leaveGroup(group); }catch (SocketException e){System.out.println("Socket: " + e.getMessage()); }catch (IOException e){System.out.println("IO: " + e.getMessage());} }finally {if(s != null) s.close();}