2 What Is Socket A socket is one endpoint of a two-way communication link between two programs running on the network.A socket is bound to a port number so that the TCP layercan identify the application that data is destined to be sent.A socket in use usually has an address bound to it. Thenature of the address depends on the communicationdomain of the socket.A single socket can communicate in only one domain.
3 Where Is Socket From?Originally developed in 4.2BSD (Berkeley SoftwareDistribution), cka Berkeley UNIX.In UNIX, a process has a set of I/O descriptors that onereads from and writes to. These descriptors may refer tocommunication channels (sockets).The lifetime of a descriptor is made up of three phases:creation (open socket), reading and writing (receive andsend to socket), and destruction (close socket).
4 Three Major Protocols Used For Socket Internet Protocol (IP): a low-level routing protocol thatbreaks data into small packets and sends them to anaddress across a network, which is not reliable.Transmission Control Protocol (TCP): a higher-levelprotocol that strings together the packets, routing andretransmitting them as necessary to reliably transmit yourdata.User Datagram Protocol (UDP): sits next to TCP and canbe used directly to support fast, connectionless, unreliabletransport of packets.
6 Java Socket Support Three types of sockets in Java The Java environment implements portable socket objects by stacking several object layers on top of low-level, platform-specific procedures.Three types of sockets in JavaTCP/IP Socket (supported by Socket and SeverSocket classes)UDP Socket (supported by DatagramSocket class)Multicast Socket (supported by MulticastSocket class)
7 TCP/IP Socket in Java TCP/IP sockets are used to implement reliable, bi-directional, persistent, point-to-point, stream-based connections between hosts on the InternetUseful for implementing network services –such as remote login (telnet, rlogin) and filetransfer (FTP) – which require data ofindefinite length to be transferred.
8 blocks until connection from client TCP/IP ServerFigure 1 Socket function for TCP client-serverServerSocketwell-known portbindlistenTCP/IP ClientacceptSocketblocks until connection from clientconnection establishmentconnect(TCP three-way handshake)writeReaddata (request)process requestdata (reply)readWriteend-of-file notificationreadClose Conn.Close Conn.
9 Seven Basic Operations Connect to a remote machineSend dataReceive dataClose a connectionListen for incoming dataBind to portNeeded only by the serverAccept connections from remote machineson the bound port
10 Socket Class -Characteristics Socket class is Java’s fundamental class for performingclient-side TCP operation.This class is used for creating TCP connections over anIP networkThis class itself uses native code to communicate with thelocal TCP stack of the host operating systemThe interface that the Socket class provides to theprogrammer is streams
11 Socket Class -Constructors Four public constructors: Public Socket(String host, int port) throws UnknownHostException, IOExceptionPublic Socket(InetAddress host, int port) throws IOExceptionPublic Socket(String host, int port, InetAddress interface, int localPort) throws IOExceptionPublic Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOExceptionTwo protected constructorsProtected Socket()Protected Socket(SocketImpl impl)
12 Socket Class -How does it work? Step one Step two The creation of a Socket object implicitly establishes a connection between the client and server.ServerportConnection requestClientStep twoServerportClientportconnectionport
13 ServerSocket ClassThe SeverSocket is designed to be a “listener”, whichwaits for clients to connect before doing anythingServer sockets wait for connections while client socketsinitiate connectionsListen for either local or remote client programs to connectto them on published ports.
14 How many request I can hold? ServerSocket Class-ConstructorsThree Public ConstructorsServerSocket(int port),ServerSocket(int port, int maxQueue)ServerSocket(int port, int maxQueue, InetAddress localAddress)How many request I can hold?
15 UDP Socket in Java Less complex Incurs fewer overheads Used in bandwidth-limited applications, wherethe overhead associated with resending packetsis not tolerable. A good example: real-timenetwork audio applications.Implemented by DatagramPacket class &DatagramSocket class
16 blocks until datagram received from a client UDP SeverSocket()bindrecvfromUDP ClientSocket()blocks until datagram received from a clientdata (request)sendtoprocess requestdata (reply)sendtorecvfromcloseSocket functions for UDP client-server
17 The Two Major Classes DatagramPacket class DatagramSocket Class Each DatagramPacket contains a data buffer, the address of the remote host to send the data to, and the port number the remote agent is listening toDatagramSocket ClassIn order for two agents to talk to each other over a UDP connection, a DatagramSocket object is created to ensure they both connected to a port on their local machines.
18 How does it work?The DatagramPacket class stuffs bytes of data into UDPpackets called datagrams and lets you unstuffy datagramsthat you receiveA DatagramSocket sends and receives data using UDPpackets, represented as DatagramPacket objectsThe remote process can receive the data in the form of aDatagramPacket by calling the receive() method on itsDatagramSocket
19 Multicast SocketsMulticasting sends data from one host to many differenthosts, but not to everyone; the data goes only to clientsthat have expressed and interest in the data by joining aparticular multicast groupOn the Internet, such event like video conference, is bestimplemented using Multicasting that built on top of UDPMulticast Sockets in Java uses the DatagramPacket classalong with a new MulticastSocket class.
20 With Multicast Sockets Without Multicast Sockets ClientRouterThe InternetRouterServerthe simplest possible multicast configurations
21 Why need Multicasting? The growing requirement All the unicast sockets provide point-to-point communication and create a connection with two well-defined endpoints. However, many tasks require a different model. For example, the television station need to broadcast to every user and Video conferencing, by contrast, sends an audio-video feed to a select group of people using multicast. Multicasting is broader than unicast, but narrower and more targeted than broadcast communication.The resources saving and the traffic reliefThe goal of multicast sockets is that no matter how complex the network, the same data should never be sent more than once over any given network segment
22 How to do it? Step one Step two Step three Create a MulticastSocket object and have the socket join a multicast groupStep twoStuff the address of the multicast group in the DatagramPacket you want to send.Step threeThe routers and the MulticastSocket class take care of the rest.
23 Four Key OperationsThe behavior of MulticastSocket is very similar to DatagramSocket’sJoin a multicast groupSend data to the member of the group3. Receive data from the group4. Leave the group
24 Problems Performance Security Complexity Restriction on multicast socket