unit – 4 4.1 background 4.2 the socket metaphor in ipc 4.3 the datagram socket api 4.4 the...

Click here to load reader

Upload: alberta-hunt

Post on 01-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

The Socket API

Unit 44.1 Background4.2 The Socket Metaphor In IPC4.3 The Datagram Socket API4.4 The Stream-Mode Socket API4.5 Socket With Non-blocking I/O Operation4.6 Secure Socket API

The Socket APIBy Asst. Prof. Priyank Gokani from SunShine Group Of InstitutionsIntroductionThe 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 InstitutionsSecond, 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 Institutions4.1 BackgroundThe 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 InstitutionsJava, a language designed with network programming in mind, provides the socket API as part of the languages core classes.These APIs all share the same message-passing model and very similar syntax.Socket is a term borrowed from telephone communicationsBy Asst. Prof. Priyank Gokani from SunShine Group Of Institutions4.2 The Socket Metaphor in IPCBorrowing 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 InstitutionsProcess AProcess BThe conceptual model of the socket APIBy Asst. Prof. Priyank Gokani from SunShine Group Of Institutions4.3 The Datagram Socket APIThe 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 InstitutionsSocket 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 Institutions4.3.1 The Connectionless Datagram SocketDatagram 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 InstitutionsA 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 receivers socket is bound, h and p in the case.)By Asst. Prof. Priyank Gokani from SunShine Group Of InstitutionsOnce 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 InstitutionsTransport Layer SoftwareProcess ASocket API runtime SupportTransport Layer SoftwareProcess BSocket API runtime SupportConnectionless Datagram SocketTransport Layer SoftwareProcess ASocket API runtime SupportTransport Layer SoftwareProcess BSocket API runtime SupportConnection-Oriented Datagram SocketA datagramBy Asst. Prof. Priyank Gokani from SunShine Group Of Institutions4.3.2 Connection-Oriented Datagram Socket APIThe 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 datagrams 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 InstitutionsThus 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 InstitutionsFollowing 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 portvoid disconnect(): Terminates the current connection, if any, from this socket.By Asst. Prof. Priyank Gokani from SunShine Group Of Institutions4.4 The Stream Mode Socket APIWhereas 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 InstitutionsP1P2a data streamUsing a stream-mode socket for data transferBy Asst. Prof. Priyank Gokani from SunShine Group Of InstitutionsIn 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 Institutionsa data streamReadWrite 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 bytesStream-mode I/OBy Asst. Prof. Priyank Gokani from SunShine Group Of InstitutionsThe 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 InstitutionsServerClient 1Client 2Connection socketData socketThe Stream-mode socket APIBy Asst. Prof. Priyank Gokani from SunShine Group Of InstitutionsIn 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 InstitutionsUsing 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 InstitutionsA process that wishes to communicate with the server is known as a client.A client creates a socket; then, via the servers connection socket, it makes a request to the server for a connection.Once the request is accepted, the clients socket is connected to the server for a connection.Once the request is accepted, the clients socket is connected to the servers 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 InstitutionsOperations and Event SynchronizationThere 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.5Accept (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 Institutions4.5 Sockets with Non-blocking I/O OperationThe 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 InstitutionsAlternatively 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 Institutions4.6 Secure Socket APIWhen 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 InstitutionsHence, 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 Institutions4.6.1 The Secure Socket LayerSecure 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 InstitutionsBy 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 Institutions4.6.2 The Java Secure Socket ExtensionThe 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