Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Socket API Unit – Background 4.2 The Socket Metaphor In IPC

Similar presentations


Presentation on theme: "The Socket API Unit – Background 4.2 The Socket Metaphor In IPC"— Presentation transcript:

1 The Socket API Unit – 4 4.1 Background 4.2 The Socket Metaphor In IPC
4.3 The Datagram Socket API 4.4 The Stream-Mode Socket API 4.5 Socket With Non-blocking I/O Operation 4.6 Secure Socket API By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

2 Introduction The Socket API is the first programming facility for implementing inter-process communication. The Socket API is a mechanism that provides a low level of abstraction for IPC. Its simplicity is why it is presented at this point. Although application programmers seldom have to code at this level, the understanding of socket API is important for at least two reasons. First, the upper-layer facilities are built on top of the socket API. That is they are implemented using the operations provided in the socket API. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

3 Second, for applications that place a premium on response time or that run on a platform with limited resources, the socket API may be the most appropriate, or may even be the only available, facility for IPC. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

4 4.1 Background The Socket API first appeared in the early 1980s as a program library in a version of the UNIX operating system known as Berkeley Unix (BSD 4.2) to provide the functionalities for IPC. Today Socket API is supported on all major operating systems. On unix based system such as BSD or Linux, the API is part of the kernel, or core, of the operating system. On personal computer operating system such as MS-DOS, windows NT (and its variants), Mac-OS, and OS/2, the WinSock). By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

5 “Socket is a term borrowed from telephone communications”
Java, a language designed with network programming in mind, provides the socket API as part of the language’s core classes. These APIs all share the same message-passing model and very similar syntax. “Socket is a term borrowed from telephone communications” By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

6 4.2 The Socket Metaphor in IPC
Borrowing from the terminology of telephony the designer of the socket API has provided a programming construct termed a Socket. A process wishing to communicate with another process must create an instance of such a construct. Unlike early telephone, however, the communication between the parties may be connection-oriented or connectionless. For clarity, we will present the connectionless socket API first. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

7 The conceptual model of the socket API
Process A Process B The conceptual model of the socket API By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

8 4.3 The Datagram Socket API
The USER DATAGRAM PROTOCOL (UDP) allows a packet to be transported (that to be sent or received at the transport layer) using connectionless communication. The data packet thus transported is called a DATAGRAM. The TRANSMISSION CONTROL PROTOCOL (TCP) is connection-oriented and transports a stream of data over a logical connection established between the sender and the receiver. The Java socket API, as with all other socket APIs, provides socket programming constructs that make use of the UDP or TCP protocol. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

9 Socket that use UDP for transport are known as DATAGRAM SCOKETS, while sockets that use TCP are termed stream sockets. Because of their relative simplicity, we will first look at datagram sockets. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

10 4.3.1 The Connectionless Datagram Socket
Datagram sockets can support both connectionless and connection-oriented communication at the application layer. This is so because even though datagrams are sent or received without the notion of connections at the transport layer, The run time support of the socket API can create and maintain logical connections for datagrams exchanged between two process. In Java, two classes are provided for the datagram socket API: The DatagramSocket class for the sockets. The Datagrampacket class for the datagrams exchanged. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

11 A process wishing to send or receive data using this API must instantiate a Datagram Socket object, or a socket in short. Each socket is said to be bound to a UDP port of the machine that is local to the process. To send a datagram to another process, a process must create an object that represents the datagram itself. This object can be created by instantiating a Datagram-Packet object that carries (1) a reference to a byte array that contains the payload data, and (2) the destination address (the host ID and port number to which the receiver’s socket is bound, h and p in the case.) By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

12 Specifying a reference to the Datagram-Packet object as an argument.
Once the Datagram-Packet object is created and loaded with payload data and destination, the sending process then invokes a call to the send method in the Datagram-Socket object, Specifying a reference to the Datagram-Packet object as an argument. At the receiving process; a Datagram-Socket object must also be instantiated and bound to a local port; the port number must agree with that specified in the datagram packet of the sender. To receive datagram sent to the socket, the process creates a Datagram –Packet object that references a byte array and calls a receive method in its Datagram-Socket object, specifying as argument a reference to the Datagram-Packet object. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

13 Transport Layer Software Transport Layer Software
Socket API runtime Support Process A Process B Socket API runtime Support Transport Layer Software Transport Layer Software Connectionless Datagram Socket Socket API runtime Support Process A Process B Socket API runtime Support Transport Layer Software Transport Layer Software Connection-Oriented Datagram Socket By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions A datagram

14 4.3.2 Connection-Oriented Datagram Socket API
The connection provided by this API is rudimentary and typically insufficient for applications. A connection is made for a socket by specifying the terminating a connection. Once such a connection is made, the socket is dedicated to exchanging datagram packets with the remote socket. On a send operation, if the datagram’s address does not match the socket address at the other end, an IllegalArgumentException will be thrown. If data is sent to the socket from a source other than the connection remote socket, that data will be ignored. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

15 Thus once a connection is attached to a datagram socket, that will not be available for communication with any other socket until the connection is terminated. Note that connection is unilateral; that is, it is enforced on only one side. The socket on the other side is free to send data and receive data from other sockets, unless it commits to a connection to this socket. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

16 Following method calls for a connection-oriented datagram socket.
void connect(InetAddress address, int port) : Creates a logical connection between this socket and a socket at the remote address and port void disconnect(): Terminates the current connection, if any, from this socket. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

17 4.4 The Stream Mode Socket API
Whereas the datagram socket API supports the exchange of discrete units of data (that is, datagrams) The stream-mode socket API provides a model of data transfer based on the Stream-Mode I/O of the Unix Operating System. By definition, a stream-mode socket supports connection- oriented communication only. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

