160 likes | 258 Views
Introduction to Interprocess communication. How do the objects within an application communicate?. Approaches you may have already used. Object A calls a method of Object B, supplying Object C (data) as method argument Object A and Object B both access Object C (a shared data object)
E N D
Introduction to Interprocess communication SE-2811 Dr. Mark L. Hornick
How do the objects within an application communicate? SE-2811 Dr. Mark L. Hornick
Approaches you may have already used • Object A calls a method of Object B, supplying Object C (data) as method argument • Object A and Object B both access Object C (a shared data object) • Object A creates/modifies (“writes”) Object C • Object B “reads” Object C This approach can be used to exchange data between threads, by using wait/notify to synchronize the writing/reading of the data SE-2811 Dr. Mark L. Hornick
Another way to exchange data • Object A writes to a file; Object B reads from the file This approach can also be used to exchange data between processes, since the physical file exists outside the life of a process • Actually, Object A writes to an output stream connected to a file, while Object B reads from an input streamalso connected to that file SE-2811 Dr. Mark L. Hornick
A stream is a serial sequence of data Input Streams are used to read data from a source Output Streams are used to write data to a destination SE-2811 Dr. Mark L. Hornick
A File is just a particular case of the source or destination of an I/O stream Under the hood, we are really writing to and reading data to the sectors and tracks of hardware device – a drive. The stream and File objects abstract the detail in an easier-to-use form. The java.io package contains many stream classes that abstract these operations differently: • DataXXputStream, ObjectXXputStream, PrintStream SE-2811 Dr. Mark L. Hornick
What other computer hardware deals with sequential bits and bytes? SE-2811 Dr. Mark L. Hornick
More generally, streams can be connected to a number of serial devices: • Files • Keyboards • Consoles • Serial ports • WAN ports • Memory SE-2811 Dr. Mark L. Hornick
Piped Streams don’t use files at all A object can write to a piped output stream to send data directly to another object reading a piped input stream • The piped output stream must be connected to a piped input stream Essentially, the output pipe writes to a memory buffer (not a file) and the input pipe reads from that buffer • The output and input pipe can be accessed from separate threads SE-2811 Dr. Mark L. Hornick
Demo SE-2811 Dr. Mark L. Hornick
Limitation of Java piped I/O Some underlying OS’s support the concept of named pipes, which allows: • An output pipe to be written from within one process • An input pipe to be read from within a second process Java’s pipes are not named and do not support the concept of named pipes directly, so Java piped I/O is generally used for inter-thread data exchange SE-2811 Dr. Mark L. Hornick
A socket is a higher-level abstraction of the end-point of a two-way communication link between a data source and a destination As with Files, streams are used to read from and write to sockets Each socket uses two streams • An input stream for reading data coming into the socket • An output stream for writing data to another socket SE-2811 Dr. Mark L. Hornick
Data is sent from one socket to another Each socket can be implemented in a separate process • Allowing two processes to exchange data The two processes do not have to be running on the same computer • Allowing processes on separate computers, connected by a network, to exchange data SE-2811 Dr. Mark L. Hornick
Every socket has a unique identifier A socket on one computer connects to a socket on a another computer by locating it by its identifier • URL – Uniform Resource Locator Some examples of a URL: • www.msoe.edu – a DNS name • 192.168.2.101 – an IP address • 127.0.0.1 – the self-address of every computer SE-2811 Dr. Mark L. Hornick
A single computer has a single URL But can support many sockets • Each socket is assigned a port number • Port numbers can range from 0 – 65535 Some port numbers are reserved for sockets used in well-known applications • Web servers and browsers use port 80 for data exchange using the http protocol • FTP clients and servers use ports 20 and 21 for file data transfer and file transfer commands • The first 1024 ports are reserved for these well-known applications SE-2811 Dr. Mark L. Hornick
Once an application locates a specific socket, it can access it’s streams and read and write to that socket • Demo SE-2811 Dr. Mark L. Hornick