{"id":205,"date":"2018-07-27T04:26:58","date_gmt":"2018-07-27T04:26:58","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=205"},"modified":"2018-08-02T07:12:53","modified_gmt":"2018-08-02T07:12:53","slug":"java-udp-socket-programming","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/java-udp-socket-programming\/","title":{"rendered":"Java UDP Socket Programming"},"content":{"raw":"<div>\r\n\r\n<strong>\u00a0 \u00a0 UDP communication<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">UDP (User Datagram Protocol) delivers independent packages whose arrival and the order of arrival is not guaranteed. Packets sent by UDP protocol are called as Datagrams. A DataGram is an independent, self-contained message sent over the network whose arrival, and content are not guaranteed.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Classes in Java that are supported to develop for UDP connection are DatagramPacket, DatagramSocket and MulticastSocket etc.<\/p>\r\n&nbsp;\r\n\r\n<strong>Datagram Sockets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">DatagramSocket defines four public constructors. They are,<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.\u00a0 \u00a0DatagramSocket( ) throws SocketException<\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">This constructor creates a DatagramSocket bound to any unused port on the local computer.<\/span><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a02.\u00a0 DatagramSocket(int <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketException<\/span><\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">This constructor creates a DatagramSocket bound to the port specified by <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 3. DatagramSocket(int <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">, InetAddress <\/span><em style=\"text-align: initial;font-size: 1em\">ipAddress<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketExce ption<\/span><\/p>\r\n\r\n<ul style=\"text-align: justify\">\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">This constructor constructs a DatagramSocket bound to the specified port and InetAddress.<\/span><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a04. DatagramSocket(SocketAddress <\/span><em style=\"text-align: initial;font-size: 1em\">address<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketException<\/span><\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This constructor constructs a DatagramSocket bound to the specified SocketAddress.<\/span><\/li>\r\n \t<li style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">SocketAddress is an abstract class that is implemented by the concrete class InetSocketAddress.<\/span><\/li>\r\n<\/ul>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Create UDP Socket<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To develop UDP socket communication, the client uses a constructor that does not require a port number. This constructor just binds the DatagramSocket to any available local port. It doesn't matter what port the client is connected to because the DatagramPackets will contain the addressing information. The server gets the port number from the DatagramPackets and send its response to that port.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Methods of Datagram Socket<\/strong>\r\n\r\n&nbsp;\r\n\r\nDatagramSocket defines many methods. Two of the most important are send( ) and receive( ).\r\n<ul>\r\n \t<li>void send(DatagramPacket <em>packet<\/em>) throws IOException<\/li>\r\n \t<li>void receive(DatagramPacket <em>packet<\/em>) throws IOException<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 1. The send( ) method sends packet to the port specified by <em>packet<\/em>.<\/p>\r\n<p style=\"text-align: justify\">2. The receive() method waits for a packet to be received from the port specified by <em>packet<\/em> and returns the result.<\/p>\r\n&nbsp;\r\n\r\n<strong>DatagramPacket class<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>The Constructors of DatagramPacket are listed below,<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 DatagramPacket(byte buf[], int offset, int length)\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 This constructor constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">2. DatagramPacket(byte buf[], int length)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 This constructor constructs a DatagramPacket for receiving packets of length length.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3. DatagramPacket(byte buf[], int offset, int length, InetAddress address, int port).<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">4. DatagramPacket(byte buf[], int offset, int length, SocketAddress address)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 This constructor constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">5. DatagramPacket(byte buf[], int length, InetAddress address, int port)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length to the specified port number on the specified host.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">6.DatagramPacket(byte buf[], int length, SocketAddress address)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length to the specified port number on the specified host.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Methods of DatagramPacket<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1.\u00a0 void setAddress(InetAddress iaddr)<\/p>\r\n<p style=\"text-align: justify\">Sets the IP address of the machine to which this datagram is being sent.<\/p>\r\n<p style=\"text-align: justify\">2. InetAddress getAddress()<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0Returns the IP address of the machine to\u00a0 which this datagram is being sent or from which the datagram was received.<\/p>\r\n<p style=\"text-align: justify\">3. void setSocketAddress(SocketAddress address)<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.<\/p>\r\n<p style=\"text-align: justify\">4. SocketAddress getSocketAddress()<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.<\/p>\r\n<p style=\"text-align: justify\">5. void setPort(int iport)<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 Sets the port number on the remote host to which this datagram is being sent.<\/p>\r\n<p style=\"text-align: justify\">6. int getPort()<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">7. byte[] getData()<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the data buffer.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">8.int getOffset()<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 Returns the offset of the data to be sent or the offset of the data received.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">9.int getLength()<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the length of the data to be sent or the length of the data received.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">10. void setData(byte[] buf, int offset, int length)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 Set the data buffer for this packet.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">11. void setData(byte[] buf)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Set the data buffer for this packet.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">12. void setLength(int length)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Set the length for this packet.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Example of ChatUDP Server and ChatUDP Client<\/strong><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0An example is illustrated for chat client server communication with UDP sockets.\r\n\r\n&nbsp;\r\n\r\n<strong>ChatUDP Server<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Datagram socket is created and binded with the port number 6000 on the localhost machine.A Datagram Packet 'dp1' is constructed to receive datagrams sent from the client. Once the datagram is received using the method receive(dp1), the byte data is obtained using dp1.getData() method. Then the bytes are converted to String and displayed on the screen.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Message from the user is again read as a String which is then converted to bytes using s.getBytes() method. A DatagramPacket 'dp2' is then constructed with the buffer data and sent to the specified port number extracted from the received datagram 'dp1'using the method dp1.getPort() and to the specified host extracted from received datagram 'dp1' using the method dp1.getAddress(). This whole chat runs in a loop until the user enters a string 'exit'.<\/p>\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 import java.io.*;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 import java.net.*;\u00a0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0public class ChatUDPServer1<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 \u00a0{<\/span>\r\n<div>\r\n\r\n\u00a0 \u00a0 public static void main(String args[]) throws Exception\r\n\r\n{\r\n\r\nDatagramSocket ds = new DatagramSocket(6000); byte[] b;\r\n\r\nDatagramPacket dp1,dp2;\r\n\r\nString s;\r\n\r\nBufferedReader br=new BufferedReader(new InputStreamReader(System.in)); b = new byte[1024];\r\n\r\ndo\r\n\r\n{\r\n\r\ndp1 = new DatagramPacket(b, b.length);\r\n\r\nds.receive(dp1);\r\n\r\nb=dp1.getData();\r\n\r\n\/* Don't use s=new String(b,0,0,b.length);-----is wrong. Use only dp1.getLength() *\/\r\n\r\n\/\/s=new String(b,0,0,dp1.getLength());\r\n\r\ns = new String(b);\r\n\r\nSystem.out.println(s);\r\n\r\nif(s.equals(\"exit\"))\r\n\r\nbreak;\r\n\r\ns=br.readLine();\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">\u00a0 \u00a0b=s.getBytes();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0dp2 = new DatagramPacket(b, s.length(), dp1.getAddress(),dp1.getPort());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 ds.send(dp2);\u00a0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 }while(!s.equals(\"exit\"));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 }<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0<\/strong>\r\n\r\n<strong> ChatUDPClient<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Datagram socket is created and binded with the available port number on the localhost machine. Message from the user is read as a string which is then converted to bytes using s.getBytes() method. A DatagramPacket 'dp1' is then constructed with the buffer data and sent to the specified port number 6000 and on the specified host given as local host where the server is running.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Datagram Packet 'dp2' is constructed to receive datagrams sent from the server. Once the datagram is received using ds.receive(dp2) the byte data is obtained using dp2.getData() method. Then the bytes are converted to string and displayed on the screen. This whole chat runs in a loop until the user enters a string 'exit'.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\npublic class ChatUDPClient1\r\n\r\n{\r\n\r\npublic static void main(String args[]) throws Exception\r\n\r\n{\r\n\r\nDatagramSocket ds = new DatagramSocket();\r\n\r\nDatagramPacket dp1,dp2;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">byte b[];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String s;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(s.equals(\"exit\"))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0\/<\/span><span style=\"text-align: initial;font-size: 1em\">* To test whether the packet contains only the required data (i.e., not empty spaces)*\/ \/* Use\u00a0 \u00a0 b=dp1.getData();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0System.out.println(new String(b,0,0,dp1.getLength()));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">do<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">*\/<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s=br.readLine();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">b=new byte[1024];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">b=s.getBytes();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">dp1=new DatagramPacket(b, s.length(), InetAddress.getByName(\"localhost\"), 6000); ds.send(dp1);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">dp2=new DatagramPacket(b, b.length);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ds.receive(dp2);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">b=dp2.getData();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">s=new String(b);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(s);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}while(!s.equals(\"exit\"));<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Output of ChatUDP Server &amp; Client<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nThe output of Chat UDP server and Chat UDP client is shown in Figure 23.1.\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-206 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120.png\" alt=\"\" width=\"605\" height=\"240\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure: 23.1 Output of ChatUDP Server &amp; ChatUDP Client<\/strong><\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Multicast Communication<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Multicasting sends data from one host to many different hosts, but not to everyone. The data goes only to clients that have expressed interest in the data by joining a particular multicast group. Multicast Sockets in Java uses the DatagramPacket class along with a new MulticastSocket class. The IETF has set aside addresses from 224.0.0.1 to 239.255.255.255 specifically for multicasting.<\/p>\r\n&nbsp;\r\n\r\n<strong>Multicast Sockets<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Four Key Operations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The behavior of MulticastSocket is very similar to DatagramSocket\u2019s. We can use a normal DatagramSocket to send and receive unicast and broadcast datagrams and to send multicast datagrams but in order to receive multicast datagrams, we need a MulticastSocket. The four key operations while performing multicast communication is listed below,<\/p>\r\n&nbsp;\r\n\r\n1.\u00a0 Join a multicast group\r\n\r\n2. Send data to the member of the group\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">3. Receive data from the group<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">4.\u00a0 Leave the group\u00a0<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0java.net.MulticastSocket<\/strong>\r\n\r\n&nbsp;\r\n\r\nIt is the subclass of java.net.DatagramSocket and constructed in the same way.\r\n\r\n&nbsp;\r\n\r\n<strong>Some extra methods used here are:<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 void joinGroup(InetAddress mcastGroup)\r\n\r\nEnter the specifies group so that you can send or receive datagrams\r\n\r\n2. void leaveGroup(InetAddress mcastGroup)\r\n\r\nLeave a group that you previously joined\r\n\r\n3. void setTimeToLive(int ttl)\r\n\r\nSets how far your datagrams will travel before routers ignore them\r\n\r\n4. int getTimeToLive()\r\n\r\nGet the default time-to-live for multicast packets sent out on the socket.\r\n\r\n&nbsp;\r\n\r\n<strong>MulticastSender<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>The steps involved in sending datagrams through Multicast Socket are,<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Create the MulticastSocket.\r\n\r\n2. Join the multicast group(s) (on startup).\r\n\r\n3. Create the DatagramPacket.\r\n\r\n4. Send the packet through the socket.\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">5. Leave the multicast group (on exit).<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0Snippet code to demonstrate the above steps to send datagrams through Multicast socket.<\/strong>\r\n\r\n&nbsp;\r\n\r\nInetAddress multicastGroup =\u00a0 InetAddress.getByName(multicastGroupAddr);\r\n\r\nMulticastSocket socket = new MulticastSocket();\r\n\r\nsocket.joinGroup(multicastGroup);\r\n\r\nsocket.setTimeToLive(5);\r\n\r\nbyte[] data = \u201cThis is the message\u201d.getBytes();\r\n\r\nDatagramPacket datagram = new DatagramPacket(data, data.length);\r\n\r\ndatagram.setAddress(multicastGroup);\r\n\r\ndatagram.setPort(1234);\r\n\r\nsocket.send(datagram);\r\n\r\nsocket.leaveGroup(multicastGroup);\r\n\r\n&nbsp;\r\n\r\n<strong>MulticastReceiver<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>The steps involved in receiving datagrams through Multicast Socket are,<\/strong>\r\n\r\n&nbsp;\r\n\r\n1. Create a multicast socket on an <em>agreed<\/em> port.\r\n\r\n2. Join the multicast group (on startup).\r\n\r\n3. Create an empty datagram.\r\n\r\n4. Wait for datagram to be delivered on the socket.\r\n\r\n5. Unpack and use the datagram.\r\n\r\n6. Leave the multicast group (on exit).\r\n\r\n<\/div>\r\n<strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Snippet code to demonstrate the above steps to receive datagrams through Multicast socket.<\/strong>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0InetAddress multicastGroup =\u00a0 InetAddress.getByName(multicastGroupAddr);\r\n\r\nMulticastSocket socket = new MulticastSocket(1234);\r\n\r\nsocket.joinGroup(multicastGroup);\r\n\r\nbyte[] data = new byte[1000];\r\n\r\nDatagramPacket packet = new DatagramPacket(data, data.length); socket.receive(packet);\r\n\r\nString message = new String(packet.getData(), 0, packet.getLength()); socket.leaveGroup(multicastGroup);\r\n\r\n&nbsp;\r\n\r\n<strong>Example of MulticastGroup<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Initially a multicast socket is created on an <em>agreed<\/em> port. To receive the multicast messages one has to join the multicast group (on startup) using the method ds.joinGroup(group). An empty datagram is then created to receive the message. Then the client waits for datagram to be delivered on the socket. The datagram is then unpacked and used. Finally it leaves the multicast group on exit.<\/p>\r\n&nbsp;\r\n\r\nimport java.io.*;\r\n\r\nimport java.net.*;\r\n\r\npublic class MulticastGroup\r\n\r\n{\r\n\r\npublic static void main(String args[])\r\n\r\n{\r\n\r\nInetAddress group = null;\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">int port = 0;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">group = InetAddress.getByName(args[0]);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">port = Integer.parseInt(args[1]);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch( Exce ption e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.err.println(\"usage\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.exit(1);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">MulticastSocket ds = null;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ds = new MulticastSocket(port);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ds.joinGroup(group);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">byte[] buffer = new byte[8192];<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0 while(true)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0\u00a0 {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ds.receive(dp);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String s = new String(dp.getData());<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(s);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0 }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(IOException e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.err.println(e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">finally<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(ds != null)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">try{ ds.leaveGroup(group);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ds.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(IOException e){}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>Example of Multicast Sender<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Initially the Multicast IP address 228.5.6.7 and port number 8759 are given as command line arguments. Datagram packet is constructed to the given IP address and port number. MulticastSocket is then created using the constructor of the class. The Multicast sender is joined in the the multicast group(s) using the method ms.joinGroup(group). The created DatagramPacket is then sent through the socket. Finally it leaves the multicast group on exit.<\/p>\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 \u00a0import java.io.*;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0import java.net.*;<\/span>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0public class MulticastSender\r\n\r\n{\r\n\r\npublic static void main(String[] args)\r\n\r\n{\r\n\r\nInetAddress group = null;\r\n\r\nint port = 0;\r\n\r\ntry\r\n\r\n{\r\n\r\ngroup = InetAddress.getByName(args[0]);\r\n\r\nport = Integer.parseInt(args[1]);\r\n\r\n}\r\n\r\ncatch( Exception e)\r\n\r\n{\r\n\r\nSystem.err.println(e);\r\n\r\nSystem.exit(1);\r\n\r\n}\r\n\r\nbyte[] data = \"here multicast data\".getBytes();\r\n\r\nDatagramPacket dp = new DatagramPacket(data, data.length, group, port); try{\r\n\r\nMulticastSocket ms = new MulticastSocket();\r\n\r\nms.joinGroup(group);\r\n\r\nms.send(dp);\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">ms.leaveGroup(group);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">ms.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(SocketException se)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.err.println(se);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">catch(IOException e)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.err.println(e);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">}\u00a0 }<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Output of Multicast Sockets<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The output of Multicast communication is shown in Figure 23.2. There are four entities, one act as a multicast sender and the other three are multicast clients who receive the message from the sender. All the four entities has to join the multicast group for communication.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121.png\" alt=\"\" width=\"635\" height=\"306\" \/><\/p>\r\n<p style=\"text-align: center\"><strong>Figure 23.2 Multicast communication<\/strong><\/p>\r\n&nbsp;\r\n\r\n<strong>TCP Socket Options<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Several methods can be used to set various socket options. Most of the time the defaults are fine.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. public void setTcpNoDelay(boolean on) throws SocketException<\/p>\r\n<p style=\"text-align: justify\">2. public boolean getTcpNoDelay() throws SocketException<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 Disable Nagle's algorithm. Valid for (client) Sockets.<\/p>\r\n<p style=\"text-align: justify\">3. public void setSoLinger(boolean on, int val) throws SocketException<\/p>\r\n<p style=\"text-align: justify\">4. public int getSoLinger() throws SocketException<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 Specify a linger-on-close timeout. Valid for (client) Sockets.<\/p>\r\n<p style=\"text-align: justify\">5. public synchronized void setSoTimeout(int timeout) throws SocketException<\/p>\r\n<p style=\"text-align: justify\">6. public synchronized int getSoTimeout() throws SocketException<\/p>\r\n<p style=\"text-align: justify\">\u00a0 \u00a0 Specify a timeout on blocking socket operations. Valid for all sockets like Socket, ServerSocket, DatagramSocket.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module explains about UDP socket communication in Java. This module gives an idea about DatagramPacket and DatagramSocket classes to build UDP communication. This module also explores about Multicasting with UDP sockets.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>References<\/strong>\r\n<ol>\r\n \t<li><em>download.java.net\/jdk7\/archive\/b123\/docs\/api\/java\/net\/<strong>DatagramPacket<\/strong>.html<\/em><\/li>\r\n \t<li><em style=\"text-align: initial;font-size: 1em\">https:\/\/www.cs.auckland.ac.nz\/~jmor159\/364\/<strong>ppt<\/strong>\/A<strong>Java<\/strong>Networking.<strong>ppt<\/strong><\/em><\/li>\r\n<\/ol>\r\n<strong>\u00a0 \u00a0Web Links<\/strong>\r\n<ul>\r\n \t<li>Herbert Schildt, \" <em>Java: The Complete Reference\", 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\r\n \t<li><em>https:\/\/www.cs.auckland.ac.nz\/~jmor159\/364\/<strong>ppt<\/strong>\/A<strong>Java<\/strong>Networking.<strong>ppt<\/strong><\/em><\/li>\r\n \t<li><em>download.java.net\/jdk7\/archive\/b123\/docs\/api\/java\/net\/<strong>DatagramPacket<\/strong>.html<\/em><\/li>\r\n<\/ul>","rendered":"<div>\n<p><strong>\u00a0 \u00a0 UDP communication<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">UDP (User Datagram Protocol) delivers independent packages whose arrival and the order of arrival is not guaranteed. Packets sent by UDP protocol are called as Datagrams. A DataGram is an independent, self-contained message sent over the network whose arrival, and content are not guaranteed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Classes in Java that are supported to develop for UDP connection are DatagramPacket, DatagramSocket and MulticastSocket etc.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Datagram Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">DatagramSocket defines four public constructors. They are,<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.\u00a0 \u00a0DatagramSocket( ) throws SocketException<\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"text-align: initial;font-size: 1em\">This constructor creates a DatagramSocket bound to any unused port on the local computer.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a02.\u00a0 DatagramSocket(int <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketException<\/span><\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"text-align: initial;font-size: 1em\">This constructor creates a DatagramSocket bound to the port specified by <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 3. DatagramSocket(int <\/span><em style=\"text-align: initial;font-size: 1em\">port<\/em><span style=\"text-align: initial;font-size: 1em\">, InetAddress <\/span><em style=\"text-align: initial;font-size: 1em\">ipAddress<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketExce ption<\/span><\/p>\n<ul style=\"text-align: justify\">\n<li><span style=\"text-align: initial;font-size: 1em\">This constructor constructs a DatagramSocket bound to the specified port and InetAddress.<\/span><\/li>\n<\/ul>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a04. DatagramSocket(SocketAddress <\/span><em style=\"text-align: initial;font-size: 1em\">address<\/em><span style=\"text-align: initial;font-size: 1em\">) throws SocketException<\/span><\/p>\n<ul>\n<li style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">This constructor constructs a DatagramSocket bound to the specified SocketAddress.<\/span><\/li>\n<li style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">SocketAddress is an abstract class that is implemented by the concrete class InetSocketAddress.<\/span><\/li>\n<\/ul>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Create UDP Socket<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To develop UDP socket communication, the client uses a constructor that does not require a port number. This constructor just binds the DatagramSocket to any available local port. It doesn&#8217;t matter what port the client is connected to because the DatagramPackets will contain the addressing information. The server gets the port number from the DatagramPackets and send its response to that port.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Methods of Datagram Socket<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>DatagramSocket defines many methods. Two of the most important are send( ) and receive( ).<\/p>\n<ul>\n<li>void send(DatagramPacket <em>packet<\/em>) throws IOException<\/li>\n<li>void receive(DatagramPacket <em>packet<\/em>) throws IOException<\/li>\n<\/ul>\n<p style=\"text-align: justify\">\u00a0 \u00a0 1. The send( ) method sends packet to the port specified by <em>packet<\/em>.<\/p>\n<p style=\"text-align: justify\">2. The receive() method waits for a packet to be received from the port specified by <em>packet<\/em> and returns the result.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>DatagramPacket class<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The Constructors of DatagramPacket are listed below,<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 DatagramPacket(byte buf[], int offset, int length)<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 This constructor constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">2. DatagramPacket(byte buf[], int length)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 This constructor constructs a DatagramPacket for receiving packets of length length.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">3. DatagramPacket(byte buf[], int offset, int length, InetAddress address, int port).<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">4. DatagramPacket(byte buf[], int offset, int length, SocketAddress address)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 This constructor constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">5. DatagramPacket(byte buf[], int length, InetAddress address, int port)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length to the specified port number on the specified host.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">6.DatagramPacket(byte buf[], int length, SocketAddress address)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0This constructor constructs a datagram packet for sending packets of length length to the specified port number on the specified host.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Methods of DatagramPacket<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1.\u00a0 void setAddress(InetAddress iaddr)<\/p>\n<p style=\"text-align: justify\">Sets the IP address of the machine to which this datagram is being sent.<\/p>\n<p style=\"text-align: justify\">2. InetAddress getAddress()<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0Returns the IP address of the machine to\u00a0 which this datagram is being sent or from which the datagram was received.<\/p>\n<p style=\"text-align: justify\">3. void setSocketAddress(SocketAddress address)<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.<\/p>\n<p style=\"text-align: justify\">4. SocketAddress getSocketAddress()<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.<\/p>\n<p style=\"text-align: justify\">5. void setPort(int iport)<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0 Sets the port number on the remote host to which this datagram is being sent.<\/p>\n<p style=\"text-align: justify\">6. int getPort()<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">7. byte[] getData()<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the data buffer.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">8.int getOffset()<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 Returns the offset of the data to be sent or the offset of the data received.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">9.int getLength()<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0Returns the length of the data to be sent or the length of the data received.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">10. void setData(byte[] buf, int offset, int length)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 Set the data buffer for this packet.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">11. void setData(byte[] buf)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0Set the data buffer for this packet.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">12. void setLength(int length)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Set the length for this packet.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">Example of ChatUDP Server and ChatUDP Client<\/strong><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 \u00a0An example is illustrated for chat client server communication with UDP sockets.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>ChatUDP Server<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Datagram socket is created and binded with the port number 6000 on the localhost machine.A Datagram Packet &#8216;dp1&#8217; is constructed to receive datagrams sent from the client. Once the datagram is received using the method receive(dp1), the byte data is obtained using dp1.getData() method. Then the bytes are converted to String and displayed on the screen.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Message from the user is again read as a String which is then converted to bytes using s.getBytes() method. A DatagramPacket &#8216;dp2&#8217; is then constructed with the buffer data and sent to the specified port number extracted from the received datagram &#8216;dp1&#8217;using the method dp1.getPort() and to the specified host extracted from received datagram &#8216;dp1&#8217; using the method dp1.getAddress(). This whole chat runs in a loop until the user enters a string &#8216;exit&#8217;.<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 import java.io.*;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 import java.net.*;\u00a0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0public class ChatUDPServer1<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 \u00a0{<\/span><\/p>\n<div>\n<p>\u00a0 \u00a0 public static void main(String args[]) throws Exception<\/p>\n<p>{<\/p>\n<p>DatagramSocket ds = new DatagramSocket(6000); byte[] b;<\/p>\n<p>DatagramPacket dp1,dp2;<\/p>\n<p>String s;<\/p>\n<p>BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); b = new byte[1024];<\/p>\n<p>do<\/p>\n<p>{<\/p>\n<p>dp1 = new DatagramPacket(b, b.length);<\/p>\n<p>ds.receive(dp1);<\/p>\n<p>b=dp1.getData();<\/p>\n<p>\/* Don&#8217;t use s=new String(b,0,0,b.length);&#8212;&#8211;is wrong. Use only dp1.getLength() *\/<\/p>\n<p>\/\/s=new String(b,0,0,dp1.getLength());<\/p>\n<p>s = new String(b);<\/p>\n<p>System.out.println(s);<\/p>\n<p>if(s.equals(&#8220;exit&#8221;))<\/p>\n<p>break;<\/p>\n<p>s=br.readLine();<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">\u00a0 \u00a0b=s.getBytes();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0dp2 = new DatagramPacket(b, s.length(), dp1.getAddress(),dp1.getPort());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 ds.send(dp2);\u00a0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 }while(!s.equals(&#8220;exit&#8221;));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 }<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0<\/strong><\/p>\n<p><strong> ChatUDPClient<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Datagram socket is created and binded with the available port number on the localhost machine. Message from the user is read as a string which is then converted to bytes using s.getBytes() method. A DatagramPacket &#8216;dp1&#8217; is then constructed with the buffer data and sent to the specified port number 6000 and on the specified host given as local host where the server is running.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Datagram Packet &#8216;dp2&#8217; is constructed to receive datagrams sent from the server. Once the datagram is received using ds.receive(dp2) the byte data is obtained using dp2.getData() method. Then the bytes are converted to string and displayed on the screen. This whole chat runs in a loop until the user enters a string &#8216;exit&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>public class ChatUDPClient1<\/p>\n<p>{<\/p>\n<p>public static void main(String args[]) throws Exception<\/p>\n<p>{<\/p>\n<p>DatagramSocket ds = new DatagramSocket();<\/p>\n<p>DatagramPacket dp1,dp2;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">BufferedReader br=new BufferedReader(new InputStreamReader(System.in));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">byte b[];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String s;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(s.equals(&#8220;exit&#8221;))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0\/<\/span><span style=\"text-align: initial;font-size: 1em\">* To test whether the packet contains only the required data (i.e., not empty spaces)*\/ \/* Use\u00a0 \u00a0 b=dp1.getData();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0System.out.println(new String(b,0,0,dp1.getLength()));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">do<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">*\/<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s=br.readLine();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">b=new byte[1024];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">b=s.getBytes();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">dp1=new DatagramPacket(b, s.length(), InetAddress.getByName(&#8220;localhost&#8221;), 6000); ds.send(dp1);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">dp2=new DatagramPacket(b, b.length);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ds.receive(dp2);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">b=dp2.getData();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">s=new String(b);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(s);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}while(!s.equals(&#8220;exit&#8221;));<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Output of ChatUDP Server &amp; Client<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>The output of Chat UDP server and Chat UDP client is shown in Figure 23.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-206 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120.png\" alt=\"\" width=\"605\" height=\"240\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120.png 605w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120-300x119.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120-65x26.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120-225x89.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-120-350x139.png 350w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure: 23.1 Output of ChatUDP Server &amp; ChatUDP Client<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Multicast Communication<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Multicasting sends data from one host to many different hosts, but not to everyone. The data goes only to clients that have expressed interest in the data by joining a particular multicast group. Multicast Sockets in Java uses the DatagramPacket class along with a new MulticastSocket class. The IETF has set aside addresses from 224.0.0.1 to 239.255.255.255 specifically for multicasting.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Multicast Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Four Key Operations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The behavior of MulticastSocket is very similar to DatagramSocket\u2019s. We can use a normal DatagramSocket to send and receive unicast and broadcast datagrams and to send multicast datagrams but in order to receive multicast datagrams, we need a MulticastSocket. The four key operations while performing multicast communication is listed below,<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Join a multicast group<\/p>\n<p>2. Send data to the member of the group<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">3. Receive data from the group<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">4.\u00a0 Leave the group\u00a0<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0java.net.MulticastSocket<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>It is the subclass of java.net.DatagramSocket and constructed in the same way.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Some extra methods used here are:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 void joinGroup(InetAddress mcastGroup)<\/p>\n<p>Enter the specifies group so that you can send or receive datagrams<\/p>\n<p>2. void leaveGroup(InetAddress mcastGroup)<\/p>\n<p>Leave a group that you previously joined<\/p>\n<p>3. void setTimeToLive(int ttl)<\/p>\n<p>Sets how far your datagrams will travel before routers ignore them<\/p>\n<p>4. int getTimeToLive()<\/p>\n<p>Get the default time-to-live for multicast packets sent out on the socket.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MulticastSender<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The steps involved in sending datagrams through Multicast Socket are,<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Create the MulticastSocket.<\/p>\n<p>2. Join the multicast group(s) (on startup).<\/p>\n<p>3. Create the DatagramPacket.<\/p>\n<p>4. Send the packet through the socket.<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">5. Leave the multicast group (on exit).<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0Snippet code to demonstrate the above steps to send datagrams through Multicast socket.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>InetAddress multicastGroup =\u00a0 InetAddress.getByName(multicastGroupAddr);<\/p>\n<p>MulticastSocket socket = new MulticastSocket();<\/p>\n<p>socket.joinGroup(multicastGroup);<\/p>\n<p>socket.setTimeToLive(5);<\/p>\n<p>byte[] data = \u201cThis is the message\u201d.getBytes();<\/p>\n<p>DatagramPacket datagram = new DatagramPacket(data, data.length);<\/p>\n<p>datagram.setAddress(multicastGroup);<\/p>\n<p>datagram.setPort(1234);<\/p>\n<p>socket.send(datagram);<\/p>\n<p>socket.leaveGroup(multicastGroup);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>MulticastReceiver<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The steps involved in receiving datagrams through Multicast Socket are,<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1. Create a multicast socket on an <em>agreed<\/em> port.<\/p>\n<p>2. Join the multicast group (on startup).<\/p>\n<p>3. Create an empty datagram.<\/p>\n<p>4. Wait for datagram to be delivered on the socket.<\/p>\n<p>5. Unpack and use the datagram.<\/p>\n<p>6. Leave the multicast group (on exit).<\/p>\n<\/div>\n<p><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 Snippet code to demonstrate the above steps to receive datagrams through Multicast socket.<\/strong><\/p>\n<div>\n<p>\u00a0 \u00a0 \u00a0InetAddress multicastGroup =\u00a0 InetAddress.getByName(multicastGroupAddr);<\/p>\n<p>MulticastSocket socket = new MulticastSocket(1234);<\/p>\n<p>socket.joinGroup(multicastGroup);<\/p>\n<p>byte[] data = new byte[1000];<\/p>\n<p>DatagramPacket packet = new DatagramPacket(data, data.length); socket.receive(packet);<\/p>\n<p>String message = new String(packet.getData(), 0, packet.getLength()); socket.leaveGroup(multicastGroup);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example of MulticastGroup<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Initially a multicast socket is created on an <em>agreed<\/em> port. To receive the multicast messages one has to join the multicast group (on startup) using the method ds.joinGroup(group). An empty datagram is then created to receive the message. Then the client waits for datagram to be delivered on the socket. The datagram is then unpacked and used. Finally it leaves the multicast group on exit.<\/p>\n<p>&nbsp;<\/p>\n<p>import java.io.*;<\/p>\n<p>import java.net.*;<\/p>\n<p>public class MulticastGroup<\/p>\n<p>{<\/p>\n<p>public static void main(String args[])<\/p>\n<p>{<\/p>\n<p>InetAddress group = null;<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">int port = 0;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">group = InetAddress.getByName(args[0]);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">port = Integer.parseInt(args[1]);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch( Exce ption e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.err.println(&#8220;usage&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.exit(1);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">MulticastSocket ds = null;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ds = new MulticastSocket(port);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ds.joinGroup(group);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">byte[] buffer = new byte[8192];<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0 while(true)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0\u00a0 {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ds.receive(dp);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String s = new String(dp.getData());<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(s);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/\u00a0\u00a0 }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}\u00a0<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(IOException e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.err.println(e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">finally<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(ds != null)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">try{ ds.leaveGroup(group);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ds.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(IOException e){}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>Example of Multicast Sender<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Initially the Multicast IP address 228.5.6.7 and port number 8759 are given as command line arguments. Datagram packet is constructed to the given IP address and port number. MulticastSocket is then created using the constructor of the class. The Multicast sender is joined in the the multicast group(s) using the method ms.joinGroup(group). The created DatagramPacket is then sent through the socket. Finally it leaves the multicast group on exit.<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0 \u00a0import java.io.*;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0import java.net.*;<\/span><\/p>\n<div>\n<p>\u00a0 \u00a0 \u00a0public class MulticastSender<\/p>\n<p>{<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>InetAddress group = null;<\/p>\n<p>int port = 0;<\/p>\n<p>try<\/p>\n<p>{<\/p>\n<p>group = InetAddress.getByName(args[0]);<\/p>\n<p>port = Integer.parseInt(args[1]);<\/p>\n<p>}<\/p>\n<p>catch( Exception e)<\/p>\n<p>{<\/p>\n<p>System.err.println(e);<\/p>\n<p>System.exit(1);<\/p>\n<p>}<\/p>\n<p>byte[] data = &#8220;here multicast data&#8221;.getBytes();<\/p>\n<p>DatagramPacket dp = new DatagramPacket(data, data.length, group, port); try{<\/p>\n<p>MulticastSocket ms = new MulticastSocket();<\/p>\n<p>ms.joinGroup(group);<\/p>\n<p>ms.send(dp);<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">ms.leaveGroup(group);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">ms.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(SocketException se)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.err.println(se);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">catch(IOException e)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.err.println(e);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">}\u00a0 }<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Output of Multicast Sockets<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The output of Multicast communication is shown in Figure 23.2. There are four entities, one act as a multicast sender and the other three are multicast clients who receive the message from the sender. All the four entities has to join the multicast group for communication.<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121.png\" alt=\"\" width=\"635\" height=\"306\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121.png 635w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121-300x145.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121-225x108.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-121-350x169.png 350w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/p>\n<p style=\"text-align: center\"><strong>Figure 23.2 Multicast communication<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>TCP Socket Options<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Several methods can be used to set various socket options. Most of the time the defaults are fine.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. public void setTcpNoDelay(boolean on) throws SocketException<\/p>\n<p style=\"text-align: justify\">2. public boolean getTcpNoDelay() throws SocketException<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0 Disable Nagle&#8217;s algorithm. Valid for (client) Sockets.<\/p>\n<p style=\"text-align: justify\">3. public void setSoLinger(boolean on, int val) throws SocketException<\/p>\n<p style=\"text-align: justify\">4. public int getSoLinger() throws SocketException<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0 Specify a linger-on-close timeout. Valid for (client) Sockets.<\/p>\n<p style=\"text-align: justify\">5. public synchronized void setSoTimeout(int timeout) throws SocketException<\/p>\n<p style=\"text-align: justify\">6. public synchronized int getSoTimeout() throws SocketException<\/p>\n<p style=\"text-align: justify\">\u00a0 \u00a0 Specify a timeout on blocking socket operations. Valid for all sockets like Socket, ServerSocket, DatagramSocket.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module explains about UDP socket communication in Java. This module gives an idea about DatagramPacket and DatagramSocket classes to build UDP communication. This module also explores about Multicasting with UDP sockets.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>References<\/strong><\/p>\n<ol>\n<li><em>download.java.net\/jdk7\/archive\/b123\/docs\/api\/java\/net\/<strong>DatagramPacket<\/strong>.html<\/em><\/li>\n<li><em style=\"text-align: initial;font-size: 1em\">https:\/\/www.cs.auckland.ac.nz\/~jmor159\/364\/<strong>ppt<\/strong>\/A<strong>Java<\/strong>Networking.<strong>ppt<\/strong><\/em><\/li>\n<\/ol>\n<p><strong>\u00a0 \u00a0Web Links<\/strong><\/p>\n<ul>\n<li>Herbert Schildt, &#8221; <em>Java: The Complete Reference&#8221;, 9th Edition,<\/em> Mcgraw-Hill, 2014.<\/li>\n<li><em>https:\/\/www.cs.auckland.ac.nz\/~jmor159\/364\/<strong>ppt<\/strong>\/A<strong>Java<\/strong>Networking.<strong>ppt<\/strong><\/em><\/li>\n<li><em>download.java.net\/jdk7\/archive\/b123\/docs\/api\/java\/net\/<strong>DatagramPacket<\/strong>.html<\/em><\/li>\n<\/ul>\n","protected":false},"author":4,"menu_order":21,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-205","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/205\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/205\/revisions\/563"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/205\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=205"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=205"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}