18 Using a stream-mode socket for data transfer
P2 P1 a data stream Using a stream-mode socket for data transfer By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

19 In stream-mode input-output, data is transferred using the concept of a continuous data stream flowing from a source to a destination (also called a sink). Data is inserted, or written, into a stream by a process that controls the source, and data is extracted, or read, from the stream by a process attached to the destination. Note that continuous nature of a stream allows data to be inserted and extracted from the stream at different rates. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

20 The units of data written and read need not match.
Write a data stream Stream-mode I/O The units of data written and read need not match. For example, 100 bytes of data written using one write operation can be read using an operation reading 20 bytes, followed by another read operation using 80 bytes By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

21 A connection between the sockets is then formed.
The stream-mode socket API is an extension of the stream- mode I/O model. Using the API, each of two processes individually creates a stream mode socket. A connection between the sockets is then formed. Data, as a read by the receiver via its socket. This is similar to the connection-oriented datagram socket API that we have already studied, except for the difference in the discrete nature of the data transported in datagram sockets. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

22 The Stream-mode socket API
Client 1 Server Connection socket Client 2 Data socket The Stream-mode socket API By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

23 The syntax of the API is interwined with the client-server paradigm.
In java, the stream-mode socket API is provided with two classes: serverSocket and Socket. The term Server comes from the client-server paradigm, for which the API was designed. The syntax of the API is interwined with the client-server paradigm. With the stream mode socket API, there are two types of sockets: The first type of sockets, provided via the ServerSocket class, is for accepting connections. (connection sockets) The other type of sockets, provided via the Socket class, is for data exchange. (data sockets) By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

24 Connection requests are accepted one at a time.
Using this API, a process known as the server establishes a connection sockets and then listen for connection requests from other processes. Connection requests are accepted one at a time. When a connection is accepted, a data socket is created for the connection. Through the data socket, the server process can read from and/or write to the data stream. When the communication session between the two process is over, the data socket is closed, and the server is free to accept the next connection request via the connection socket. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

25 When the communication session between the two processes.
A process that wishes to communicate with the server is known as a client. A client creates a socket; then, via the server’s connection socket, it makes a request to the server for a connection. Once the request is accepted, the client’s socket is connected to the server for a connection. Once the request is accepted, the client’s socket is connected to the server’s data socket so that the client may proceed to read from and/or write to the data stream. When the communication session between the two processes. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

26 Operations and Event Synchronization
There are two main classes in the stream socket API: the ServerSocket class and the Socket class. The ServerSocket class is the establishment of connections, while the Socket class is for the transfer of data. Methods of ServerSocket (Connection Socket) and Socket (data socket) class are as per the Table 4.4 and 4.5 Accept (accepting a connection). If no request is waiting, the server process will be suspended until a request for connection arrives. Reading from the input stream associated with a data socket. If the amount of data requested is not currently present in the data stream, the reading process will be blocked until a sufficient amount of data has been written into the data stream. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

27 4.5 Sockets with Non-blocking I/O Operation
The APIs we have seen provides Asynchronous (non- blocking) send operations and synchronous (blocking) receive operations. Using these APIs, a process that reads from a socket is subject to blocking. To maximize concurrency, threads can be used so that a read operation is performed in a writing thread while a separate thread remains active for processing other tasks. However, in some applications that require extensive use of threads, the overhead incurred can be harmful to the performance or, worse yet, the viability, of the application. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

28 Alternatively there are socket APIs that provide non-blocking I/O operations.
Using such an API, neither send nor receive will result in blocking, it is necessary for the receiving process to use a listener to be notified of the arrival of the data. Asynchronous socket are available with Winsock, and, as of version 1.4 java also provides a new I/O package, java.nio (NIO),that offers sockets with non-blocking I/O operations. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

29 4.6 Secure Socket API When we are working with Socket API, we should be aware that secure socket APIs exist. Which are socket APIs enhanced with data security measures. Using conventional socket APIs, data is transmitted as bit streams over network links. The bit stream, if intercepted by means of tools such as network links. The bit streams, if intercepted by means of tools such as network protocol analyzers, can be decoded by someone. who has knowledge of the data representation of the data exchanged. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

30 Hence, it is risky to use sockets to transmit sensitive data, such as credit information and authentication data. To address the problems, protocols have been introduced to secure the data transmitted using socket APIs. Some of the well-known protocols are described in the following paragraph. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

31 4.6.1 The Secure Socket Layer
Secure Sockets Layer (SSL) was a protocol developed by the Netscape Communication Corporation for transmitting private documents over the internet. An SSL API has methods of function similar to the socket API, Except that data is encrypted before it is transmitted over an SSL Connection. SSL is supported by modern browsers. When run with the SSL protocol selected, these browser will transmit encrypted data using the SSL Socket API. Many web sites also use the protocol to obtain confidential user information, such as credit card numbers. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

32 By convention, the URL of a web page that require an SSL.
Connection starts with https: instead of http: By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions

33 4.6.2 The Java Secure Socket Extension
The Java secure socket extension (JSSE) is a set of java packages that enable secure internet communication. It implements a version of SSL and TLS (Transport Layer Security) protocols and includes functionalities for data encryption, server authentication, message integrity, and optional client authentication. Using JSSE, developers can provide for the secure passage of data between two processes. The JSEE API features syntax similar to the Java connection- oriented socket API. By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions


Download ppt "The Socket API Unit – Background 4.2 The Socket Metaphor In IPC"

Similar presentations


Ads by Google